Advertisement
Guest User

Attach text to highlight in a PDF

a guest
Mar 11th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. import java.awt.geom.Rectangle2D;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6.  
  7. import javax.management.MXBean;
  8.  
  9. import org.apache.pdfbox.pdmodel.PDDocument;
  10. import org.apache.pdfbox.pdmodel.PDPage;
  11. import org.apache.pdfbox.pdmodel.common.PDRectangle;
  12. import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
  13. import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup;
  14. import org.apache.pdfbox.util.PDFTextStripperByArea;
  15.  
  16. public class Test {
  17.  
  18.     public static void main(String[] args) {
  19.  
  20.         try {
  21.             PDDocument pddDocument = PDDocument.load(new File("./sample.pdf"));
  22.             List allPages = pddDocument.getDocumentCatalog().getAllPages();
  23.             for (int i = 0; i < allPages.size(); i++) {
  24.                 PDPage page = (PDPage) allPages.get(i);
  25.                 List<PDAnnotation> la = page.getAnnotations();
  26.                 for (PDAnnotation anot : la) {
  27.                     if (anot instanceof PDAnnotationTextMarkup) {
  28.                         System.out.println("Highlight found");
  29.                         processHighlight((PDAnnotationTextMarkup) anot);
  30.                         System.out.println("====");
  31.                     }
  32.                 }
  33.                 pddDocument.save(new File("./result.pdf"));
  34.             }
  35.             pddDocument.close();
  36.         } catch (Exception ex) {
  37.             ex.printStackTrace();
  38.         }
  39.  
  40.     }
  41.  
  42.     private static void processHighlight(PDAnnotationTextMarkup highlight)
  43.             throws IOException {
  44.         System.out.println(highlight.getRectangle());
  45.         float[] quads = highlight.getQuadPoints();
  46.         PDFTextStripperByArea stripper = new PDFTextStripperByArea();
  47.         PDRectangle pagesize = highlight.getPage().findMediaBox();
  48.         System.out.println("Points: " + quads.length);
  49.         for (int i = 0; i < quads.length; i++)
  50.             System.out.println(quads[i]);
  51.  
  52.         for (int i = 0; i < quads.length; i += 8) {
  53.             stripper.setSortByPosition(true);
  54.             Rectangle2D.Float rect = new Rectangle2D.Float(quads[i] - 1,
  55.                     pagesize.getHeight() - quads[i + 1], quads[i + 6]
  56.                             - quads[i], quads[i + 1] - quads[i + 7]);
  57.             stripper.addRegion("" + i, rect);
  58.         }
  59.         stripper.extractRegions(highlight.getPage());
  60.         List<String> lines = new LinkedList<String>();
  61.         for (String region : stripper.getRegions())
  62.             lines.add(stripper.getTextForRegion(region));
  63.  
  64.         // Format text and set it as comment of the annotation
  65.         String highlightText = "";
  66.         for (String line : lines)
  67.             highlightText = highlightText + line;
  68.         highlight.setContents(highlightText);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement