【问题标题】:How do I add annotation to existing PDF如何向现有 PDF 添加注释
【发布时间】:2015-12-16 20:56:07
【问题描述】:

我拥有添加标题、内容、页面编号和坐标值的注释的日期。我想使用这些数据向现有 PDF 添加注释。 我尝试使用 pdfbox(Java) 向现有 PDF 添加注释。我可以在现有页面的最后部分添加注释新页面,但由于无法理解如何访问现有页面以及如何为页面添加注释,我无法向现有页面添加注释。

下面是我的代码。请解决。

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();
    }
  }
}

【问题讨论】:

    标签: java pdf annotations


    【解决方案1】:

    要向现有页面添加注释,请执行以下操作:

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

    您的代码主要在 1.8 中工作,这是很难看到的注释。一些代码错误:

    改变

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

    到

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

    改变

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

    到

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

    另外,最好这样获取您的文档:

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

    要查看注释的位置,请在 Adob​​e Reader 中单击最后一页的“注释”、“注释列表”。要使注释更明显,请添加:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 2020-11-25
    • 2015-04-19
    • 1970-01-01
    相关资源
    最近更新 更多