How to print pdf as specified custom paper size?

Hi, Support:

I want to print any pdf file with custom paper size,but it throws exception, therefore , all the print jobs fail. my code is as follow:

Dim printersetting As System.Drawing.Printing.PrinterSettings = New System.Drawing.Printing.PrinterSettings()
printersetting.Copies = Copies 'Copies=1
printersetting.PrinterName = PrinterName 'PrinterName=“HP1106”

Dim pagesetting As System.Drawing.Printing.PageSettings = New System.Drawing.Printing.PageSettings(printersetting)
Dim viewer As PdfViewer = New PdfViewer()
viewer.BindPdf(InputPdf)
viewer.AutoResize = True
viewer.AutoRotate = True
pagesetting.PaperSize.Height = PrintHeight
'PrintHeight=428, the code throws an exception saying “pagesetting.PaperSize.Height is readonly except for the pagesetting.PaperSize.Kind = PaperKind.Custom”, however, when setting pagesetting.PaperSize.Kind = PaperKind.Custom, it throws an exception saying “pagesetting.PaperSize.Kind is readonly”
pagesetting.PaperSize.Width = PrintWidth 'PrintWidth=284
viewer.PrintDocumentWithSettings(pagesetting, printersetting)

How to print pdf file with custom paper size?

Thanks for your help!

@ducaisoft

To print a PDF file with a custom paper size using Aspose.PDF, you need to ensure that you are correctly setting the PaperSize property of the PageSettings object. The exception you are encountering indicates that the PaperSize properties are read-only unless the PaperKind is set to Custom.

Here’s how you can set a custom paper size correctly:

  1. Create a new instance of PaperSize with your desired dimensions.
  2. Set the PaperSize.Kind to PaperKind.Custom before setting the width and height.
  3. Use the PrintDocumentWithSettings method to print the document with the specified settings.

Here’s an example of how to implement this in your code:

Dim printerSettings As System.Drawing.Printing.PrinterSettings = New System.Drawing.Printing.PrinterSettings()
printerSettings.Copies = 1  ' Set the number of copies
printerSettings.PrinterName = "HP1106"  ' Specify your printer name

Dim pageSettings As System.Drawing.Printing.PageSettings = New System.Drawing.Printing.PageSettings(printerSettings)
Dim viewer As PdfViewer = New PdfViewer()
viewer.BindPdf("InputPdf.pdf")  ' Bind your PDF file
viewer.AutoResize = True
viewer.AutoRotate = True

' Create a custom paper size
Dim customPaperSize As New System.Drawing.Printing.PaperSize("CustomSize", PrintWidth, PrintHeight)
pageSettings.PaperSize = customPaperSize  ' Set the custom paper size

' Print the document with the specified settings
viewer.PrintDocumentWithSettings(pageSettings, printerSettings)

Make sure to replace PrintWidth and PrintHeight with the actual dimensions you want to use for your custom paper size. This should resolve the exceptions you are facing and allow you to print the PDF with the specified custom paper size.

If you continue to experience issues, please check that your printer supports the custom paper size you are trying to set, as some printers may have limitations on the sizes they can handle.

For further details, you can refer to the official documentation on printing with Aspose.PDF, which provides additional insights into handling various printing scenarios.

Sources:
[1]: Set Choose paper source by PDF page size
[2]: PrintPaperSize | Aspose.PDF for Java API Reference