Apache PDFBox Center Text PDF Document Example
The following example demonstrates how to center a text in PDF document using Apache PDFBox.
Maven Dependencies
We use Apache Maven to manage our project dependencies. Make sure the following dependencies reside on the class-path.
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.8</version>
</dependency>
Apache PDFBox Center Text PDF Document
We need to do some calculations in order to calculate the center of the PDF document. Take a look at the following example.
package com.memorynotfound.pdf.pdfbox;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import java.io.File;
import java.io.IOException;
public class CenterText {
public static void main(String[] args) throws Exception{
try {
String title = "Apache PDFBox Center Text PDF Document";
PDFont font = PDType1Font.HELVETICA_BOLD;
int marginTop = 30;
int fontSize = 16;
final PDDocument doc = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
PDRectangle mediaBox = page.getMediaBox();
doc.addPage(page);
PDPageContentStream stream = new PDPageContentStream(doc, page);
float titleWidth = font.getStringWidth(title) / 1000 * fontSize;
float titleHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
float startX = (mediaBox.getWidth() - titleWidth) / 2;
float startY = mediaBox.getHeight() - marginTop - titleHeight;
stream.beginText();
stream.setFont(font, fontSize);
stream.newLineAtOffset(startX, startY);
stream.showText(title);
stream.endText();
stream.close();
doc.save(new File("/tmp/center-text.pdf"));
} catch (IOException e){
System.err.println("Exception while trying to create pdf document - " + e);
}
}
}
Demo
When we run the application, the text is centered on the PDF document.
References
- Apache PdfBox Official Website
- Apache PdfBox API Javadoc
- Apache PdfBox add multiline paragraph
- Apache PdfBox create PDF document