我正在开发一些需要为用户打印输出的工具,并且使用 System.Drawing 和 System.Graphics 类来布局要打印的文档,我已经取得了一些不错的效果,但是我需要更多的页面布局功能.
到目前为止,我使用并取得了良好效果的代码在这里:
//create the Print Dialog
PrintDialog pd = new PrintDialog();
//Create Document to Print
PrintDocument doc = new PrintDocument();
//Add document to dialog to print
pd.Document = doc;
//Adds the contents of the page
doc.PrintPage += new PrintPageEventHandler(pd_printpage);
//Launch the Print Dialog
DialogResult res = pd.ShowDialog();
//if everything is okay with the dialog, Print it.
if(res == DialogResult.OK)
{
doc.DefaultPageSettings.Landscape = false;
doc.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("Env10", 4, 8);
doc.Print();
}
实际打印在这里
//Generate a Graphics object
Graphics gfx = e.Graphics;
//Set the Font to print with
Font courier = new Font("Courier New", 12);
//check to see if data is not set to default.
if (check_text())
{
//if the manual radio is selected:
if (rd_ManRcpt.Checked == true)
{
//Place Amount on page.
gfx.DrawString(tx_amt.Text, courier, new SolidBrush(Color.Black), 55, 80);
//Place Date on page
gfx.DrawString(tx_date.Text, courier, new SolidBrush(Color.Black), 35, 105);
//Place Serial number to page
gfx.DrawString(tx_No.Text, courier, new SolidBrush(Color.Black), 250, 105);
//place contributer name to page.
gfx.DrawString(tx_CredTo.Text, courier, new SolidBrush(Color.Black), 75, 130);
//Place Address Information on Page
gfx.DrawString(tx_add1.Text + "/n" + tx_add2.Text + "/n" + tx_city.Text + ", " + tx_state.Text + " " + tx_zip.Text, courier, new SolidBrush(Color.Black), 75, 200);
}
else if (rd_BDRcpt.Checked == true)
{
gfx.DrawString("$40.75", courier, new SolidBrush(Color.Black), 515, 10);
gfx.DrawString("03/13/2017", courier, new SolidBrush(Color.Black), 625, 105);
gfx.DrawString("XXXXXXX", courier, new SolidBrush(Color.Black), 625, 165);
gfx.DrawString("XXXXXX", courier, new SolidBrush(Color.Black), 625, 215);
gfx.DrawString("Contributer Here", courier, new SolidBrush(Color.Black), 75, 175);
gfx.DrawString("Address Information Here.", courier, new SolidBrush(Color.Black), 75, 195);
}
这里的代码只是生成两张收据之一并打印出来。我不确定如何定义打印机将在 10 号信封页上打印的页面大小和方向。
这个问题的另一部分是如何完全控制正在打印的文档的布局,或者如果可能的话,使用 HTML 来控制布局,因为我非常熟悉,我相信其他人也会对这种技术感兴趣。