Printing PowerPoint presentations allows you to convert your digital slides into tangible documents that can be shared, distributed, or used for reference. Whether you need handouts for a meeting, materials for a presentation, or physical copies for archival purposes, printing PowerPoint presentations is a versatile way to turn content into physical objects.

In this article, you will learn how to print PowerPoint documents in Java using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<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</artifactId>
        <version>10.11.4</version>
    </dependency>
</dependencies>

Print PowerPoint with the Default Printer in Java

Printing a PowerPoint presentation is a straightforward process that can be done using the default printer with default printer settings on your computer.

To print a PowerPoint file with the default printer, follow these steps.

  • Create a Presentation object.
  • Load a PowerPoint file from a given file path.
  • Create a PresentationPrintDocument object based on the document.
  • Print the document with the default printer using print() method of the PresentationPrintDocument object.
  • Java
import com.spire.presentation.Presentation;
import com.spire.presentation.PresentationPrintDocument;

public class PrintWithDefaultPrinter {

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

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

        // Load a PowerPoint file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pptx");

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

        // Print the document
        printDocument.print();

        // Dispose resources
        presentation.dispose();
        printDocument.dispose();
    }
}

Print PowerPoint with a Specified Printer in Java

If you want to print your PowerPoint presentation using a specific printer rather than the default one, you can easily do so by following these steps.

  • Create a Presentation object.
  • Load a PowerPoint file from a given file path.
  • Create a PrinterSettings object.
  • Specify the printer name using PrinterSettings.print() method.
  • Print the document using Presentation.print() method.
  • Java
import com.spire.presentation.Presentation;
import com.spire.presentation.printing.PrinterSettings;

public class PrintWithSpecifiedPrinter {

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

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

        // Load a PowerPoint file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pptx");

        // Create a PrinterSettings object
        PrinterSettings printerSettings = new PrinterSettings();

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

        // Print the document
        presentation.print(printerSettings);

        // Dispose resources
        presentation.dispose();
    }
}

Print Multiple Slides on One Page in Java

Printing multiple slides on one page is a convenient way to optimize paper usage and create compact handouts or reference materials from your PowerPoint presentation.

The following are the steps to print multiple slides on one page using Java.

  • Create a Presentation object.
  • Load a PowerPoint file from a given file path.
  • Set the slide count per page for printing using Presentation.setSlideCountPerPageForPrint() method.
  • Print the document using Presentation.print() method.
  • Java
import com.spire.presentation.PageSlideCount;
import com.spire.presentation.Presentation;
import com.spire.presentation.printing.Duplex;
import com.spire.presentation.printing.PrinterSettings;

public class PrintMultipleSlidesOnOnePage {

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

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

        // Load a PowerPoint file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pptx");

        // Set slide count per page for print
        presentation.setSlideCountPerPageForPrint(PageSlideCount.Two);

        // Create a PrinterSettings object
        PrinterSettings printerSettings = new PrinterSettings();

        // Print the document
        presentation.print(printerSettings);

        // Dispose resources
        presentation.dispose();
    }
}

Print PowerPoint in Grayscale in Java

By printing in grayscale, you can remove the color elements from your slides and obtain a monochromatic version of your presentation.

The following are the steps to print PowerPoint in grayscale using Java.

  • Create a Presentation object.
  • Load a PowerPoint file from a given file path.
  • Enable grayscale printing using Presentation.setGrayLevelForPrint() method
  • Print the document using Presentation.print() method.
  • Java
import com.spire.presentation.Presentation;
import com.spire.presentation.printing.PrinterSettings;

public class PrintInGrayScale {

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

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

        // Load a PowerPoint file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pptx");

        // Enable grayscale printing mode
        presentation.setGrayLevelForPrint(true);

        // Create a PrinterSettings object
        PrinterSettings printerSettings = new PrinterSettings();

        // Print the document
        presentation.print(printerSettings);

        // Dispose resources
        presentation.dispose();
    }
}

Print PowerPoint on Both Sides of the Paper in Java

Printing PowerPoint slides on both sides of the paper can be a practical and eco-friendly option, as it reduces paper consumption and helps create more compact handouts or documents.

The steps to print a PowerPoint file on both sides of the pager are as follow.

  • Create a Presentation object.
  • Load a PowerPoint file from a given file path.
  • Create a PrinterSettings object.
  • Enable duplex printing mode using PrinterSettings.setDuplex() method.
  • Print the document using Presentation.print() method.
  • Java
import com.spire.presentation.Presentation;
import com.spire.presentation.printing.Duplex;
import com.spire.presentation.printing.PrinterSettings;

public class PrintInDuplexMode {

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

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

        // Load a PowerPoint file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pptx");

        // Create a PrinterSettings object
        PrinterSettings printerSettings = new PrinterSettings();

        // Enable duplex printing mode
        printerSettings.setDuplex(Duplex.Default);

        // Print the document
        presentation.print(printerSettings);

        // Dispose resources
        presentation.dispose();
    }
}

Set Print Range when Printing PowerPoint in Java

