Click here to Skip to main content
15,917,645 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have an issue regarding printing multiple pages using the code below. It always prints only the last page. Can you guys please help me?

C#
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 imageDataPage1 = 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, imageDataPage1, args);

                   pd.Print();
               }
           }


C#
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;
                   }
               }
           }

       }
Posted
Comments
Richard MacCutchan 4-Apr-14 9:38am    
You need to use your debugger to step through the code and see what happens for the first two pages.

1 solution

I have came across the same situation and unfortunatly not found solutions, after so much struggle I have solved issue.
Following link will sure help you to solve your trouble.

http://technet.weblineindia.com/web/print-multiple-different-formats-of-files-in-one-click-in-asp-net/[^]

Please post your feedback if it's helps you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900