我遇到了一个非常棘手的问题。我们有应该填写的表单,但有些人在 foxit 中使用注释自由格式文本注释而不是填写表单字段,因此注释永远不会变平。当我们的渲染软件生成最终文档时,注释不包括在内。
我尝试的解决方案是基本上浏览文档,获取注释文本内容并将其写入 pdf,使其位于最终文档上,然后删除实际注释,但我遇到了一个我不知道字体的问题注释正在使用、行距等,因此无法找到如何从 pdfbox 中获取它以准确地重新创建注释,因为注释看起来在未展平的表单上。基本上我想展平在 foxit 中创建的自由形式的注释(打字机注释功能)这是代码。它正在工作,但我再次努力弄清楚如何让注释写入我的最终 pdf 文档。再次在 acroform 上展平不起作用,因为这些不是 acroform 字段!实时代码过滤掉任何不是自由文本类型注释的内容,但下面的代码应该显示我的问题。
public static void main(String [] args)
{
String startDoc = "C:/test2/test.pdf";
String finalFlat = "C:/test2/test_FLAT.pdf";
try {
// for testing
try {
//BasicConfigurator.configure();
File myFile = new File(startDoc);
PDDocument pdDoc = PDDocument.load( myFile );
PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
PDAcroForm pdAcroForm = pdCatalog.getAcroForm();
// set the NeedApperances flag
pdAcroForm.setNeedAppearances(false);
// correct the missing page link for the annotations
for (PDPage page : pdDoc.getPages()) {
for (PDAnnotation annot : page.getAnnotations()) {
System.out.println(annot.getContents());
System.out.println(annot.isPrinted());
System.out.println(annot.isLocked());
System.out.println(annot.getAppearance().toString());
PDPageContentStream contentStream = new PDPageContentStream(pdDoc, page, PDPageContentStream.AppendMode.APPEND,true,true);
int fontHeight = 14;
contentStream.setFont(PDType1Font.TIMES_ROMAN, fontHeight);
float height = annot.getRectangle().getLowerLeftY();
String s = annot.getContents().replaceAll("\t", " ");
String ss[] = s.split("\\r");
for(String sss : ss)
{
contentStream.beginText();
contentStream.newLineAtOffset(annot.getRectangle().getLowerLeftX(),height );
contentStream.showText(sss);
height = height + fontHeight * 2 ;
contentStream.endText();
}
contentStream.close();
page.getAnnotations().remove(annot);
}
}
pdAcroForm.flatten();
pdDoc.save(finalFlat);
pdDoc.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
catch (Exception e) {
System.err.println("Exception: " + e.getLocalizedMessage());
}
}