将 pdf 文件复制到另一个文件时添加注释
adding annotations while copying a pdf file to another
我正在尝试为复制到我的新 pdf 的每个页面添加注释,但无法做到...
这是我的代码。
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.*;
import java.awt.*;
import java.io.FileOutputStream;
public class Annotations {
public static void main(String[] args) {
try {
PdfReader reader = new PdfReader("string-to-pdf.pdf");
Document document = new Document(reader.getPageSizeWithRotation(1));
PdfCopy copy = new PdfCopy(document,
new FileOutputStream("temp.pdf"));
copy.setPdfVersion(PdfWriter.VERSION_1_5);
document.open();
for(int i = 1; i <=reader.getNumberOfPages();i++){
copy.addPage(copy.getImportedPage(reader,i));
copy.addAnnotation(PdfAnnotation.createLink(copy, new Rectangle(200f, 700f, 30455454f, 800f), PdfAnnotation.HIGHLIGHT_TOGGLE, PdfAction.javaScript("app.alert('Hello');\r", copy)));
}
document.newPage();
// page 3
PdfContentByte pcb = new PdfContentByte(copy);
pcb.setColorFill(new Color(0xFF, 0x00, 0x00));
document.close();
} catch (Exception de) {
de.printStackTrace();
}
}
}
正在复制文件,但在新文件中看不到注释。
PdfCopy
是为了忠实的页面复制,不是为了创作;因此,从 PdfWriter
继承的修改例程被禁用,例如
@Override
public void addAnnotation(PdfAnnotation annot) { }
不过,可以通过页面标记进行专门的操作,参见。 createPageStamp
。该方法的 JavaDocs 包含一些示例使用代码,包括添加注释:
PdfImportedPage page = copy.getImportedPage(reader, 1);
PdfCopy.PageStamp ps = copy.createPageStamp(page);
ps.addAnnotation(PdfAnnotation.createText(copy, new Rectangle(50, 180, 70, 200), "Hello", "No Thanks", true, "Comment"));
PdfContentByte under = ps.getUnderContent();
under.addImage(img);
PdfContentByte over = ps.getOverContent();
over.beginText();
over.setFontAndSize(bf, 18);
over.setTextMatrix(30, 30);
over.showText("total page " + totalPage);
over.endText();
ps.alterContents();
copy.addPage(page);
但是请注意,像这样应用 PageStamp
实际上会操纵原始 PdfReader
。因此,之后不要继续使用 PdfReader
实例,假设其内容是原始内容,特别是 pagestamped 复制页面是脏的。
我正在尝试为复制到我的新 pdf 的每个页面添加注释,但无法做到...
这是我的代码。
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.*;
import java.awt.*;
import java.io.FileOutputStream;
public class Annotations {
public static void main(String[] args) {
try {
PdfReader reader = new PdfReader("string-to-pdf.pdf");
Document document = new Document(reader.getPageSizeWithRotation(1));
PdfCopy copy = new PdfCopy(document,
new FileOutputStream("temp.pdf"));
copy.setPdfVersion(PdfWriter.VERSION_1_5);
document.open();
for(int i = 1; i <=reader.getNumberOfPages();i++){
copy.addPage(copy.getImportedPage(reader,i));
copy.addAnnotation(PdfAnnotation.createLink(copy, new Rectangle(200f, 700f, 30455454f, 800f), PdfAnnotation.HIGHLIGHT_TOGGLE, PdfAction.javaScript("app.alert('Hello');\r", copy)));
}
document.newPage();
// page 3
PdfContentByte pcb = new PdfContentByte(copy);
pcb.setColorFill(new Color(0xFF, 0x00, 0x00));
document.close();
} catch (Exception de) {
de.printStackTrace();
}
}
}
正在复制文件,但在新文件中看不到注释。
PdfCopy
是为了忠实的页面复制,不是为了创作;因此,从 PdfWriter
继承的修改例程被禁用,例如
@Override
public void addAnnotation(PdfAnnotation annot) { }
不过,可以通过页面标记进行专门的操作,参见。 createPageStamp
。该方法的 JavaDocs 包含一些示例使用代码,包括添加注释:
PdfImportedPage page = copy.getImportedPage(reader, 1);
PdfCopy.PageStamp ps = copy.createPageStamp(page);
ps.addAnnotation(PdfAnnotation.createText(copy, new Rectangle(50, 180, 70, 200), "Hello", "No Thanks", true, "Comment"));
PdfContentByte under = ps.getUnderContent();
under.addImage(img);
PdfContentByte over = ps.getOverContent();
over.beginText();
over.setFontAndSize(bf, 18);
over.setTextMatrix(30, 30);
over.showText("total page " + totalPage);
over.endText();
ps.alterContents();
copy.addPage(page);
但是请注意,像这样应用 PageStamp
实际上会操纵原始 PdfReader
。因此,之后不要继续使用 PdfReader
实例,假设其内容是原始内容,特别是 pagestamped 复制页面是脏的。