我想知道如何在 PDFBOX 中创建 TextField。 (itext 到 pdfbox 迁移)
以前,文本框是使用 iText 创建的来给出名称,然后使用 JavaScript 来隐藏文本。
我目前正在努力将其更改为 PDFBox。 使用 PDPageContentStream 时, JS无法获取TextField的字段名。
有什么好的办法吗?
iText 源
Rectangle rectangle = new Rectangle(pageSize);
rectangle.setBackgroundColor(Color.BLACK);
TextField text = new TextField(writer, rectangle, "text" + pageIndex);
text.setWriter(writer);
text.setTextColor(Color.WHITE);
text.setFont(baseFont);
text.setFontSize(100);
text.setBackgroundColor(Color.BLACK);
text.setOptions(TextField.READ_ONLY);
text.setText("NOT PRINT");
text.setAlignment(Element.ALIGN_CENTER);
writer.addAnnotation(text.getTextField());
pdfBox 源 (1)
PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND,true, true);
contentStream.addRect(0, 0, page.getMediaBox().getWidth(), page.getMediaBox().getHeight());
contentStream.setNonStrokingColor(Color.black);
contentStream.fill();
contentStream.beginText();
contentStream.setTextMatrix(Matrix.getTranslateInstance(0f,0f));
contentStream.newLineAtOffset(x, y);
contentStream.showText("NOT PRINT");
contentStream.endText();
pdfBox 源 (2)
for (int i = 0 ; i < document.getNumberOfPages() ; i++) {
PDPage page = document.getPage(i);
PDAcroForm acroForm = new PDAcroForm(document);
document.getDocumentCatalog().setAcroForm(acroForm);
PDResources resources = new PDResources();
acroForm.setDefaultResources(resources);
String fontName = resources.add(font).getName();
String defaultAppearance = "/" + fontName + " 120 Tf 1 0 0 1 260 254 rg";
PDTextField tf = new PDTextField(acroForm);
tf.setDefaultAppearance(defaultAppearance);
tf.setAlternateFieldName("text"+i);
tf.setPartialName("text"+i);
tf.setDefaultAppearance("text"+i);
tf.setValue("not print");
acroForm.getFields().add(tf);
PDAnnotationWidget widget = tf.getWidgets().get(0);
widget.setPrinted(true);
acroForm.setNeedAppearances(true);
widget.setAnnotationName("text"+i);
page.getAnnotations().add(widget);
}
JavaScript
javascript.append("var found = false;");
javascript.append(" if(this.URL.indexOf('" + pdf_accept_url + "') == 0){");
javascript.append(" found = true;");
javascript.append("}");
javascript.append("if (found == true) {");
javascript.append(" var len = " + pageCount + ";");
javascript.append(" for (var i=0; i<len; i++) {");
if (existPrintPermission) {
// 1 – Field Hidden in Viewer and in Printed Document
javascript.append(" this.getField('text'+i).display = 1;");
} else {
// 3 – Field Hidden in Viewer, Visible on Printed Document
javascript.append(" this.getField('text'+i).display = 3;");
}
javascript.append(" }");
javascript.append("}");
如果您使用 Pdfbox 源1, 我无法从js获取contentStream值。
当使用Pdfbox source2时, 我成功获取了警报中文本字段的值, 但是,它不会出现在实际屏幕上。
Previously, textboxes were created using iText to give names, and then JavaScript was used to hide text.
I'm currently working on changing it to PDFBox.
When working with PDPageContentStream,
JS is unable to obtain the field name of TextField.
Is there a good way?
iText Source
Rectangle rectangle = new Rectangle(pageSize);
rectangle.setBackgroundColor(Color.BLACK);
TextField text = new TextField(writer, rectangle, "text" + pageIndex);
text.setWriter(writer);
text.setTextColor(Color.WHITE);
text.setFont(baseFont);
text.setFontSize(100);
text.setBackgroundColor(Color.BLACK);
text.setOptions(TextField.READ_ONLY);
text.setText("NOT PRINT");
text.setAlignment(Element.ALIGN_CENTER);
writer.addAnnotation(text.getTextField());
pdfBox Source (1)
PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND,true, true);
contentStream.addRect(0, 0, page.getMediaBox().getWidth(), page.getMediaBox().getHeight());
contentStream.setNonStrokingColor(Color.black);
contentStream.fill();
contentStream.beginText();
contentStream.setTextMatrix(Matrix.getTranslateInstance(0f,0f));
contentStream.newLineAtOffset(x, y);
contentStream.showText("NOT PRINT");
contentStream.endText();
pdfBox Source (2)
for (int i = 0 ; i < document.getNumberOfPages() ; i++) {
PDPage page = document.getPage(i);
PDAcroForm acroForm = new PDAcroForm(document);
document.getDocumentCatalog().setAcroForm(acroForm);
PDResources resources = new PDResources();
acroForm.setDefaultResources(resources);
String fontName = resources.add(font).getName();
String defaultAppearance = "/" + fontName + " 120 Tf 1 0 0 1 260 254 rg";
PDTextField tf = new PDTextField(acroForm);
tf.setDefaultAppearance(defaultAppearance);
tf.setAlternateFieldName("text"+i);
tf.setPartialName("text"+i);
tf.setDefaultAppearance("text"+i);
tf.setValue("not print");
acroForm.getFields().add(tf);
PDAnnotationWidget widget = tf.getWidgets().get(0);
widget.setPrinted(true);
acroForm.setNeedAppearances(true);
widget.setAnnotationName("text"+i);
page.getAnnotations().add(widget);
}
JavaScript
javascript.append("var found = false;");
javascript.append(" if(this.URL.indexOf('" + pdf_accept_url + "') == 0){");
javascript.append(" found = true;");
javascript.append("}");
javascript.append("if (found == true) {");
javascript.append(" var len = " + pageCount + ";");
javascript.append(" for (var i=0; i<len; i++) {");
if (existPrintPermission) {
// 1 – Field Hidden in Viewer and in Printed Document
javascript.append(" this.getField('text'+i).display = 1;");
} else {
// 3 – Field Hidden in Viewer, Visible on Printed Document
javascript.append(" this.getField('text'+i).display = 3;");
}
javascript.append(" }");
javascript.append("}");
If you use Pdfbox source1,
I failed to get the contentStream value from js.
When using Pdfbox source2,
I succeeded in getting the value of the text field in alert,
but, it does not appear on the actual screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论