Mycee
Mycee

Reputation: 33

How do I add annotation to existing PDF

I own date for add annotation that title & contents & pageNo & coordinate value.I want add annotation to existing PDF using these data. I tryed to add annotation to existing PDF using pdfbox(Java). I could add annotation new page that was inserted into the last part of an existing page, But I cannot add annotation to existing pages because of cannot understand how to access existing pages and how to add annotation the page.

Below is my code. Pls resoleve it.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup;

public class Sample{
  private String forannot[][];
  Sample(String[][] forannot){
    this.forannot = forannot;
  }
  public void tryaddannot() throws FileNotFoundException, IOException{
    PDFParser pps = new PDFParser(new FileInputStream(Filepath);
    pps.parse();
    PDDocument docs = pps.getPDDocument();

    try{
      //insert new page
      PDPage pages = new PDPage();
      docs.addPage(pages);
      List<PDAnnotationTextMarkup> annotations = pages.getAnnotations();

      //generate instanse for annotation
      PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);

      //set the rectangle
      PDRectangle position = new PDRectangle();
      position.setLowerLeftX(170);
      position.setLowerLeftY(125);
      position.setUpperRightX(195);
      position.setUpperRightY(140);
      txtMark.setRectangle(position);

      //set the quadpoint
      float[] quads = new float[8];
      //x1,y1
      quads[0] = position.getLowerLeftX();
      quads[1] = position.getUpperRightY()-2;
      //x2,y2
      quads[2] = position.getUpperRightX();
      quads[3] = quads[1];
      //x3,y3
      quads[4] = quads[0];
      quads[5] = position.getLowerLeftY()-2;
      //x4,y4
      quads[6] = quads[2]; 
      quads[7] = quads[5]; 
      txtMark.setQuadPoints(quads);
      txtMark.setContents("Highlighted since it's important");
      annotations.add(txtMark); 
      docs.save(Filepath);
    } catch (COSVisitorException e) {
      e.printStackTrace();
    }
    finally
    {
      docs.close();
    }
  }
}

Upvotes: 3

Views: 7051

Answers (1)

Tilman Hausherr
Tilman Hausherr

Reputation: 18851

To add annotation to existing an page, do this:

    PDPage page = (PDPage) doc.getDocumentCatalog().getAllPages().get(0);
    try
    {
        List<PDAnnotation> annotations = page.getAnnotations();

Your code mostly works in 1.8, it's the annotation that is hard to see. Some code errors:

change

PDFParser pps = new PDFParser(new FileInputStream(Filepath);

to

PDFParser pps = new PDFParser(new FileInputStream(Filepath));

and change

  List<PDAnnotationTextMarkup> annotations = pages.getAnnotations();

to

  List<PDAnnotation> annotations = pages.getAnnotations();

Also, it is better to get your document like this:

PDDocument doc = PDDocument.loadNonSeq(new File(...), null);

To see where your annotation is, either click in Adobe Reader on the last page on "Comments", "Comments list". To make the annotation more visible, add this:

        PDGamma colourBlue = new PDGamma();
        colourBlue.setB(1);
        txtMark.setColour(colourBlue);

Upvotes: 1

Related Questions