使用PrintDocument打印多个页面

时间:2014-04-02 09:26:50

标签: c# printing

我在使用以下代码打印多个页面时遇到问题。它始终只打印最后一页。你能帮帮我吗?

using (var rasterizer = new PdfRasterizer(pdfInputPdf))
{
    // Create a JpegImageFormat object.
    var jpegImageFormat = new JpegImageFormat(100);

    // Create a FixedImageSize object with required width and height.
    var imageSize = new PercentageImageSize(400);

    // Save the image.
    var imageData = rasterizer.Draw(jpegImageFormat, imageSize);

    using (var pd = new PrintDocument())
    {
        var margins = new Margins(0, 40, 0, 40);
        pd.DefaultPageSettings.Margins = margins;
        pd.DefaultPageSettings.Color = true;
        pd.DefaultPageSettings.Landscape = false;

        pd.PrintPage += (sender, args) =>
                        {
                            PrintPage(text1, text2, imageData, pd, args);
                        };

        pd.Print();
    }
}

这节课:

   private static int counter = 0;
        private static void PrintPage(string text1, string text2, byte[][] imageData, PrintPageEventArgs args)
        {

            foreach (var b in imageData)
            {
                using (var stream = new MemoryStream(b))
                {
                    var i = Image.FromStream(stream);

                    CreateNotApprovedWatermark(i, text1, text2);

                    if (args.PageSettings.PrinterSettings.CanDuplex)
                    {
                        args.PageSettings.PrinterSettings.Duplex = Duplex.Horizontal;
                    }

                    var m = args.MarginBounds;

                    if (i.Width / (double)i.Height > m.Width / (double)m.Height) // image is wider
                    {
                        m.Height = (int)(i.Height / (double)i.Width * m.Width);
                    }
                    else
                    {
                        m.Width = (int)(i.Width / (double)i.Height * m.Height);
                    }
                    args.Graphics.DrawImage(i, m);
                    if (counter <= 2)
                    {
                        counter++;
                        args.HasMorePages = true;
                    }
                    else
                    {
                        args.HasMorePages = false;
                    }
                }
            }

        }

1 个答案:

答案 0 :(得分:0)

退出一些试验和错误后,我找到了解决问题的简单方法:

  using (var rasterizer = new PdfRasterizer(new Foxit.PDF.Rasterizer.InputPdf(pdfData)))
            {
                // Create a JpegImageFormat object.
                var jpegImageFormat = new JpegImageFormat(100);

                // Create a FixedImageSize object with required width and height.
                var imageSize = new PercentageImageSize(400);

                // Save the image.
                var imagePages = rasterizer.Draw(jpegImageFormat, imageSize);

                using (var pd = new PrintDocument())
                {
                    var margins = new Margins(0, 40, 0, 40);
                    pd.DefaultPageSettings.Margins = margins;
                    pd.DefaultPageSettings.Color = true;
                    pd.DefaultPageSettings.Landscape = false;

                    var pageNumber = 0;

                    pd.PrintPage += (sender, args) =>
                    {
                        PrintPage(pageNumber, text1, text2, imagePages[pageNumber], args);
                        if (pageNumber < imagePages.Count())
                        {
                            pageNumber++;
                            args.HasMorePages = pageNumber != imagePages.Count();
                        }
                    };

                    pd.Print();
                }
            }

问题在于放置HasMorePages属性。