Winform打印(PrintDocument)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaobw831/article/details/83866526

PrintDocument

在 Windows 窗体应用程序打印时,主要使用PrintDocument 组件,其 定义一种可重用的可发送到打印机上的对象。

示例

基础示例

private StreamReader streamToPrint;
private Font printFont;
private void btnPrintFromTxtFile_Click(object sender, EventArgs e)
{
    try
    {
        streamToPrint = new StreamReader($"{AppDomain.CurrentDomain.BaseDirectory}Files\\PrintTemplate.txt");
        try
        {
            printFont = new Font("宋体", 10);
            PrintDocument pd = new PrintDocument();
            PaperSize ps = pd.PrinterSettings.PaperSizes.Cast<PaperSize>().FirstOrDefault(p => p.PaperName == "A4");
            pd.DefaultPageSettings.Landscape = true;
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            pd.Print();
        }
        finally
        {
            streamToPrint.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
   float linesPerPage = 0;
   float yPos = 0;
   int count = 0;
   float leftMargin = ev.MarginBounds.Left;
   float topMargin = ev.MarginBounds.Top;
   string line = null;

   // Calculate the number of lines per page.
   linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);

   // Print each line of the file
   while (count < linesPerPage && (line = streamToPrint.ReadLine()) != null)
   {
       yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
       ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
       count++;
   }

   // If more lines exits,print another page.
   if (line != null)
       ev.HasMorePages = true;
   else
       ev.HasMorePages = false;
}

如何:打印 Windows 窗体中的多页文本文件

private PrintDocument pdMultiPage = new PrintDocument();
private string stringToPrint;
private void brnPrintMultiPageTextFile_Click(object sender, EventArgs e)
{
    pdMultiPage.PrintPage += new PrintPageEventHandler(pdMultiPage_PrintPage);
    ReadFile();
    pdMultiPage.Print();
}
private void ReadFile()
{
    string docName = "PrintMultiPageTemplate.txt";
    string docPath = $"{AppDomain.CurrentDomain.BaseDirectory}\\Files\\";
    pdMultiPage.DocumentName = docName;
    using (FileStream stream = new FileStream(docPath + docName, FileMode.Open))
    using (StreamReader reader = new StreamReader(stream))
    {
        stringToPrint = reader.ReadToEnd();
    }

}

private void pdMultiPage_PrintPage(object sender, PrintPageEventArgs e)
{
    int charactersOnPage = 0;
    int linesPerPage = 0;

    Font font = new Font("宋体", 18);

    // Sets the velues of charatersOnPage to the number of characters
    // of stringToPrint that will fit within the bounds of page.
    e.Graphics.MeasureString(stringToPrint, font, e.MarginBounds.Size, StringFormat.GenericTypographic, out charactersOnPage, out linesPerPage);

    // Draw the string within the bounds of the page
    e.Graphics.DrawString(stringToPrint, font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic);

    // Remove the portion of the string that has been printed.
    stringToPrint = stringToPrint.Substring(charactersOnPage);

    // Check to see if more pages are to be printed.
    e.HasMorePages = (stringToPrint.Length > 0);
}

参考

PrintDocument 类

Windows 窗体打印支持

本文源码

展开阅读全文

没有更多推荐了,返回首页