首页 > 解决方案 > 当我使用drawing.printing打印pdf文档时,pdf文档存在字节不足

问题描述

当我打印文档时,文档变成这样: https ://gyazo.com/1b0ba3daf5b6ac8435c70adb822feaae

我已经以这种方式将其发送到打印机,但打印机打印我喜欢这样。

我在互联网上搜索了一些解决方案,但我一无所获。我从 Windows 站点获得此代码。

我已经尝试了很多,但我的想法已经用完了,我在互联网上找不到任何东西

//print function
for (int i = 0; i < 1; i++)
{
    try
    {
        streamToPrint = new StreamReader(FilePath);
        try
        {
             printFont = new Font("Arial", 10);
             PrintDocument pd = new PrintDocument();
             pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
             pd.PrinterSettings.PrinterName = printers;
             pd.Print();
        }
        finally
        {
            streamToPrint.Close();
        }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
}

//print page function
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 exist, print another page.
    if (line != null)
    {
        ev.HasMorePages = true;
        else
        ev.HasMorePages = false;
    }
}

标签: c#pdfprintingsystem.drawing

解决方案


推荐阅读