Issues with printing a PDF file with the original document page settings

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.

1 Like

@Dantre1979

To print a PDF file using the original document page settings in your .NET 8 application with Aspose.PDF, you can follow these steps to ensure that the page size and orientation are correctly set.

Step-by-Step Guide

  1. Bind the PDF Document: Use the PdfViewer class to bind your PDF file.
  2. Retrieve Page Size: Get the dimensions of the first page using the GetPageRect method. This will return the size in points, where 1 point is 1/72 of an inch.
  3. Convert Page Size: Since the PaperSize property expects dimensions in hundredths of an inch, you will need to convert the dimensions from points to hundredths of an inch using the formula:
    [
    \text{Size in hundredths of an inch} = \left(\frac{\text{Size in points}}{72}\right) \times 100
    ]
  4. Set Page Orientation: Determine if the page is in landscape or portrait orientation by comparing the width and height.
  5. Print Document: Use the PrintDocumentWithSettings method with the configured PrinterSettings and PageSettings.

Example Code

Here’s how you can implement this:

using Aspose.Pdf;
using Aspose.Pdf.Facades;
using Aspose.Pdf.Printing;

public class PdfPrinter
{
    public void PrintPdf(string filePath)
    {
        // Create PdfViewer object
        PdfViewer viewer = new PdfViewer();
        viewer.BindPdf(filePath);
        viewer.AutoResize = true;
        viewer.PrintPageDialog = false;

        // Create PrinterSettings
        PrinterSettings ps = new PrinterSettings()
        {
            PrinterName = "Microsoft Print to PDF"
        };

        // Get the original page size
        var document = new Document(filePath);
        var pageSize = document.Pages[1].GetPageRect(true); // Get size of the first page

        // Convert points to hundredths of an inch
        int pageWidth = (int)((pageSize.Width / 72) * 100);
        int pageHeight = (int)((pageSize.Height / 72) * 100);

        // Set PageSettings
        PageSettings pgs = new PageSettings()
        {
            PaperSize = new PaperSize("", pageWidth, pageHeight),
            Margins = new Devices.Margins(0, 0, 0, 0),
            Landscape = pageSize.Width > pageSize.Height // Set landscape if width > height
        };

        // Print the document
        viewer.PrintDocumentWithSettings(pgs, ps);
        viewer.Close();
    }
}

Explanation of the Code

  • The PdfViewer is initialized and the PDF is bound to it.
  • The page size is retrieved and converted from points to hundredths of an inch.
  • The PageSettings object is configured with the calculated dimensions and orientation.
  • Finally, the document is printed using the specified settings.

Additional Notes

  • Ensure that you have the correct printer name set in the PrinterSettings.
  • The conversion from points to hundredths of an inch is necessary because the PaperSize class requires dimensions in that format.
  • If you encounter any issues with the printer settings or dimensions, double-check the values being passed to ensure they are correct.

This approach should help you print the PDF file while maintaining its original settings without any unexpected behavior. If you have further questions or need additional assistance, feel free to ask!

Basically, the AI reply was saying that what I did was essentially correct. It made a mistake, though : with a landscape document, height and width must be swapped otherwise it won’t work as expected. If the PaperSize gets a width that is larger than the height, as it would be the case with a landscape PDF, it tend to use the printer default page size instead.

The one bit of information that was really useful to me was the fact that GetPageRect return the size in points. Knowing the actual meaning of what this method is returning make me more confident that my conversion formula makes sense. This info really should be specified in the documentation for GetPageRect.

I have one question : is the PrintDocumentWithSettings(PrinterSettings) method really supposed to print with the document first page size? This is what it’s supposed to do according to the documentation for this method, but I observed that it’s actually printing with the printer default settings instead, so the documentation seems to be wrong.

1 Like

@Dantre1979

We need to perform some investigation against this scenario and for that we have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-59453

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like