DEV Community

E-iceblue Product Family
E-iceblue Product Family

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 from this 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>
Enter fullscreen mode Exit fullscreen mode

Example 1. Print with network connected printer

import com.spire.ms.Printing.PrinterSettings;
import com.spire.presentation.Presentation;
import com.spire.presentation.PresentationPrintDocument;

public class PrintPowerPointFile {

    public static void main(String[] args) throws Exception {

        //Create a Presentation object
        Presentation presentation = new Presentation();

        //Load the PowerPoint document
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");

        //Create a PresentationPrintDocument object
        PresentationPrintDocument printDocument = new PresentationPrintDocument(presentation);

        //Print the slide with Grayscale
        printDocument.setGrayLevelForPrint(true);

        //Get the printer settings
        PrinterSettings printerSettings = printDocument.getPrinterSettings();

        //Set print range
        printerSettings.setFromPage(1);
        printerSettings.setToPage(4);

        //Set printer name
        printerSettings.setPrinterName("HP ColorLaserJet MFP M278-M281 PCL-6 (V4)");

        //Set print copy
        printerSettings.setCopies((short)1);

        //Call print method to print the selected pages
        printDocument.print();
        presentation.dispose();
    }
}
Enter fullscreen mode Exit fullscreen mode

Example 2. Print with virtual printer

import com.spire.ms.Printing.PrinterSettings;
import com.spire.presentation.Presentation;
import com.spire.presentation.PresentationPrintDocument;

public class PrintToDocument {

    public static void main(String[] args) throws Exception {

        //Create a Presentation object
        Presentation presentation = new Presentation();

        //Load the PowerPoint document
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");

        //Create a PresentationPrintDocument object
        PresentationPrintDocument printDocument = new PresentationPrintDocument(presentation);

        //Get the printer settings
        PrinterSettings printerSettings = printDocument.getPrinterSettings();

        //Set printer name
        printerSettings.setPrinterName("Microsoft Print to PDF");

        printerSettings.setPrintToFile(true);

        printerSettings.setPrintFileName("out/PrintToPdf.pdf");

        printDocument.print();
        presentation.dispose();
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)