在java中使用itext填写pdf表单有问题

发布于 2024-12-10 20:18:08 字数 873 浏览 0 评论 0原文

我正在使用 iText 填写 pdf 表单,控制台中没有错误,但是当我打开输出 pdf 时,它给出“预期一个 dict 对象”,并在给出另一条消息后显示“此文档在 Adob​​e reader 中启用了扩展功能。该文档自创建以来已发生更改,并且不再可以使用扩展功能。”

此外,输出 pdf 也有我所做的更改,但它不具有原始 pdf 文档的功能,如上面的消息所示。

我使用了下面的代码...

PdfReader reader = new PdfReader("C:\\Users\\asfs\\Downloads\\1013-Form22.pdf");
String name = "C:\\Users\\asfs\\Downloads\\Temp.pdf";

PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(name));
AcroFields form = stamp.getAcroFields();

String last = "Form22_Dtls[0].Page1[0].Country_C[0]";

form.setField(last, "Sample Country name");

HashMap map = new HashMap();
map = form.getFields();
Iterator iterator = map.keySet().iterator();
while(iterator.hasNext())
    System.out.println("Field is >>>"+iterator.next());
// close pdf stamper
stamp.setFormFlattening(true);
stamp.close();

请帮助我。

谢谢

I am using iText to fill pdf form, no error in console, but when i am opening the output pdf it is giving "Expected a dict object" and after it gives another message saying "This document enabled extended features in Adobe reader. The document has been changed since it was created and use of extended features is no longer available."

Also the output pdf have the changes i made but it does not have features of original pdf document, as the above message shows.

I have used below code...

PdfReader reader = new PdfReader("C:\\Users\\asfs\\Downloads\\1013-Form22.pdf");
String name = "C:\\Users\\asfs\\Downloads\\Temp.pdf";

PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(name));
AcroFields form = stamp.getAcroFields();

String last = "Form22_Dtls[0].Page1[0].Country_C[0]";

form.setField(last, "Sample Country name");

HashMap map = new HashMap();
map = form.getFields();
Iterator iterator = map.keySet().iterator();
while(iterator.hasNext())
    System.out.println("Field is >>>"+iterator.next());
// close pdf stamper
stamp.setFormFlattening(true);
stamp.close();

Please help me.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

香草可樂 2024-12-17 20:18:08

尝试:

// open the pdf stamper in edit mode
stamp = new PdfStamper(reader, new FileOutputStream(name), '\0', true);

PdfStamper 需要处于追加模式。

Try:

// open the pdf stamper in edit mode
stamp = new PdfStamper(reader, new FileOutputStream(name), '\0', true);

The PdfStamper needs to be in append mode.

百思不得你姐 2024-12-17 20:18:08

如果您想要 Java 代码,这里是我从博客中获得的一个片段。但它使用 pdf Nitro,以防万一这有帮助或让我知道

private ByteArrayOutputStream editPdfDocument() throws Exception {
PdfReader reader = null;
PdfStamper stamper = null;
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
HashMap fieldsWithValues = new HashMap();
int user_id = 1234; // unique key for the object
String dirPath = “D:/abc”;// directory path
String fileName = “def.pdf”;// name of the file
try {
reader = new PdfReader(dirPath + “/” + fileName);
stamper = new PdfStamper(reader, baosPDF);
AcroFields form = stamper.getAcroFields();
HashMap fields = form.getFields();
Set keys = fields.keySet();   // keys represents the names of all the form fields
fieldsWithValues = fetchFieldValuesForObject(user_id, keys);
// fetchFieldValuesForObject(user_id, keys) method will fetch the values of the fields from Database for object identified by user_id
Iterator itr = keys.iterator();
while (itr.hasNext()) {
String fieldName = (String) itr.next();
String fieldValue = fieldsWithValues.get(fieldName) != null ? (String)(fieldsWithValues.get(fieldName)) : “”;
form.setField(fieldName, fieldValue);
}
stamper.setFormFlattening(true);
} catch (Exception dex) {
if (stamper != null)
stamper.close();
if (reader != null)
reader.close();
throw dex;
}
return baosPDF;
}`enter code here`

它是使用 Java 直接从数据库检索数据的 Pdf 表单填写,希望它有效
访问自动填写表单的 Java 代码

if you want a Java code, here's a snippet I got from one a blog. But it uses pdf Nitro, in case this helps or let me know

private ByteArrayOutputStream editPdfDocument() throws Exception {
PdfReader reader = null;
PdfStamper stamper = null;
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
HashMap fieldsWithValues = new HashMap();
int user_id = 1234; // unique key for the object
String dirPath = “D:/abc”;// directory path
String fileName = “def.pdf”;// name of the file
try {
reader = new PdfReader(dirPath + “/” + fileName);
stamper = new PdfStamper(reader, baosPDF);
AcroFields form = stamper.getAcroFields();
HashMap fields = form.getFields();
Set keys = fields.keySet();   // keys represents the names of all the form fields
fieldsWithValues = fetchFieldValuesForObject(user_id, keys);
// fetchFieldValuesForObject(user_id, keys) method will fetch the values of the fields from Database for object identified by user_id
Iterator itr = keys.iterator();
while (itr.hasNext()) {
String fieldName = (String) itr.next();
String fieldValue = fieldsWithValues.get(fieldName) != null ? (String)(fieldsWithValues.get(fieldName)) : “”;
form.setField(fieldName, fieldValue);
}
stamper.setFormFlattening(true);
} catch (Exception dex) {
if (stamper != null)
stamper.close();
if (reader != null)
reader.close();
throw dex;
}
return baosPDF;
}`enter code here`

It's Pdf form filling using Java that directly retrieves data from database, hope it works
Visit Java Codes for Automatic form fill

蓝戈者 2024-12-17 20:18:08

据我所知, stamp.setFormFlattening(true);您正在调用,删除字段并将其替换为内容,这就是您的新文档不保留字段的原因

as far as i know, the stamp.setFormFlattening(true); you are calling, remove fields and replace that with content, that's why your new document doesn't preserve fields

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文