I’m trying to print a PDF file with a PdfViewer object in a .NET 8 application, using Aspose.PDF 25.2.0. It basically works, although I’m having issues with trying to print the file while keeping the original document page size and orientation.
I first tried using PrintDocumentWithSettings method with only a PrinterSettings parameter. According to your documentation (PdfViewer.PrintDocumentWithSettings | Aspose.PDF for .NET API Reference), the output page size should fit the the document first page size. However, what I observed is that it is using the printer default settings instead. My code was basically looking like this :
PdfViewer viewer = new();
viewer.BindPdf("TestFile.pdf");
viewer.AutoResize = true;
viewer.PrintPageDialog = false;
var ps = new Aspose.Pdf.Printing.PrinterSettings()
{
PrinterName = "Microsoft Print to PDF"
};
viewer.PrintDocumentWithSettings(ps);
viewer.Close();
Since that didn’t work as expected, I’m now trying to get the original document settings and applying them to the PrintDocumentWithSettings method via a PageSettings object and its PaperSize property. This is turning out to be far more difficult to do than I expected.
To get the original page size, I open the file in a Document object, and use GetPageRect on the first page of a document, since it’s apparently the way to do this. I can also get it with the GetPageWidth and GetPageHeight of a PdfFileInfo object, which seems to basically amount to the same thing in the end.
The main problem with these methods is that I’m getting strange results. For one thing, in the documention there is no mention of what the results should be. I would expect to get sizes in inches, or maybe in millimeters, or in related units. Instead, I’m getting a number I don’t understand the real meaning of. For example, for a document with a page size of 8,5 X 11 inches, I get 612 X 792. A 8,5 X 14 document gives me a size of 612 X 1008. So, basically 72 per inch, is that it? I don’t know what those numbers are supposed to mean.
The annoying thing is that I can’t simply put those numbers in the PaperSize object, since it’s expecting hundredths of an inch instead of whatever those numbers actually are. So, I’m supposed to convert them? By doing something like Size / 72 * 100? I don’t like coding something bizarre like that when I don’t fully understand the meaning of it.
There is also the fact that there is no apparant Landscape property in a PDF file, while there is one in the PageSettings object that absolutely must be used to print in a landscape orientation, meaning that I must verify the original size and flip the numbers if the width is larger than the height as well as using the Landscape property. Otherwise, if I just input a larger width than height in the PaperSize object, I get undesirable results (it seems to use the printer default settings in these cases). It would have been nice if the PageSettings could simply determine by itself the orientation.
I’m ending up with something like this :
PdfViewer viewer = new();
var document = new Document("TestFile.pdf");
viewer.BindPdf(document);
viewer.AutoResize = true;
viewer.PrintPageDialog = false;
var ps = new Aspose.Pdf.Printing.PrinterSettings()
{
PrinterName = "Microsoft Print to PDF"
};
var pageSize = document.Pages.First().GetPageRect(true);
var pageHeight = (int)((pageSize.Height > pageSize.Width ? pageSize.Height : pageSize.Width) / 72 * 100);
var pageWidth = (int)((pageSize.Height > pageSize.Width ? pageSize.Width : pageSize.Height) / 72 * 100);
var pgs = new Aspose.Pdf.Printing.PageSettings()
{
PaperSize = new Aspose.Pdf.Printing.PaperSize("", pageWidth, pageHeight),
Margins = new Aspose.Pdf.Devices.Margins(0, 0, 0, 0),
Landscape = pageSize.Width > pageSize.Height
};
viewer.PrintDocumentWithSettings(pgs, ps);
viewer.Close();
In short, how can I get the PdfViewer to print the file using its original settings? Do I really need to do all that just to get it to print the file as is? At the vey least, why must I convert the size with this nonsensical formula? I have a feeling I’m not doing this the right way.