How to set the font in ApachePDF Box version 3.0.2

3 min read 23-09-2024
How to set the font in ApachePDF Box version 3.0.2


Apache PDFBox is a powerful open-source Java library that enables the creation and manipulation of PDF documents. One of the most common tasks when working with PDF documents is setting the font for text. In this article, we will explore how to set the font in Apache PDFBox version 3.0.2, along with practical examples to help you get started.

Problem Scenario

Many developers face the challenge of changing fonts in PDF documents while using the Apache PDFBox library. Here is a simple code snippet that demonstrates how to set the font in a PDF using Apache PDFBox:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class SetFontExample {
    public static void main(String[] args) {
        try (PDDocument document = new PDDocument()) {
            PDPage page = new PDPage();
            document.addPage(page);
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            
            // Set the font and write text to the PDF
            contentStream.setFont(PDType1Font.HELVETICA, 12);
            contentStream.beginText();
            contentStream.newLineAtOffset(100, 700);
            contentStream.showText("Hello, World!");
            contentStream.endText();
            contentStream.close();
            
            document.save("HelloWorld.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Understanding the Code

In the example above, we start by importing the necessary classes from the Apache PDFBox library. We then create a new PDF document and add a page to it.

Next, we create a PDPageContentStream, which is used to add content to the page.

  1. Setting the Font: The line contentStream.setFont(PDType1Font.HELVETICA, 12); sets the font to Helvetica with a size of 12 points.
  2. Writing Text: We begin a text block using contentStream.beginText();, set the text position with newLineAtOffset(), and then add the text with showText("Hello, World!");.
  3. Closing the Text Block: After writing the text, we close the text block and the content stream.

Finally, we save the document to a file named "HelloWorld.pdf".

Practical Example: Setting Custom Fonts

Besides the standard fonts provided by PDFBox, you may need to set custom fonts. To use custom TrueType fonts, the code would look like this:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import java.io.File;
import java.io.IOException;

public class CustomFontExample {
    public static void main(String[] args) {
        try (PDDocument document = new PDDocument()) {
            PDPage page = new PDPage();
            document.addPage(page);
            PDPageContentStream contentStream = new PDPageContentStream(document, page);

            // Load a custom TrueType font
            PDTrueTypeFont customFont = PDTrueTypeFont.load(document, new File("path/to/custom-font.ttf"));

            // Set the custom font
            contentStream.setFont(customFont, 14);
            contentStream.beginText();
            contentStream.newLineAtOffset(100, 700);
            contentStream.showText("Using a Custom Font!");
            contentStream.endText();
            contentStream.close();

            document.save("CustomFontExample.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Key Takeaways

  • Standard Fonts: Apache PDFBox comes with a set of predefined fonts like Helvetica, Times-Roman, and Courier. You can use these easily without requiring external files.
  • Custom Fonts: To use custom fonts, you must have the font file in TTF format. Make sure to specify the correct file path when loading the font.
  • Text Positioning: Remember that PDF coordinates start from the bottom-left corner of the page, so positioning your text might require some adjustments.

Conclusion

Setting the font in Apache PDFBox version 3.0.2 is straightforward, whether you're using standard or custom fonts. This functionality allows you to create dynamic and visually appealing PDF documents tailored to your needs. If you need more advanced features, consider exploring the official Apache PDFBox Documentation for further insights.

Additional Resources

By understanding how to set fonts in PDFBox, you can take full advantage of this library's capabilities and enhance your PDF generation process. Happy coding!