locked
How to print Invoice in WPF RRS feed

  • Question

  • Dear All,

    I am create a Point Of Sales System in WPF. I want to print invoice while completion of Order. Data Will be provided from TextBox for Invoice,

    (txtinvoices, txtQty, txtTotalPrice, txtAmountPaid and txtDate) are some textbox i want to print on Invoices. There is one barcode Image as well for each order that is needed to print on Invoice.

    Anyone can help me in printing in WPF ???

    Thanks in Advance


    Jazaib Hussain

    Sunday, May 5, 2013 9:26 PM

Answers

  • you got a few options that you could use.

    Use the FlowDocument class to build the report template and then put in the numbers.

    Use external templates like word document and use merge fields and replace using the interop.

    Use 3rd party tool for the reporting like crystal which is free.

    use MicrosoftReporting classes which are free also.

    all these options will allow you to print through their respective viewers and flowdocument you can print direct.

    Hope that helps

    andy

    • Proposed as answer by Lisa Zhu Thursday, May 9, 2013 1:25 PM
    • Marked as answer by Jazaib Hussain Thursday, May 9, 2013 1:59 PM
    Sunday, May 5, 2013 10:19 PM
  • Hi Jazib,

    you would first create the report in the VS designer (or Microsoft reporting tool) and set parameters i.e. @Title and @ImageSource.

    then in your c# code you pass the parameters like

    LocalReport report = new LocalReport();
    report.EnableExternalImages = true;
    report.ReportPath = @"YourReport.rdlc";
    report.SetParameters(new ReportParameter("Title", txtBox.Text));
    report.SetParameters(new ReportParameter("ImageSource", "PathToImage"));

    this report can then be set as the local report of a reportviewer control which has print and export options on it, or if you want to print direct you can export it to a memory stream that you send to a PrintDocument.

    within the report itself you can add image fields, or text fields and set their source to parameter values like

    =Parameters!Title.Value

    hope that helps you get started

    cheers

    andy

    Monday, May 6, 2013 10:07 AM
  • Hi Jazib,

    i am not sure if you can pass an image as a stream to microsoftreports, or if any of the others support this. If you save the image in the database it is easy or if you save the image to a temporary file and then pass this as the parameter like above, and then just remove the file afterwards.

    in regards to sending straight to printer, create the report as shown above then call the export function and then print report function as shown below. could prob do with a bit of tidying up but this is example from a service i did a while back.

    this prints fine to normal printers, but you might need different code specific to the printer, but you should be able to get that info from the printer manufacturer website if you do.

            #region Reporting
            private int m_currentPageIndex;
            private IList<Stream> m_streams;
    
            private Stream CreateStream(string name, string fileNameExtension, Encoding encoding,
                string mimeType, bool willSeek)
            {
                Stream stream = new MemoryStream();
                m_streams.Add(stream);
                return stream;
            }
    
            private void Export(LocalReport report)
            {
                string deviceInfo =
                    @"<DeviceInfo>
                    <OutputFormat>EMF</OutputFormat>
                    <PageWidth>8.5in</PageWidth>
                    <PageHeight>11in</PageHeight>
                    <MarginTop>0.25in</MarginTop>
                    <MarginLeft>0.25in</MarginLeft>
                    <MarginRight>0.25in</MarginRight>
                    <MarginBottom>0.25in</MarginBottom>
                </DeviceInfo>";
                Warning[] warnings;
                m_streams = new List<Stream>();
                report.Render("Image", deviceInfo, CreateStream, out warnings);
                foreach (Stream stream in m_streams)
                    stream.Position = 0;
            }
    
            private void ReportPrint()
            {
                if (m_streams == null || m_streams.Count == 0)
                    throw new Exception("Error: no stream to print.");
                PrintDocument printDoc = new PrintDocument();
                printDoc.PrinterSettings.PrinterName = EDI_PRINTER;
                if (!printDoc.PrinterSettings.IsValid)
                {
                    throw new Exception("Error: cannot find printer.");
                }
                else
                {
                    printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
                    m_currentPageIndex = 0;
                    printDoc.Print();
                }
    
            }
    
            private void printDoc_PrintPage(object sender, PrintPageEventArgs ev)
            {
                Metafile pageImage = new
       Metafile(m_streams[m_currentPageIndex]);
    
                // Adjust rectangular area with printer margins.
                Rectangle adjustedRect = new Rectangle(
                    ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
                    ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
                    ev.PageBounds.Width,
                    ev.PageBounds.Height);
    
                // Draw a white background for the report
                ev.Graphics.FillRectangle(Brushes.White, adjustedRect);
    
                // Draw the report content
                ev.Graphics.DrawImage(pageImage, adjustedRect);
    
                // Prepare for the next page. Make sure we haven't hit the end.
                m_currentPageIndex++;
                ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
            }
            #endregion

    andy
    • Proposed as answer by Lisa Zhu Thursday, May 9, 2013 1:25 PM
    • Marked as answer by Jazaib Hussain Tuesday, June 18, 2013 9:30 PM
    Monday, May 6, 2013 7:07 PM

All replies

  • you got a few options that you could use.

    Use the FlowDocument class to build the report template and then put in the numbers.

    Use external templates like word document and use merge fields and replace using the interop.

    Use 3rd party tool for the reporting like crystal which is free.

    use MicrosoftReporting classes which are free also.

    all these options will allow you to print through their respective viewers and flowdocument you can print direct.

    Hope that helps

    andy

    • Proposed as answer by Lisa Zhu Thursday, May 9, 2013 1:25 PM
    • Marked as answer by Jazaib Hussain Thursday, May 9, 2013 1:59 PM
    Sunday, May 5, 2013 10:19 PM
  • Dear Andy,

    Well i am not storing Images in Database.

    I am not too expert in this field. Can you give me some demo code for one TextBox and one image printing in MicrosoftReporting please ???


    Thanks


    Jazaib Hussain


    Sunday, May 5, 2013 10:21 PM
  • Hi Jazib,

    you would first create the report in the VS designer (or Microsoft reporting tool) and set parameters i.e. @Title and @ImageSource.

    then in your c# code you pass the parameters like

    LocalReport report = new LocalReport();
    report.EnableExternalImages = true;
    report.ReportPath = @"YourReport.rdlc";
    report.SetParameters(new ReportParameter("Title", txtBox.Text));
    report.SetParameters(new ReportParameter("ImageSource", "PathToImage"));

    this report can then be set as the local report of a reportviewer control which has print and export options on it, or if you want to print direct you can export it to a memory stream that you send to a PrintDocument.

    within the report itself you can add image fields, or text fields and set their source to parameter values like

    =Parameters!Title.Value

    hope that helps you get started

    cheers

    andy

    Monday, May 6, 2013 10:07 AM
  • Dear Andy,

    Sorry i never use any reporting tool. Can you further guide me how i can get image from the code given below and how i can show it on the report every time... ???

    //InvoiceID Generate txtInvoiceID.Text = DateTime.Now.ToString("sMMHHmyyyydd" + userId);

    //Barcode Generation BarcodeEncoder enc = new BarcodeEncoder(); WriteableBitmap image = enc.Encode(BarcodeFormat.Code39, txtInvoiceID.Text); barCodeImage.Source = image;

    On the Other Hand i am using thermal invoice printer with Touch Screen PC. I don't want to show any dialog in between invoice form and print. Just Click on invoice form and it must directly print rather than showing any dialogue in middle.

    Bundle of Thanks


    Jazaib Hussain



    • Edited by Jazaib Hussain Monday, May 6, 2013 5:23 PM Add Information
    Monday, May 6, 2013 5:06 PM
  • Hi Jazib,

    i am not sure if you can pass an image as a stream to microsoftreports, or if any of the others support this. If you save the image in the database it is easy or if you save the image to a temporary file and then pass this as the parameter like above, and then just remove the file afterwards.

    in regards to sending straight to printer, create the report as shown above then call the export function and then print report function as shown below. could prob do with a bit of tidying up but this is example from a service i did a while back.

    this prints fine to normal printers, but you might need different code specific to the printer, but you should be able to get that info from the printer manufacturer website if you do.

            #region Reporting
            private int m_currentPageIndex;
            private IList<Stream> m_streams;
    
            private Stream CreateStream(string name, string fileNameExtension, Encoding encoding,
                string mimeType, bool willSeek)
            {
                Stream stream = new MemoryStream();
                m_streams.Add(stream);
                return stream;
            }
    
            private void Export(LocalReport report)
            {
                string deviceInfo =
                    @"<DeviceInfo>
                    <OutputFormat>EMF</OutputFormat>
                    <PageWidth>8.5in</PageWidth>
                    <PageHeight>11in</PageHeight>
                    <MarginTop>0.25in</MarginTop>
                    <MarginLeft>0.25in</MarginLeft>
                    <MarginRight>0.25in</MarginRight>
                    <MarginBottom>0.25in</MarginBottom>
                </DeviceInfo>";
                Warning[] warnings;
                m_streams = new List<Stream>();
                report.Render("Image", deviceInfo, CreateStream, out warnings);
                foreach (Stream stream in m_streams)
                    stream.Position = 0;
            }
    
            private void ReportPrint()
            {
                if (m_streams == null || m_streams.Count == 0)
                    throw new Exception("Error: no stream to print.");
                PrintDocument printDoc = new PrintDocument();
                printDoc.PrinterSettings.PrinterName = EDI_PRINTER;
                if (!printDoc.PrinterSettings.IsValid)
                {
                    throw new Exception("Error: cannot find printer.");
                }
                else
                {
                    printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
                    m_currentPageIndex = 0;
                    printDoc.Print();
                }
    
            }
    
            private void printDoc_PrintPage(object sender, PrintPageEventArgs ev)
            {
                Metafile pageImage = new
       Metafile(m_streams[m_currentPageIndex]);
    
                // Adjust rectangular area with printer margins.
                Rectangle adjustedRect = new Rectangle(
                    ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
                    ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
                    ev.PageBounds.Width,
                    ev.PageBounds.Height);
    
                // Draw a white background for the report
                ev.Graphics.FillRectangle(Brushes.White, adjustedRect);
    
                // Draw the report content
                ev.Graphics.DrawImage(pageImage, adjustedRect);
    
                // Prepare for the next page. Make sure we haven't hit the end.
                m_currentPageIndex++;
                ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
            }
            #endregion

    andy
    • Proposed as answer by Lisa Zhu Thursday, May 9, 2013 1:25 PM
    • Marked as answer by Jazaib Hussain Tuesday, June 18, 2013 9:30 PM
    Monday, May 6, 2013 7:07 PM