簡體   English   中英

C#從生成的格式化字符串中打印發票,每打印機頁面一張發票

[英]C# Printing invoices from a generated, formatted string, one invoice per printer page

我已經生成了一個單據格式的長發票。 我現在需要在打印機上打印出來,每頁顯示一張發票。

我看過這些教程/幫助以及我當時的一些代碼:

https://msdn.microsoft.com/zh-CN/library/cwbe712d%28v=vs.110%29.aspx

https://social.msdn.microsoft.com/Forums/zh-CN/93e54c4f-fd07-4b60-9922-102439292f52/c-printing-a-string-to-printer?forum=csharplanguage

我主要關注第二個。

我最終得到的是(使用單個按鈕的單個表單運行VS C#項目):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestPrint
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string stringToPrint = "Hello\r\nWorld\r\n\r\n<<< Page >>>World\r\nHello";

        private void button1_Click(object sender, EventArgs e)
        {

            //  Convert string to strings
            string[] seperatingChars = { "<<< Page >>>" };
            string[] printString = stringToPrint.Split(seperatingChars, System.StringSplitOptions.RemoveEmptyEntries);

            //  Connect to the printer
            PrintDocument printDocument1 = new PrintDocument();     // Stream to the printer

            //  Send to printer (reference: https://social.msdn.microsoft.com/Forums/en-US/93e54c4f-fd07-4b60-9922-102439292f52/c-printing-a-string-to-printer?forum=csharplanguage)
            foreach (string s in printString)
            {
                printDocument1.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
                {
                    e1.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black),
                    new RectangleF(0, 0, printDocument1.DefaultPageSettings.PrintableArea.Width,
                    printDocument1.DefaultPageSettings.PrintableArea.Height));
                };
                try
                {
                    printDocument1.Print();
                    printDocument1.Dispose();
                }
                catch (Exception ex)
                {
                    throw new Exception("Exception Occured While Printing", ex);
                }
            }
        }
    }
}

我將長字符串分解成要打印到每個頁面的部分,然后將其發送到打印機。 這對於第一個發票/頁面效果很好,但之后僅將每個頁面添加到第一個圖像上(我添加了printDocument1.Dispose();嘗試對其進行排序但不起作用)。

我想知道的是如何將字符串打印為單個字符串,同時每頁保留一張發票。

編輯:我如何生成字符串作為打印機的多頁圖像?

編輯:完整的解決方案。

 public partial class Form1 : Form
    {
        //string to print
        private string stringToPrint = "Hello\r\nWorld\r\n\r\n<<< Page >>>World\r\nHello";
        //list of strings/pages
        private List<string> pageData = new List<string>();
        //enumerator for iteration thru "pages"
        private IEnumerator pageEnumerator;
        //print document
        PrintDocument printDocument1;

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {

            //  Connect to the printer
            printDocument1 = new PrintDocument();
            //handle events
            printDocument1.BeginPrint += new PrintEventHandler(BeginPrint);
            printDocument1.PrintPage += new PrintPageEventHandler(PrintPage);


            try
            {
                //do print
                printDocument1.Print();
                printDocument1.Dispose();
            }
            catch (Exception ex)
            {
                throw new Exception("Exception Occured While Printing", ex);
            }

        }

        void BeginPrint(object sender, PrintEventArgs e)
        {
            //  Convert string to strings
            string[] seperatingChars = { "<<< Page >>>" };

            //generate some dummy strings to print
            pageData = stringToPrint.Split(seperatingChars, System.StringSplitOptions.RemoveEmptyEntries).ToList();

            // get enumerator for dummy strings
            pageEnumerator = pageData.GetEnumerator();

            //position to first string to print (i.e. first page)
            pageEnumerator.MoveNext(); 
        }

        private void PrintPage(object sender, PrintPageEventArgs e)
        {
            //define font, brush, and print area
            Font font = new Font("Times New Roman", 12);
            Brush brush = Brushes.Black;
            RectangleF area = new RectangleF(0, 0, printDocument1.DefaultPageSettings.PrintableArea.Width,
                    printDocument1.DefaultPageSettings.PrintableArea.Height);

            //print current page
            e.Graphics.DrawString(pageEnumerator.Current.ToString(), font, brush, area);

            // advance enumerator to determine if we have more pages.
            e.HasMorePages = pageEnumerator.MoveNext();

        }


    }

據我所知,此行導致了您描述的問題:

printDocument1.PrintPage += delegate(object sender1,
PrintPageEventArgs e1)

嘗試將其更改為:

printDocument1.PrintPage = delegate(object sender1,
PrintPageEventArgs e1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM