Print Using Flow Document In WPF

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.
  1. FlowDocument doc = new FlowDocument();  
  2. //Call this method to class where you need to code for printing.
  3.        public void PrintUsingFlowDocument()  
  4.        {  
  5.            //We need to use paragraphs to addthe content in the blocks of flow document.  
  6.            string s1 = "abc";  
  7.            string s2 = "xyz";  
  8.            Paragraph p = new Paragraph();  
  9.            Span s = new Span();  
  10.            s = new Span(new Run("Company Name :Test Company"));  
  11.            s.Inlines.Add(new LineBreak());//Line break is used for next line.  
  12.            p.Inlines.Add(s);// Add the span content into paragraph.  
  13.            //If we have some dynamic text the span in flow document does not under "    " as space and we need to use "\t"  for space.  
  14.            s = new Span(new Run(s1+"\t"+s2));//we need to use \t for space between s1 and s2 content.  
  15.            s.Inlines.Add(new LineBreak());  
  16.            p.Inlines.Add(s);  
  17.            //Give style and formatting to paragraph content.  
  18.            p.FontSize = 14;  
  19.            p.FontStyle = FontStyles.Normal;  
  20.            p.TextAlignment = TextAlignment.Center;  
  21.            doc.Blocks.Add(p);  
  22.            //Print Image or barcode in flow document.  
  23.            //let we need to print barcode for 123456.  
  24.            BarCode("123456");
  25. 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.");

  26.               
  27.        }  
  28.        public void BarCode(string stringForBarCode)  
  29.        {  
  30.            if (stringForBarCode != String.Empty)  
  31.            {  
  32.                string path = @"D:\\data\\Images\\BarCodeImages\\";  
  33.                if (!Directory.Exists(path))  
  34.                {  
  35.                    Directory.CreateDirectory(path);  
  36.                    System.IO.DirectoryInfo di = new DirectoryInfo(path);  
  37.                    foreach (FileInfo file in di.GetFiles())  
  38.                    {  
  39.                        file.Delete();  
  40.                    }  
  41.                }  
  42.                else  
  43.                {  
  44.                    System.IO.DirectoryInfo di = new DirectoryInfo(path);  
  45.   
  46.                    foreach (FileInfo file in di.GetFiles())  
  47.                    {  
  48.                        file.Delete();  
  49.                    }  
  50.                    string saveLocation = "C:\\BarCodeImages\\" + Convert.ToString(stringForBarCode) + ".png"; //"/" + filename; \  
  51.                    GenerateImageString(Convert.ToString(Convert.ToString(stringForBarCode))).Save(saveLocation, ImageFormat.Png);  
  52.                    AddBarCode(saveLocation, "B");  
  53.                }  
  54.            }  
  55.        }  
  56.   
  57.        //Convert string to barcode image.  
  58.        private System.Drawing.Image GenerateImageString(string uniqueCode)  
  59.        {  
  60.            //Read in the parameters  
  61.            string strData = uniqueCode;  
  62.            int imageHeight = 50;  
  63.            int imageWidth = 200;  
  64.   
  65.            BarcodeLib.TYPE type = BarcodeLib.TYPE.UNSPECIFIED;  
  66.            type = BarcodeLib.TYPE.CODE128;  
  67.            System.Drawing.Image barcodeImage = null;  
  68.            try  
  69.            {  
  70.                BarcodeLib.Barcode b = new BarcodeLib.Barcode();  
  71.                b.IncludeLabel = false;  
  72.                b.Alignment = BarcodeLib.AlignmentPositions.CENTER;  
  73.                barcodeImage = b.Encode(type, strData.Trim(), imageWidth, imageHeight);  
  74.                System.IO.MemoryStream MemStream = new System.IO.MemoryStream();  
  75.                barcodeImage.Save(MemStream, System.Drawing.Imaging.ImageFormat.Png);  
  76.                byte[] imageBytes = MemStream.ToArray();  
  77.                return byteArrayToImage(imageBytes);  
  78.            }  
  79.            catch (Exception ex)  
  80.            {  
  81.                throw ex;  
  82.            }  
  83.            finally  
  84.            {  
  85.                barcodeImage.Dispose();  
  86.            }  
  87.        }  
  88.   
  89.        //Convert byte to image.  
  90.        public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)  
  91.        {  
  92.            MemoryStream ms = new MemoryStream(byteArrayIn);  
  93.            System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);  
  94.            return returnImage;  
  95.        }  
  96.   
  97.        //Add barcode to document.  
  98.        public void AddBarCode(string ImagePath, string text)  
  99.        {  
  100.            System.Windows.Controls.Image image = new System.Windows.Controls.Image();  
  101.            BitmapImage bimg = new BitmapImage();  
  102.            bimg.BeginInit();  
  103.            bimg.UriSource = new Uri(ImagePath, UriKind.Absolute);  
  104.            bimg.EndInit();  
  105.            image.Source = bimg;  
  106.            image.Width = 150;  
  107.            image.Height = 50;  
  108.            image.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;  
  109.            doc.Blocks.Add(new BlockUIContainer(image));  
  110.        }  
The dll required for barcode is attached. I added the Windows based reference also.
 
Thank you.
Ebook Download
View all
Learn
View all