我已根据需要生成了一长串发票。我现在需要在打印机上打印出来,每页显示一张发票。
我已经查看了这些教程/帮助以及我的一些代码:
https://msdn.microsoft.com/en-us/library/cwbe712d%28v=vs.110%29.aspx
我主要跟随第二个。
我最终得到的结果是(使用单个按钮单独使用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();
以尝试排序但不起作用)。
我想知道的是,如何在每页保留一张发票的同时将字符串打印为单个字符串。
编辑:如何将字符串生成为打印机的多页图像?
答案 0 :(得分:0)
编辑:完整的解决方案。
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();
}
}
答案 1 :(得分:-3)
从我所看到的,这一行引起了你所描述的问题:
printDocument1.PrintPage += delegate(object sender1,
PrintPageEventArgs e1)
尝试将其更改为:
printDocument1.PrintPage = delegate(object sender1,
PrintPageEventArgs e1)