By setting the print range in PowerPoint, you have control over which slides are printed, allowing you to customize your printouts according to your specific needs.

To specify a range of slides to be printed, follow the steps below.

  • Create a Presentation object.
  • Load a PowerPoint file from a given file path.
  • Create a PrinterSettings object.
  • Specify a range of slides to be printed using PrinterSettings.setFromPage() and PrinterSettings.setToPage() methods.
  • Print the document using Presentation.print() method.
  • Java
import com.spire.presentation.Presentation;
import com.spire.presentation.printing.PrintRange;
import com.spire.presentation.printing.PrinterSettings;

public class SetPrintRange {

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

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

        // Load a PowerPoint file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pptx");

        // Create a PrinterSettings object
        PrinterSettings printerSettings = new PrinterSettings();

        // Specify a range of slides to be printed
        printerSettings.setPrintRange(PrintRange.SomePages);
        printerSettings.setFromPage(1);
        printerSettings.setToPage(4);

        // Print the document
        presentation.print(printerSettings);

        // Dispose resources
        presentation.dispose();
    }
}

Set Copies when Printing PowerPoint in Java

By setting the number of copies, you can easily specify how many duplicates of your slides you want to print.

To specify the number of copies to be printed, follow these steps.

  • Create a Presentation object.
  • Load a PowerPoint file from a given file path.
  • Create a PrinterSettings object.
  • Specify the number of copies to be printed using PrinterSettings.setCopies() method.
  • Print the document using Presentation.print() method.
  • Java
import com.spire.presentation.Presentation;
import com.spire.presentation.printing.PrinterSettings;

public class SetCopies {

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

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

        // Load a PowerPoint file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Test.pptx");

        // Create a PrinterSettings object
        PrinterSettings printerSettings = new PrinterSettings();

        // Specify the number of copies to be printed
        printerSettings.setCopies((short)2);

        // Print the document
        presentation.print(printerSettings);

        // Dispose resources
        presentation.dispose();
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Java: Add Page Number to Word Documents

2023-06-26 07:54:00 Written by Koohji

Page numbers in Word documents are marked on each page to indicate the order and the number of pages. They can facilitate document creators to manage the document content and help users quickly find specific content in the document, thus improving reading speed and reading experience. This article is going to show how to use Spire.Doc for Java to add page numbers to Word documents programmatically.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<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.doc</artifactId>
        <version>13.11.2</version>
    </dependency>
</dependencies>

Add Page Numbers to a Word Document in Java

Page numbers in Word are displayed using specific types of fields. For example, the Page field displays the page number of the current page, the NumPages field displays the total number of pages in a document, and the SectionPages field displays the total number of pages in a section.

Spire.Doc for Java provides the Paragraph.appendField(String fieldName, FieldType fieldType) method to add various types of fields to a Word document, including the Page field (FieldType.Field_Page), the NumPages field (FieldType.Field_Num_Pages), and the SectionPages field (FieldType.Field_Section_Pages).

The following steps explain how to add a Page field and a NumPages field to the footer of a Word document to display the current page number and the total number of pages in the document using Spire.Doc for Java:

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Get the first section using Document.getSections().get() method.
  • Get the footer of the first section using Section.getHeadersFooters().getFooter() method.
  • Add a paragraph to the footer, and then add a Page field and a NumPages field to the paragraph using Paragraph.appendField(String fieldName, FieldType fieldType) method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;

public class addPageNumbersWholeDocument {
    public static void main(String[] args) {

        //Create an object of Document class
        Document document = new Document();

        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Get the first section
        Section section = document.getSections().get(0);

        //Get the footer of the section
        HeaderFooter footer = section.getHeadersFooters().getFooter();

        //Add Page and NumPages fields to the footer and set the format
        Paragraph footerParagraph = footer.addParagraph();
        footerParagraph.appendField("page number", FieldType.Field_Page);
        footerParagraph.appendText(" / ");
        footerParagraph.appendField("page count", FieldType.Field_Num_Pages);
        footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        footerParagraph.getStyle().getCharacterFormat().setFontSize(16);

        //Save the document
        document.saveToFile("PageNumberWholeDocument.docx");
        document.dispose();
    }
}

Java: Add Page Number to Word Documents

Restart Page Numbering for Each Section in a Word Document in Java

Restarting page numbering allows you to start the page numbers at a particular number in each section, rather than continuing from the page numbers of the previous section.

To restart page numbering for each section of a Word document, you need to loop through all sections in the document and add a Page field and a SectionPages field to each section. Then use the Section.getPageSetup().setRestartPageNumbering(true) method to enable restarting page numbering and the Section.getPageSetup().setPageStartingNumber(int value) method to set the starting page number for each section. The detailed steps are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Loop through the sections in the document.
  • Call the Paragraph.appendField(String fieldName, FieldType fieldType) method to add a Page field and a SectionPages field to each section.
  • Call the Section.getPageSetup().setRestartPageNumbering(true) method to enable restarting page numbering and the Section.getPageSetup().setPageStartingNumber(int value) method to set the starting page number for each section.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.HeaderFooter;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;

public class addPageNumbersEachSection {
    public static void main(String[] args) {

        //Create an object of Document class
        Document document = new Document();

        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Get the count of sections in the document
        int s = document.getSections().getCount();

        //Loop through the sections in the document
        for (int i = 0; i < s; i++) {

            //Add Page and SectionPages fields to each section
            HeaderFooter footer = document.getSections().get(i).getHeadersFooters().getFooter();
            Paragraph footerParagraph = footer.addParagraph();
            footerParagraph.appendField("page number", FieldType.Field_Page);
            footerParagraph.appendText(" / ");
            footerParagraph.appendField("section page count", FieldType.Field_Section_Pages);
            footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            footerParagraph.getStyle().getCharacterFormat().setFontSize(16);

            //Restart page numbering for each section
            if (i == s-1)
                break;
            else {
                document.getSections().get(i + 1).getPageSetup().setRestartPageNumbering(true);
                document.getSections().get(i + 1).getPageSetup().setPageStartingNumber(1);
            }
        }

        //Save the document
        document.saveToFile("PageNumbersSections.docx");
        document.dispose();
    }
}

Java: Add Page Number to Word Documents

Add Page Numbers to a Specific Section in a Word Document in Java

By default, when you insert page numbers into the footer of a section, the subsequent sections will automatically link to the previous section to continue displaying the page numbers. If you want to add page numbers to only a specific section, you will need to unlink the subsequent sections from the previous section, and then delete the content of the footers in the subsequent sections. The detailed steps are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Get the second section of the document using Document.getSections().get() method.
  • Call the Section.getPageSetup().setRestartPageNumbering(true) method to enable restarting page numbering and the Section.getPageSetup().setPageStartingNumber(int value) method to set the starting page number for the section.
  • Call the Paragraph.appendField(String fieldName, FieldType fieldType) method to add a Page field and a SectionPages field to the section.
  • Unlink the subsequent section from the second section using the Section.getHeadersFooters().getFooter().setLinkToPrevious(false) method.
  • Delete the content of the footers in the subsequent sections.
  • Save the document using Doucment.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;

public class addPageNumbersToSpecificSection {
    public static void main(String[] args) {

        //Create an object of Document
        Document document = new Document();

        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Get the second section
        Section section = document.getSections().get(1);

        //Get the footer of the second section
        HeaderFooter footer = section.getHeadersFooters().getFooter();

        //Set the start page as the first page of the second section and the starting number as 1
        section.getPageSetup().setRestartPageNumbering(true);
        section.getPageSetup().setPageStartingNumber(1);

        //Add page numbers to the footer and set the format
        Paragraph footerParagraph = footer.addParagraph();
        footerParagraph.appendField("Page number", FieldType.Field_Page);
        footerParagraph.appendText(" / ");
        footerParagraph.appendField("Number of pages", FieldType.Field_Section_Pages);
        footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        footerParagraph.getStyle().getCharacterFormat().setFontSize(12);

        //Unlink the subsequent section from the second section
        document.getSections().get(2).getHeadersFooters().getFooter().setLinkToPrevious(false);
        //Delete the content of the footers in the subsequent sections
        for (int i = 2; i < document.getSections().getCount(); i++)
        {
            document.getSections().get(i).getHeadersFooters().getFooter().getChildObjects().clear();
            document.getSections().get(i).getHeadersFooters().getFooter().addParagraph();
        }

        //Save the document
        document.saveToFile("PageNumberOneSection.docx");
        document.dispose();
    }
}

Java: Add Page Number to Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Get Bookmark Text in Java

2019-08-08 08:48:58 Written by Koohji

This article demonstrates how to get the text inside a bookmark in a Word document using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.documents.BookmarksNavigator;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextBodyPart;
import com.spire.doc.fields.TextRange;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class GetBookmarkText {

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

        //create a Document object
        Document doc = new Document();

        //load a sample Word file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

        //get the specific bookmark
        BookmarksNavigator navigator = new BookmarksNavigator(doc);
        navigator.moveToBookmark("MyBookmark");

        //get the bookmark content
        TextBodyPart textBodyPart = navigator.getBookmarkContent();

        //declare a String variable
        String text = "";

        //loop through body items
        for (Object item : textBodyPart.getBodyItems()) {

            //determine if an item is a paragraph
            if (item instanceof Paragraph) {
                Paragraph paragraph = (Paragraph) item;

                //loop through the child objects of the paragraph
                for (Object childObj : paragraph.getChildObjects()) {

                    //determine if a child object is a text range
                    if (childObj instanceof TextRange) {

                        //get text from the text range
                        TextRange textRange = (TextRange) childObj;
                        text = text + textRange.getText();
                    }
                }
            }
        }

        //write the bookmark text to a .txt file
        PrintWriter printWriter = new PrintWriter("output/BookmarkText.txt");
        printWriter.println(text);
        printWriter.close();
    }
}

Get Bookmark Text in Java

page 62