Reputation: 43
Based on the code posted in https://stackoverflow.com/a/51654691 I have been able to apply the highlight, underline and strikeout annotations to text in PDFs. Now I'm trying the conceptually similar squiggly annotation, and it doesn't seem to do anything (the text appears unaltered).
I can't find any examples specifically for this (and in the PDFBox JIRA no relevant issues come up regarding "squiggly"), so I'm stuck on how to use this annotation.
I could post the code I used, but it is essentially the same as the one linked above, except using SUB_TYPE_SQUIGGLY instead of SUB_TYPE_HIGHLIGHT.
Update: This is the code I'm testing this with in its most basic form.
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
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;
public class MainSourceSO {
public static void main (String[] args) throws Exception {
PDDocument document = PDDocument.load(new File("SO-example.pdf"));
List<PDAnnotation> annotations = document.getPage(0).getAnnotations();
PDColor color = new PDColor(new float[] { 1, 1 / 255F, 1 }, PDDeviceRGB.INSTANCE);
PDAnnotationTextMarkup highlight = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_UNDERLINE);
highlight.setRectangle(new PDRectangle(72, 400, 72, 18));
highlight.setQuadPoints(new float[]{72.0f,418.0f,142.002f,418.0f,72.0f,400.0f,142.002f,400.0f});
highlight.setColor(color);
annotations.add(highlight);
highlight = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
highlight.setRectangle(new PDRectangle(72, 500, 72, 18));
highlight.setQuadPoints(new float[]{72.0f,518.0f,142.002f,518.0f,72.0f,500.0f,142.002f,500.0f});
highlight.setColor(color);
annotations.add(highlight);
highlight = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_STRIKEOUT);
highlight.setRectangle(new PDRectangle(72, 600, 72, 18));
highlight.setQuadPoints(new float[]{72.0f,618.0f,142.002f,618.0f,72.0f,600.0f,142.002f,600.0f});
highlight.setColor(color);
annotations.add(highlight);
highlight = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_SQUIGGLY);
highlight.setRectangle(new PDRectangle(72, 700, 72, 18));
highlight.setQuadPoints(new float[]{72.0f,718.0f,142.002f,718.0f,72.0f,700.0f,142.002f,700.0f});
highlight.setColor(color);
annotations.add(highlight);
File file1 = new File("SO-example-2.pdf");
document.save(file1);
}
}
Upvotes: 2
Views: 912
Reputation: 18851
This code worked for me:
PDDocument document = PDDocument.load(...);
List<PDAnnotation> annotations = document.getPage(0).getAnnotations();
PDAnnotationTextMarkup highlight = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_SQUIGGLY);
highlight.setRectangle(new PDRectangle(72, 500, 72, 18));
highlight.setQuadPoints(new float[]{72.0f,518.0f,142.002f,518.0f,72.0f,500.0f,142.002f,500.0f});
PDColor yellow = new PDColor(new float[] { 1, 1, 100 / 255F }, PDDeviceRGB.INSTANCE);
highlight.setColor(yellow);
annotations.add(highlight);
document.save(...);
I don't know what didn't work for you. Maybe it was the color (hard to see), maybe the rectangle, or maybe the quadpoints. (There is a bug in the PDF specification about the quadpoints, see here).
Upvotes: 2