This is a more advanced example of using the
Apache PDFBox library. It demonstrates how to add some effects (called
annotations in PDF terms) to text, namely highlighting, underlining, squiggly underlining and strikethrough. It's a bit tricky because these can't be applied to text as it is added to the document, but must be added later after a page is otherwise complete.
If you're new to PDFBox, start with the PdfBox example rather than this one.
For highlighting, also see https://gist.github.com/joelkuiper/331a399961941989fec8
import java.io.*;
import java.util.List;
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.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;
public class SimpleAnnotation {
final static String[] texts = { "Underline", "Strikeout", "Squiggly", "Highlight", "Strikeout Underline" };
final static String[] annotations = { "Underline", "Strikeout", "Squiggly", "Highlight" };
public static void main (String[] args) throws Exception {
String outputFileName = "SimpleAnnotation.pdf";
if (args.length > 0)
outputFileName = args[0];
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
PDRectangle rect = page.getMediaBox();
document.addPage(page);
PDPageContentStream cos = new PDPageContentStream(document, page);
cos.setFont(PDType1Font.HELVETICA, 14);
int line = 0;
for (String str : texts) {
cos.beginText();
cos.newLineAtOffset(100, rect.getHeight() - 50*(++line));
cos.showText(str);
cos.endText();
}
cos.close();
PDFTextStripper stripper = new MyAnnotator();
stripper.setSortByPosition(true);
stripper.setStartPage(0);
stripper.setEndPage(document.getNumberOfPages());
Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
stripper.writeText(document, dummy);
document.save(outputFileName);
document.close();
}
private static class MyAnnotator extends PDFTextStripper {
public MyAnnotator() throws IOException {
super();
}
@Override
protected void writeString (String string, List<TextPosition> textPositions) throws IOException {
float posXInit = 0, posXEnd = 0, posYInit = 0, posYEnd = 0, width = 0, height = 0, fontHeight = 0;
for (String anno : annotations) {
if (string.contains(anno)) {
posXInit = textPositions.get(0).getXDirAdj();
posXEnd = textPositions.get(textPositions.size() - 1).getXDirAdj()
+ textPositions.get(textPositions.size() - 1).getWidth();
posYInit = textPositions.get(0).getPageHeight() - textPositions.get(0).getYDirAdj();
posYEnd = textPositions.get(0).getPageHeight() - textPositions.get(textPositions.size() - 1).getYDirAdj();
width = textPositions.get(0).getWidthDirAdj();
height = textPositions.get(0).getHeightDir();
List<PDAnnotation> annotationsInPage = document.getPage(this.getCurrentPageNo() - 1).getAnnotations();
PDAnnotationTextMarkup markup = null;
PDColor color = new PDColor(new float[]{ 1, 1 / 255F, 1 }, PDDeviceRGB.INSTANCE);
switch (anno) {
case "Underline":
markup = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_UNDERLINE);
break;
case "Strikeout":
markup = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_STRIKEOUT);
break;
case "Squiggly":
markup = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_SQUIGGLY);
break;
case "Highlight":
markup = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
break;
}
PDRectangle position = new PDRectangle();
position.setLowerLeftX(posXInit);
position.setLowerLeftY(posYEnd);
position.setUpperRightX(posXEnd);
position.setUpperRightY(posYEnd + height);
markup.setRectangle(position);
float quadPoints[] = {posXInit, posYEnd + height + 2,
posXEnd, posYEnd + height + 2,
posXInit, posYInit - 2,
posXEnd, posYEnd - 2};
markup.setQuadPoints(quadPoints);
markup.setColor(color);
annotationsInPage.add(markup);
}
}
}
}
}
CodeSnippets