Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Print Using Flow Document In WPF
WhatsApp
Neha
8y
42.8
k
0
4
25
Blog
Printing in WPF using Windows driver and Flow document
Printing using HTML and the web browser can also be done in WPF but as per my experience, in WPF printing, there is a little delay in printing using the web browser method. So, the solution to this delayed printing is using the Flow document for printing instead of web browser.
Flow Document
Flow document offers run time re-flow of content dynamically, on the basis of various factors, such as window size, and device resolution.
Let's take an example to understand it. Also, we will discuss various content elements that are required to write a document using Flow document.
We need to print a receipt with the details of company and customer purchases. The main factors that we need for formatting are 'Space' , 'Next Line', 'Margins', and 'Image print':
The three basic things that we continuously use to write a document in Flow document are Paragraph, Spam, and document blocks. Please see the below program and commenting of program to learn the use of various factors.
FlowDocument doc =
new
FlowDocument();
//Call this method to class where you need to code for printing.
public
void
PrintUsingFlowDocument()
{
//We need to use paragraphs to addthe content in the blocks of flow document.
string s1 =
"abc"
;
string s2 =
"xyz"
;
Paragraph p =
new
Paragraph();
Span s =
new
Span();
s =
new
Span(
new
Run(
"Company Name :Test Company"
));
s.Inlines.Add(
new
LineBreak());
//Line break is used for next line.
p.Inlines.Add(s);
// Add the span content into paragraph.
//If we have some dynamic text the span in flow document does not under " " as space and we need to use "\t" for space.
s =
new
Span(
new
Run(s1+
"\t"
+s2));
//we need to use \t for space between s1 and s2 content.
s.Inlines.Add(
new
LineBreak());
p.Inlines.Add(s);
//Give style and formatting to paragraph content.
p.FontSize = 14;
p.FontStyle = FontStyles.Normal;
p.TextAlignment = TextAlignment.Center;
doc.Blocks.Add(p);
//Print Image or barcode in flow document.
//let we need to print barcode for 123456.
BarCode("123456");
doc.Name = "FlowDoc";
doc.PageWidth = 210;
doc.PagePadding = new Thickness(3, 5, 2, 4);
// Create IDocumentPaginatorSource from FlowDocument
IDocumentPaginatorSource idpSource = doc;
// Call PrintDocument method to send document to printer
printDlg.PrintDocument(idpSource.DocumentPaginator, "Receipt Printing.");
}
public
void
BarCode(string stringForBarCode)
{
if
(stringForBarCode != String.Empty)
{
string path = @
"D:\\data\\Images\\BarCodeImages\\"
;
if
(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
System.IO.DirectoryInfo di =
new
DirectoryInfo(path);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
}
else
{
System.IO.DirectoryInfo di =
new
DirectoryInfo(path);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
string saveLocation =
"C:\\BarCodeImages\\" + Convert.ToString(stringForBarCode) + "
.png
"; //"
/" + filename; \
GenerateImageString(Convert.ToString(Convert.ToString(stringForBarCode))).Save(saveLocation, ImageFormat.Png);
AddBarCode(saveLocation,
"B"
);
}
}
}
//Convert string to barcode image.
private
System.Drawing.Image GenerateImageString(string uniqueCode)
{
//Read in the parameters
string strData = uniqueCode;
int
imageHeight = 50;
int
imageWidth = 200;
BarcodeLib.TYPE type = BarcodeLib.TYPE.UNSPECIFIED;
type = BarcodeLib.TYPE.CODE128;
System.Drawing.Image barcodeImage = null;
try
{
BarcodeLib.Barcode b =
new
BarcodeLib.Barcode();
b.IncludeLabel =
false
;
b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
barcodeImage = b.Encode(type, strData.Trim(), imageWidth, imageHeight);
System.IO.MemoryStream MemStream =
new
System.IO.MemoryStream();
barcodeImage.Save(MemStream, System.Drawing.Imaging.ImageFormat.Png);
byte[] imageBytes = MemStream.ToArray();
return
byteArrayToImage(imageBytes);
}
catch
(Exception ex)
{
throw
ex;
}
finally
{
barcodeImage.Dispose();
}
}
//Convert byte to image.
public
static
System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms =
new
MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
return
returnImage;
}
//Add barcode to document.
public
void
AddBarCode(string ImagePath, string text)
{
System.Windows.Controls.Image image =
new
System.Windows.Controls.Image();
BitmapImage bimg =
new
BitmapImage();
bimg.BeginInit();
bimg.UriSource =
new
Uri(ImagePath, UriKind.Absolute);
bimg.EndInit();
image.Source = bimg;
image.Width = 150;
image.Height = 50;
image.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
doc.Blocks.Add(
new
BlockUIContainer(image));
}
The dll required for barcode is attached. I added the Windows based reference also.
Thank you.
Fast Printing
Flow Document
WPF
Up Next
Printing WPF Window to Printer and Fit on a Page
Ebook Download
View all
WPF Simplified: Build Windows Apps Using C# and XAML
Read by 1.5k people
Download Now!
Learn
View all
Membership not found