Posted on
Print PowerPoint Documents in Java Applications
In this article, we’re going introduce how to send a PowerPoint document to a network connected printer or a virtual printer by using Spire.Prensetaion for Java. Spire provides the PresentationPrintDocument class to control how a PowerPoint presentation is printed, for example, print in grayscale, set the print range and copies. The following code snippets demonstrates the same.
Installing Spire.Presentation.jar
If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file fromthis link and add it as a dependency in your application.
<repositories><repository><id>com.e-iceblue</id><name>e-iceblue</name><url>http://repo.e-iceblue.com/nexus/content/groups/public/</url></repository></repositories><dependencies><dependency><groupId>e-iceblue</groupId><artifactId>spire.presentation</artifactId><version>4.1.6</version></dependency></dependencies>
Example 1. Print with network connected printer
importcom.spire.ms.Printing.PrinterSettings;importcom.spire.presentation.Presentation;importcom.spire.presentation.PresentationPrintDocument;publicclassPrintPowerPointFile{publicstaticvoidmain(String[]args)throwsException{//Create a Presentation objectPresentationpresentation=newPresentation();//Load the PowerPoint documentpresentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");//Create a PresentationPrintDocument objectPresentationPrintDocumentprintDocument=newPresentationPrintDocument(presentation);//Print the slide with GrayscaleprintDocument.setGrayLevelForPrint(true);//Get the printer settingsPrinterSettingsprinterSettings=printDocument.getPrinterSettings();//Set print rangeprinterSettings.setFromPage(1);printerSettings.setToPage(4);//Set printer nameprinterSettings.setPrinterName("HP ColorLaserJet MFP M278-M281 PCL-6 (V4)");//Set print copyprinterSettings.setCopies((short)1);//Call print method to print the selected pagesprintDocument.print();presentation.dispose();}}
Example 2. Print with virtual printer
importcom.spire.ms.Printing.PrinterSettings;importcom.spire.presentation.Presentation;importcom.spire.presentation.PresentationPrintDocument;publicclassPrintToDocument{publicstaticvoidmain(String[]args)throwsException{//Create a Presentation objectPresentationpresentation=newPresentation();//Load the PowerPoint documentpresentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");//Create a PresentationPrintDocument objectPresentationPrintDocumentprintDocument=newPresentationPrintDocument(presentation);//Get the printer settingsPrinterSettingsprinterSettings=printDocument.getPrinterSettings();//Set printer nameprinterSettings.setPrinterName("Microsoft Print to PDF");printerSettings.setPrintToFile(true);printerSettings.setPrintFileName("out/PrintToPdf.pdf");printDocument.print();presentation.dispose();}}
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse