- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了一个非常棘手的问题。我们有应该填写的表单,但有些人在福昕阅读器中使用注释自由格式文本注释而不是填写表单字段,因此注释永远不会变平。当我们的渲染软件生成最终文档时,不包括注释。
我尝试的解决方案基本上是浏览文档,获取注释文本内容并将其写入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());
}
}
最佳答案
这不是一件有趣的事情。经过一百万次不同的测试,我仍然不明白所有的细微差别,但这个版本似乎可以扁平化所有 pdf 文件和注释(如果它们在 PDF 上可见)。测试了大约六个 pdf 创建者,如果注释在页面上可见,则有望将其扁平化。我怀疑有更好的方法,通过拉动矩阵并对其进行转换等等,但这是我让它在任何地方工作的唯一方法。
public static void flattenv3(String startDoc, String endDoc) {
org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO);
String finalFlat = endDoc;
try {
try {
//BasicConfigurator.configure();
File myFile = new File(startDoc);
PDDocument pdDoc = PDDocument.load(myFile);
PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
PDAcroForm pdAcroForm = pdCatalog.getAcroForm();
if (pdAcroForm != null) {
pdAcroForm.setNeedAppearances(false);
pdAcroForm.flatten();
}
// set the NeedApperances flag
boolean isContentStreamWrapped;
int ii = 0;
for (PDPage page: pdDoc.getPages()) {
PDPageContentStream contentStream;
isContentStreamWrapped = false;
List < PDAnnotation > annotations = new ArrayList < > ();
for (PDAnnotation annotation: page.getAnnotations()) {
if (!annotation.isInvisible() && !annotation.isHidden() && annotation.getNormalAppearanceStream() != null)
{
ii++;
if (ii > 1) {
// contentStream.close();
// continue;
}
if (!isContentStreamWrapped) {
contentStream = new PDPageContentStream(pdDoc, page, AppendMode.APPEND, true, true);
isContentStreamWrapped = true;
} else {
contentStream = new PDPageContentStream(pdDoc, page, AppendMode.APPEND, true);
}
PDAppearanceStream appearanceStream = annotation.getNormalAppearanceStream();
PDFormXObject fieldObject = new PDFormXObject(appearanceStream.getCOSObject());
contentStream.saveGraphicsState();
boolean needsTranslation = resolveNeedsTranslation(appearanceStream);
Matrix transformationMatrix = new Matrix();
boolean transformed = false;
float lowerLeftX = annotation.getNormalAppearanceStream().getBBox().getLowerLeftX();
float lowerLeftY = annotation.getNormalAppearanceStream().getBBox().getLowerLeftY();
PDRectangle bbox = appearanceStream.getBBox();
PDRectangle fieldRect = annotation.getRectangle();
float xScale = fieldRect.getWidth() - bbox.getWidth();
transformed = true;
lowerLeftX = fieldRect.getLowerLeftX();
lowerLeftY = fieldRect.getLowerLeftY();
if (bbox.getLowerLeftX() <= 0 && bbox.getLowerLeftY() < 0 && Math.abs(xScale) < 1) //BASICALLY EQUAL TO 0 WITH ROUNDING
{
lowerLeftY = fieldRect.getLowerLeftY() - bbox.getLowerLeftY();
if (bbox.getLowerLeftX() < 0 && bbox.getLowerLeftY() < 0) //THis is for the o
{
lowerLeftX = lowerLeftX - bbox.getLowerLeftX();
}
} else if (bbox.getLowerLeftX() == 0 && bbox.getLowerLeftY() < 0 && xScale >= 0) {
lowerLeftX = fieldRect.getUpperRightX();
} else if (bbox.getLowerLeftY() <= 0 && xScale >= 0) {
lowerLeftY = fieldRect.getLowerLeftY() - bbox.getLowerLeftY() - xScale;
} else if (bbox.getUpperRightY() <= 0) {
if (annotation.getNormalAppearanceStream().getMatrix().getShearY() < 0) {
lowerLeftY = fieldRect.getUpperRightY();
lowerLeftX = fieldRect.getUpperRightX();
}
} else {
}
transformationMatrix.translate(lowerLeftX,
lowerLeftY);
contentStream.transform(transformationMatrix);
contentStream.drawForm(fieldObject);
contentStream.restoreGraphicsState();
contentStream.close();
}
}
page.setAnnotations(annotations);
}
pdDoc.save(finalFlat);
pdDoc.close();
File file = new File(finalFlat);
// Desktop.getDesktop().browse(file.toURI());
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
System.err.println("Exception: " + e.getLocalizedMessage());
}
}
}
关于java - PDFBOX 2.0+ java 拼合注释 freetext 由 Foxit 创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61112832/
Foxit 手册说您可以在 pdf 中创建一个可以运行 java 脚本的链接。请参阅下面的选项“I”。 是否可以编写 java 脚本来打开存储在 pdf 附件中的 excel 文件? 我对 Java
通过批处理文件使用Foxit Reader通过FOR循环打印PDF文件: SET printing=C:\PDF\Printing SET fox=C:\Program Files\Foxit Sof
我需要将 Foxit Reader(PDF 阅读软件)嵌入到网页中。有谁知道在以下代码中使用正确的 classid 和参数: You must install Foxit
我在通过 Foxit Reader 打印 PDF 时遇到问题。问题不在于它没有打印,而是在尝试这样做时加载了GUI。我想要打印只是发生在后台...... 到目前为止我读到的所有内容都表明下面的代码实际
场景:WinForms 桌面应用程序,.Net 2.0 我们有 0.01% 的用户无法打印我们通过 FoxIt PDF SDK ActiveX v5.0.1 呈现的文档 当用户单击“打印”按钮时,我们
为什么这个 PDF 在 Foxit Reader 而不是 Adobe Reader 中显示签名? 这是来自 Syncfusion PDF library 的代码用于生成它(另请参阅有关 signi
我遇到了一个非常棘手的问题。我们有应该填写的表单,但有些人在福昕阅读器中使用注释自由格式文本注释而不是填写表单字段,因此注释永远不会变平。当我们的渲染软件生成最终文档时,不包括注释。 我尝试的解决方案
我的应用程序正在使用 itext# 生成 PDF 文档。文件可以正常打开并在 Foxit Reader 中正确显示,但在 Adobe Acrobat 中会出现以下错误: There was an er
我正在尝试从 HTML 页面链接到 PDF 文件。我想添加指向特定页面的链接,使用诸如 http://www.mydomain.com/some-pdf-file.pdf#nameddest=some
长话短说,当您使用 Web browser control和 VBA 打开嵌入在表单中的 pdf 文件,pdf 阅读器会自动触发打印事件。 当前设置 Win1064Bit/Office365 版本 1
当我将 R 图保存为 pdf 时,轴文本“℃”变成了点“...”。为什么? 我正在使用 Windows 平台和 Foxit 阅读器。 特定符号不能出现在pdf文件中吗? 最佳答案 使用 ?plotma
我是一名优秀的程序员,十分优秀!