I am trying to upgrade from the old PDF Kit library to the new PDF library (version 11.8.0). The new PdfViewer class does not implement the java.awt.print.Printable interface. How can I print a PDF document using the standard libraries? In my case the user has already selected the printer to use so I do not want to print to the default printer or to have a system print dialog appear. Do you have an example of how to achieve this?
Hi there,
Thanks for your inquiry. Please check following sample code to print PDF document to specific printer without invoking print dialogue. Hopefully it will help you to accomplish the task.
Document document = new Document(new FileInputStream(myDir + "HelloWorld.pdf"));
PdfViewer viewer = new PdfViewer(document);
viewer.setAutoResize(true);
viewer.setAutoRotate(true);
viewer.setPrintPageDialog(false);
PdfPrinterSettings settings = new PdfPrinterSettings();
settings.setPrinterName("Microsoft XPS Document Writer");
viewer.printDocumentWithSettings(settings);
Please feel free to contact us for any further assistance.
Best Regards,
Hi Philip,
Thanks for using our API’s.
All the classes and enumerations of legacy Aspose.Pdf.Kit for Java are merged to com.aspose.pdf.facades package of Aspose.Pdf for Java API. In order to accomplish your requirements, please try using following code snippet.
For more information, please visit Working with PDF printing (facades)
[Java]
// Create PdfViewer object
com.aspose.pdf.facades.PdfViewer viewer = **new** com.aspose.pdf.facades.PdfViewer();
// Open input PDF file
viewer.bindPdf("c:/pdftest/IsWordWrapped.pdf");
// Set attributes for printing
viewer.setAutoResize(**true**); // Print the file with adjusted size
viewer.setAutoRotate(**true**); // Print the file with adjusted rotation
viewer.setPrintPageDialog(**false**); // Do not produce the page number dialog when printing
// gets a printjob object.
java.awt.print.PrinterJob printJob = java.awt.print.PrinterJob.*getPrinterJob*();
// gets the default page.
java.awt.print.PageFormat pf = printJob.defaultPage();
// print PDF document
viewer.printDocument();
// close the Pdf file.
viewer.closePdfFile();