如何用C#打印人类可读的文档?
我正在为用户开发一些需要打印输出的工具,并且我在System.Drawing和System.Graphics类中获得了一些很好的结果,可以对要打印的文档进行布局,但是我需要更多的页面布局功能 到目前为止,我使用并取得良好效果的代码如下:如何用C#打印人类可读的文档?,c#,system.drawing,system.drawing.graphics,C#,System.drawing,System.drawing.graphics,我正在为用户开发一些需要打印输出的工具,并且我在System.Drawing和System.Graphics类中获得了一些很好的结果,可以对要打印的文档进行布局,但是我需要更多的页面布局功能 到目前为止,我使用并取得良好效果的代码如下: //create the Print Dialog PrintDialog pd = new PrintDialog(); //Create Document to Print PrintDocument doc = new Print
//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来控制布局,因为我非常熟悉,我相信其他人也会对这种技术感兴趣。您是否尝试过用Crystal Reports来设计布局?这听起来像是试图通过到中国去看看长城是如何建造的来建造冰棒摊。您使用的方法设计得非常严格,如果您需要灵活性,如调整纸张大小、页面方向等,则需要通过调整纸张大小来修改
绘图
调用,或者使用更灵活的方法,如Crystal Reports。从表面上看,这可能让人望而生畏,但实际上并没有那么糟糕。如果你不想拥有如此强大的功能,还有其他的报告生成框架。对于这种过分夸张的比较,我深表歉意。我们是非营利组织,不使用Crystal Reports设置。我只是认为,学习一些需要如此多开销的东西,并为一个简单的应用程序进行设置,让用户只需填写一张表格和一个“打印”按钮,这会适得其反。我们可能希望将其集成到其他系统中,但目前还没有。此外,打印的尺寸和项目不会更改,并且始终位于页面下。如果您使用WPF,另一种方法是创建一个带有文本框/标签等的表单,可以使用您的数据填充该表单&使用PrintDialog PrintVisual方法。您可以获得所选打印机的打印功能,以缩放和设置横向/纵向模式。