我正在研究一些需要为用户打印输出的工具,我在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控制布局,因为我非常熟悉,我相信其他人会对这种技术感兴趣好。