Apache POI DefaultFontReplacer Examples



Unveiling the Magic of Apache POI DefaultFontReplacer: A Comprehensive Guide with Examples


Apache POI, the Java API for Microsoft Documents, is a powerful tool that allows developers to create, modify, and extract information from Word, Excel, and PowerPoint documents. Among its many features, the `DefaultFontReplacer` stands out as a valuable component for customizing font handling in document processing.

In this blog post, we'll explore the `DefaultFontReplacer` in Apache POI and provide hands-on examples to showcase its capabilities.

Understanding DefaultFontReplacer

The `DefaultFontReplacer` in Apache POI is designed to replace fonts in a document with custom fonts. This can be particularly useful when you need to ensure consistent font styles across different systems or when dealing with documents that use fonts not available on the target system.

Getting Started

Before diving into examples, make sure you have Apache POI added to your project's dependencies. You can do this by including the necessary JAR files or using a build tool like Maven or Gradle.

For Maven, add the following dependency:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version> <!-- Replace with the latest version -->
</dependency>

For Gradle:

implementation 'org.apache.poi:poi:4.1.2' // Replace with the latest version

Now, let's move on to some practical examples.

Example 1: Basic Font Replacement


import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFFont;
import org.apache.poi.ooxml.util.DocumentHelper;

public class FontReplacementExample {

    public static void main(String[] args) {
        try {
            // Load the existing document
            XWPFDocument document = new XWPFDocument(DocumentHelper.createDocument());

            // Access a paragraph and run
            XWPFParagraph paragraph = document.createParagraph();
            XWPFRun run = paragraph.createRun();

            // Set the original font
            XWPFFont originalFont = run.getFont();
            originalFont.setFamily("Arial");
            originalFont.setColor("FF0000"); // Red color

            // Create a DefaultFontReplacer instance
            DefaultFontReplacer fontReplacer = new DefaultFontReplacer();

            // Replace the font with a custom font
            fontReplacer.replaceFont(document, "Arial", "Times New Roman");

            // Save the modified document
            document.write(new FileOutputStream("modified_document.docx"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


In this example, we create a simple Word document with one paragraph and one run. The original font is set to Arial with red color. We then use `DefaultFontReplacer` to replace the font with Times New Roman.

Example 2: Batch Font Replacement in a Document


import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.ooxml.util.DocumentHelper;

public class BatchFontReplacementExample {

    public static void main(String[] args) {
        try {
            // Load the existing document
            XWPFDocument document = new XWPFDocument(DocumentHelper.createDocument());

            // Create multiple paragraphs with different fonts
            for (int i = 0; i < 5; i++) {
                XWPFParagraph paragraph = document.createParagraph();
                XWPFRun run = paragraph.createRun();
                run.setText("Paragraph " + (i + 1));
                run.setFontFamily("Calibri");
            }

            // Create a DefaultFontReplacer instance
            DefaultFontReplacer fontReplacer = new DefaultFontReplacer();

            // Replace the font in the entire document
            fontReplacer.replaceFont(document, "Calibri", "Arial");

            // Save the modified document
            document.write(new FileOutputStream("batch_modified_document.docx"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we create a document with multiple paragraphs, each using the Calibri font. The `DefaultFontReplacer` is then used to replace all instances of Calibri with Arial throughout the document.

Conclusion

The `DefaultFontReplacer` in Apache POI provides a powerful mechanism for customizing font handling in Word documents. Whether you need to replace fonts for consistency or address font compatibility issues, this component is a valuable addition to your document processing toolkit.

Feel free to experiment with these examples and adapt them to your specific requirements. As you delve deeper into Apache POI, you'll discover its versatility in handling various aspects of document manipulation. 

Happy coding!



















Post a Comment

Previous Post Next Post