printdocument打印的页面太多

时间:2016-08-02 14:07:26

标签: c# printdocument

帮助,我有一个问题,PrintDocument打印太多页面没有明显的原因

不幸的是,错误有点难以重现。我在测试过程中从未能够重现它。仅当用户打印超过两页的文档时才会发生这种情况。 要打印的文件是多页Tiff图像

我的打印窗口中有以下代码:

PrintDocumentAdvanced p = new PrintDocumentAdvanced(tmpFileName);

p.PrinterSettings.PrinterName = "Prt-002";
p.PreparePrint();
p.Print();

PreparePrint方法设置应打印的页面。它读取tiff文件中的页数:

PreparePrint()
{
    Image img = Image.FromFile(tmp);
    Guid objGuid = img.FrameDimensionsList[0];
    FrameDimension objDimension = new FrameDimension(objGuid);
    int cnt = img.GetFrameCount(objDimension);
    PrinterSettings.FromPage = 1;
    PrinterSettings.ToPage = cnt;
}

打印时,会为每个页面调用:

private void PreparePrint(object o, PrintPageEventArgs e)
{
    Image img = Image.FromFile(tmp);
    Guid objGuid = img.FrameDimensionsList[0];
    FrameDimension objDimension = new FrameDimension(objGuid);

    // Checks if there are more pages to be printed

    Logger("Trying to print page " + lastPagePrinted + " Total pages: " + img.GetFrameCount(objDimension));

    if (img.GetFrameCount(objDimension) > lastPagePrinted + 1 && lastPagePrinted + 1 < PrinterSettings.ToPage)
    {
        e.HasMorePages = true;
    }

    Logger("Has more pages: " + e.HasMorePages);

    img.SelectActiveFrame(objDimension, lastPagePrinted);

    e.Graphics.DrawImage(img, m);
}

正如您在代码中看到的,我添加了两个记录器。打印三页文档时的结果:

  

尝试打印第0页总页数:3

     

有更多页面:真实

     

尝试打印第1页总页数:3

     

有更多页面:真实

     

尝试打印第2页总页数:3

     

有更多页面:错误

     

尝试打印第3页总页数:3

     

有更多页面:错误

当打印第3页(索引为2的页面)时,它看到没有更多要打印的页面,我们也到达了要在Settings.ToPage上打印的最后一页,但它仍决定调用打印方法一更多的时间当然会失败,因为文档中没有更多的页面 每次打印时都不会发生这种情况。 1页的所有文档都将正确打印,但如果页面超过1页,则有50%的可能会失败

知道为什么吗?

我一直在寻找这个问题,但到目前为止还没有找到任何东西。 我也尝试更改代码,其中不使用PrinterSettings.FromPage和PrinterSettings.ToPage,但有自己的变量来跟踪需要打印的页面,但它没有区别

完整类的代码(与上面的代码相比有一些变化):

public class PrintDocumentAdvanced : PrintDocument
    {
        private string tmp = "tmpPrint.tif";
        private string fileName;

        public PrintDocumentAdvanced(string fileName)
        {
            this.fileName = fileName;
            this.PrintPage += PreparePrint;
        }

        private int firstPage;
        private int lastPage;

        private bool printPrepared = false;

        public bool PreparePrint()
        {
            if (System.IO.File.Exists(fileName))
            {
                FileTools.Converter.ConvertPdfToTiff(fileName, tmp);

                Image img = Image.FromFile(tmp);
                Guid objGuid = img.FrameDimensionsList[0];
                FrameDimension objDimension = new FrameDimension(objGuid);

                // Checks if there are more pages to print

                int cnt = img.GetFrameCount(objDimension);

                if (PrinterSettings.FromPage > PrinterSettings.ToPage)
                {
                    return false;
                }

                if (PrinterSettings.FromPage == 0 && PrinterSettings.ToPage == 0)
                {
                    firstPage = 1;
                    lastPage = cnt;
                    PrinterSettings.FromPage = 0;
                    PrinterSettings.ToPage = 0;
                }
                else
                {
                    firstPage = PrinterSettings.FromPage;
                    lastPage = PrinterSettings.ToPage;
                    PrinterSettings.FromPage = 0;
                    PrinterSettings.ToPage = 0;
                }

                lastPagePrinted = firstPage - 1;

                img.Dispose();
                img = null;

                printPrepared = true;

                return true;
            }
            else
            {
                printPrepared = false;
                return false;
            }
        }

        public void DisposeObject()
        {
            System.IO.File.Delete(tmp);
        }

        private int lastPagePrinted = -1;

        private void PreparePrint(object o, PrintPageEventArgs e)
        {
            if (printPrepared == false)
            {
                throw new Exception("PreparePrint() needs to be called before printing!");
            }

            if (System.IO.File.Exists(fileName))
            {
                // Loads tif file from image

                Image img = Image.FromFile(tmp);

                Guid objGuid = img.FrameDimensionsList[0];

                FrameDimension objDimension = new FrameDimension(objGuid);

                // Checks if there are more pages to print
                Logger("Trying to print page " + lastPagePrinted + " Total pages: " + img.GetFrameCount(objDimension));

                if (img.GetFrameCount(objDimension) > lastPagePrinted + 1 && lastPagePrinted + 1 < lastPage)
                {
                    e.HasMorePages = true;
                }

                Logger("Has more pages: " + e.HasMorePages);

                img.SelectActiveFrame(objDimension, lastPagePrinted);

                Rectangle m = new Rectangle(-25, -25, e.PageBounds.Width /*+ e.PageBounds.X - (e.PageSettings.Margins.Right / 2)*/, e.PageBounds.Height /*+ e.PageBounds.Y - (e.PageSettings.Margins.Bottom / 2)*/);

                if ((double)img.Width / (double)img.Height > (double)m.Width / (double)m.Height) // image is wider
                {
                    m.Height = (int)((double)img.Height / (double)img.Width * (double)m.Width);
                }
                else
                {
                    m.Width = (int)((double)img.Width / (double)img.Height * (double)m.Height);
                }

                // Draws the image to the print object

                e.Graphics.DrawImage(img, m);

                // Disposes the temporary image and delete the file

                img.Dispose();

                lastPagePrinted++;
            }
        }

        public void Logger(String lines)
        {

            // Write the string to a file.append mode is enabled so that the log
            // lines get appended to  test.txt than wiping content and writing the log

            System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\Temp\\mylog.txt", true);
            file.WriteLine(lines);

            file.Close();

        }
    }

额外注意:完整代码实际上会收到一个包含.PDF文件路径的字符串。然后它会将.PDF文件转换为.TIF文件,它将读取并打印

0 个答案:

没有答案