Print a PowerPoint Presentation in Java
During the process of manipulating MS PowerPoint documents, there are times when you may need to print the presentations you've created. This article will share the following two ways to print a PowerPoint document programmatically using a free Java library.
Print All Presentation Slides with the Default Printer
Print PowerPoint Document to Virtual Printer
Print Some Slides in a PowerPoint Document
Import Dependency
Before starting, you'll need to install the free library (Free Spire.Presentation for Java) in your Java application. Below are two methods provided.
● Download the free library and unzip it, and then add the Spire.Presentation.jar file to your project as dependency.
● Directly add the jar dependency to your maven project by adding the following configurations to the pom.xml.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
Print All Presentation Slides with the Default Printer
The PresentationPrintDocument.print() method allows you to print all the presentation slides with the default printer. The complete sample code is shown below.
import com.spire.presentation.Presentation;
import com.spire.presentation.PresentationPrintDocument;
public class PrintPPT {
public static void main(String[] args) throws Exception {
//Create a Presentation instance
Presentation presentation = new Presentation();
//Load a sample PowerPoint document
presentation.loadFromFile("Sample.pptx");
//Print all the presentation slides with default printer
PresentationPrintDocument document = new PresentationPrintDocument(presentation);
document.print();
presentation.dispose();
}
}
Print PowerPoint Document to Virtual Printer
Free Spire.Presentation for Java also allows you to print your PowerPoint presentation to a virtual printer (Microsoft XPS Document Writer). The complete sample code is shown below.
import com.spire.presentation.*;
public class printPPTByVirtualPrinter {
public static void main(String[] args) throws Exception{
//Create a Presentation instance
Presentation presentation = new Presentation();
//Load the file from disk
presentation.loadFromFile("Sample.pptx");
//Print PowerPoint document to virtual printer
PresentationPrintDocument document = new PresentationPrintDocument(presentation);
document.getPrinterSettings().setPrinterName("Microsoft XPS Document Writer");
presentation.print(document);
}
}
Print Some Slides in a PowerPoint Document
If you only want to print some specified presentation slides, you can first select some slides (continuous or discontinuous ) from the PowerPoint document using the methods of PresentationPrintDocument class and then print them using Presentation.print(PresentationPrintDocument presentationPrintDocument) method. The complete sample code is shown below.
import com.spire.ms.Printing.PrintRange;
import com.spire.presentation.*;
public class printMultipleSlides {
public static void main(String[] args) throws Exception{
//Create a Presentation instance
Presentation ppt = new Presentation();
//Load the document from disk
ppt.loadFromFile("Sample.pptx");
PresentationPrintDocument document = new PresentationPrintDocument(ppt);
//Set continuous print area
document.getPrinterSettings().setPrintRange(PrintRange.SomePages);
document.getPrinterSettings().setFromPage(1);
document.getPrinterSettings().setToPage(4);
//Set discontinuous print area
//document.selectSlidesForPrint("1", "2-4");
ppt.print(document);
ppt.dispose();
}
}