page 147

Insert HTML String in PowerPoint in Java

2019-08-27 06:07:39 Written by Koohji

This article demonstrates how to render text with simple HTML tags to formatted text in a presentation slide by using Spire.Presentation for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.IAutoShape;
import com.spire.presentation.Presentation;
import com.spire.presentation.ShapeType;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

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

        //create a Presentation object
        Presentation ppt = new Presentation();
        
        //add a shape to the first slide
        IAutoShape shape = ppt.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(50, 50, 400, 100));
        shape.getFill().setFillType(FillFormatType.NONE);

        //clear the default paragraph
        shape.getTextFrame().getParagraphs().clear();

        //define html string
        String htmlString = "<ul>" +
                            "<li style=\"color:blue\">Spire.Presentation for Java</li>" +
                            "<li style=\"color:green\">Spire.PDF for Java</li>" +
                            "<li style=\"color:gray\">Spire.Doc for Java</li>" +
                            "<li style=\"color:red\">Spire.Barcode for Java</li>" +
                            "</ul>";

        //insert html string in the shape
        shape.getTextFrame().getParagraphs().addFromHtml(htmlString);

        //save to file
        ppt.saveToFile("output/InsertHtml.pptx", FileFormat.PPTX_2013);
    }
}

Insert HTML String in PowerPoint in Java

Java: Create a Table of Contents in Word

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

The table of contents is a field in a Word document that displays the headings of all levels and their corresponding page numbers in the document. Moreover, the table of contents can be updated after changes are made to the content of the document, and new headings and page numbers can be generated according to the new document content. Therefore, the table of contents can be a great convenience both when editing documents and reading them, while also making them more professional. This article is going to show how to insert the table of contents into Word documents through Java programs using Spire.Doc for Java.

Install Spire.Doc for Java

First, 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>14.1.3</version>
    </dependency>
</dependencies>

Create a Table of Contents with the Default Format

Before creating a table of contents, it is necessary to set the level for each heading, so that the table of contents can be generated according to each heading and its level. The level of a heading can be set by using the Paragraph.getListFormat().setListLevelNumber(int) method.

Spire.Doc for Java provides the Paragraph.appendTOC() method to insert a table of contents into a paragraph. The detailed steps for inserting a table of contents with the default style in a Word document are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Add a section to the document using Document.addSection() method and insert it into the document after the cover section using Document.getSections().insert() method.
  • Add a paragraph to the section using Section.addParagraph() method.
  • Create a table of contents in the paragraph using Paragraph.appendTOC() method.
  • Update the table of contents to display the headings in the document and their page numbers using Document.updateTableOfContents() method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TableOfContent;

public class insertTableOfContents {
    public static void main(String[] args) {
        //Create an object of Document class
        Document doc = new Document();

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

        //Add a section and insert it into the document after the cover section
        Section section = doc.addSection();
        doc.getSections().insert(1, section);

        //Add a paragraph to the section
        Paragraph paragraph = section.addParagraph();
        paragraph.appendText("Table of Contents\r\n");
        paragraph.getStyle().getCharacterFormat().setFontSize(14f);

        //Create a table of contents in the paragraph
        paragraph.appendTOC(2, 3);

        //Update the table of contents
        doc.updateTableOfContents();

        //Save the document
        doc.saveToFile("CreateTableOfContents.docx");
    }
}

Java: Create a Table of Contents in Word

Create a Table of Contents with Customized Format

In Spire.Doc for Java, the TableOfContent class represents a table of contents of a Word document.  The parts displayed in the table of contents can be customized by the switches while initializing an instance of the TableOfContent class. For example, the switches, "{\\o \"1-3\" \\n 1-2}", mean displaying the first to third level headings while omitting the page numbers of the first and second level headings.

The detailed steps for customizing a table of contents in a Word document are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Insert a section after the cover section using Document.getSections().insert() method and add a paragraph to the section using Section.addParagraph() method.
  • Create an instance of TableOfContent class to create a table of contents that displays the first to third level headings and omits page numbers of the first and second level headings.
  • Insert the table of contents to the paragraph using Paragraph.getItems().add() method.
  • Insert a field separator mark and a field end mark to the paragraph using Paragraph.appendFieldMark() method to end the table of contents field.
  • Set the instance as the table of contents of the document using Document.setTOC() method.
  • Update the table of contents to display the current contents for the document using Document.updateTableOfContents() method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.documents.FieldMarkType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TableOfContent;

public class insertTableOfContentsWithCustomizedStyle {
    public static void main(String[] args) {
        //Create an object of Document class
        Document doc = new Document();

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

        //Insert a section after the first section and add a paragraph in the section
        Section section = doc.addSection();
        doc.getSections().insert(1, section);
        Paragraph paragraph = section.addParagraph();
        paragraph.getStyle().getCharacterFormat().setFontSize(14f);

        //Initialize an instance of TableOfContent class
        TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\n 1-2}");

        //Insert the table of contents to the document
        paragraph.getItems().add(toc);

        //Insert field marks to end the field
        paragraph.appendFieldMark(FieldMarkType.Field_Separator);
        paragraph.appendFieldMark(FieldMarkType.Field_End);

        //Set the created table of contents as the table of contents of the document
        doc.setTOC(toc);

        //Update the table of contents
        doc.updateTableOfContents();

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

Java: Create a Table of Contents in Word

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.

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>11.1.1</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.

page 147