It took several hours, but I've finally been able to reproduce the problem. It seems there is an issue if the PdfDocument is saved to a stream prior to printing. In my particular case I need to save it to the stream prior to printing because I save it to a BLOB in our database.
If you comment out the .SaveToStream(ms) below the code works fine. With the .SaveToStream(ms) it prints a blank page but the output.pdf looks fine.
I've uploaded the logo I'm using, but I don't think the particular logo makes any difference. In my testing with different logos they all experience the same problem.
- Code: Select all
PdfDocument pdfDoc = new PdfDocument();
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(0.25f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
margin.Bottom = unitCvtr.ConvertUnits(0.1f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
margin.Left = unitCvtr.ConvertUnits(0.1f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
PdfPageBase page = pdfDoc.Pages.Add(PdfPageSize.Letter, margin);
PdfImage labelLogo = PdfImage.FromFile("logo.png");
page.Canvas.DrawImage(labelLogo, new PointF(indent, 0));
MemoryStream ms = new MemoryStream();
pdfDoc.SaveToStream(ms);
pdfDoc.PrintFromPage = 1;
pdfDoc.PrintToPage = pdfDoc.Pages.Count;
pdfDoc.PrintDocument.DocumentName = "ThisIsATest";
pdfDoc.PrintDocument.Print();
pdfDoc.SaveToFile("output.pdf");
Login to view the files attached to this post.