【问题标题】:get all acrosfields avalibles in a pdf document itext7在 pdf 文档 itext7 中获取所有 acrosfields avalibles
【发布时间】:2017-09-27 22:38:21
【问题描述】:

我正在尝试根据一些数据计算(使用Java)自动修改pdf模板

我没有修改 pdf 的经验,我正在尝试使用 itext7 来执行此操作。

我一直在阅读如何将文本添加到 pdf 甚至 here 我看到了如何使用“键”来填写 Acrosfield 字段

不过,我没有制作我正在使用的 pdf 模板(可修改),所以我不知道您可以手动填写的字段是用 Acrosfields 还是其他技术制作的,我不知道是什么键或每个字段如果他们有一个......

我看到this的问题;它在哪里说如何获取所有字段及其值,但是当我尝试出现在我得到的唯一答案中的代码时;

main.java:[40,0] error: illegal start of type
main.java:[40,19] error: ')' expected
main.java:[40,30] error: <identifier> expected
3 errors 

在这部分:

for (String fldName : fldNames) {
    System.out.println( fldName + ": " + fields.getField( fldName ) );
}

经过一番尝试,我一直在寻找更多信息,但如果可能的话,我找不到获得这些“钥匙”的方法......

------- 编辑 -------

我编写此代码是为了复制我的 pdf 模板,其中每个字段中都有 Acrosfield 键的名称:

package novgenrs;


import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Set;
public class MakePDF {

public static void MakePDF(String[] args) throws IOException, DocumentException{


PdfReader reader = new PdfReader("template.pdf");

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("result.pdf"));

//AcroFields form = stamper.getAcroFields();

AcroFields fields = reader.getAcroFields();

AcroFields wrt = stamper.getAcroFields(); 

Set<String> fldNames = fields.getFields().keySet();

for (String fldName : fldNames) {
  wrt.setField(fldName, fldName) ; 
}

stamper.close();
reader.close(); 

}


}

注意:这仅适用于 itext5。出于某种原因,当我尝试使用 itext7 执行此操作时,我无法使其正常工作,因此我尝试使用 itext5 执行此操作,并且成功了!

【问题讨论】:

  • 如果 fields 的类型为 AcroFields 并且 fldNames 是字符串集合,则该循环看起来没问题。您可能需要在该循环之前发布您的代码
  • :/ 我找到了为什么会出现这个错误...我已经将代码编写为主体类的一部分,而不是作为方法,因此编译器无法理解 for 循环。我以前在 C++ 上编码,但我不知道!!!

标签: java pdf itext itext7


【解决方案1】:

如果您想获得问题的完整答案,您必须提供 PDF 以便我们进行检查,但这些已经是一些可以帮助您找到正确方向的答案。

当您提到How to fill out a pdf file programmatically? (AcroForm technology) 时,您指的是How to fill out a pdf file programmatically? (AcroForm technology) 的iText 7 版本,这是同一问题的答案,但对于使用iText 5 的开发人员而言。正如您所见,iText 之间存在很大差异5 和 iText 7。

但是,当您引用 How do I get all the fields and value from a PDF file using iText? 时,您会得到一个仅用于 iText 5 的答案。如果您使用的是 iText 7,则该代码将不起作用,因为它是 iText 5 代码。

您需要的代码可以在这里找到:How to get specific types from AcroFields? Like PushButtonField, RadioCheckField, etc

PdfReader reader = new PdfReader(src);
PdfDocument pdfDoc = new PdfDocument(reader);
// Get the fields from the reader (read-only!!!)
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
// Loop over the fields and get info about them
Set<String> fields = form.getFormFields().keySet();
for (String key : fields) {
    writer.print(key + ": ");
    PdfName type = form.getField(key).getFormType();
    if (0 == PdfName.Btn.compareTo(type)) {
        if(((PdfButtonFormField)form.getField(key)).isPushButton()){
            writer.println("Pushbutton");
        } else {
            if(((PdfButtonFormField)form.getField(key)).isRadio()){
                writer.println("Radiobutton");                   
            }else {
                writer.println("Checkbox");
            }
        }
    } else if (0 == PdfName.Ch.compareTo(type)) {
        writer.println("Choicebox");
    } else if (0 == PdfName.Sig.compareTo(type)) {
        writer.println("Signature");
    } else if (0 == PdfName.Tx.compareTo(type)) {
        writer.println("Text");
    }else {
        writer.println("?");
    }
}

此代码将遍历所有字段,并将key 写入System.out,以及与该键对应的字段类型。您也可以使用RUPS 来检查您的 PDF。

你提到:

main.java:[40,0] error: illegal start of type
main.java:[40,19] error: ')' expected
main.java:[40,30] error: <identifier> expected

我不清楚这是编译器错误还是运行时错误。

  • 如果是编译器错误,您只是在某处遗漏了 ),在这种情况下,您的问题与 iText 完全无关(这是我怀疑的:您只是 非常简单 编程错误)。
  • 如果是运行时错误,则说明您的 PDF 有问题。在 PDF 中,字符串由括号分隔。也许您的 PDF 中某处缺少括号(但我怀疑:您会得到不同类型的错误)。

简而言之:尝试一个好的 IDE,该 IDE 会告诉您缺少括号的位置。如果您没有立即找到该位置,请通过添加缩进和空格来清理您的代码。这应该清楚您忘记) 的位置。

【讨论】:

  • 我过去常常在没有 IDE 的情况下在 C++ 上进行编码,但是,由于这个项目有部分是关于 Java 的,我将在本季度的代码中学习它,所以我决定在 Java 上进行,所以我使用的是 Netbeans。 . 错误是我将代码作为主类的一部分而不是作为方法编写,所以编译器无法理解我对 for 循环的意思!!
  • 好的,我尝试了你的代码,虽然模板是可写的,但我没有得到任何输出。也许它不使用 Acrosfield ......我将尝试使用 RUPS 来检查模板。我还将编辑我的第一篇文章并添加模板。
  • 如果表单有表单域(您在 Adob​​e Reader 中看到的),但没有 AcroForm 域,那么该表单可能是 XFA 表单。在这种情况下,您的 PDF 不是传统的 PDF,而 PDF 只是 XML 表单体系结构 (XFA) 格式的 XML 包装器。
猜你喜欢
  • 1970-01-01
  • 2020-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 2022-12-21
  • 2021-04-07
  • 1970-01-01
相关资源
最近更新 更多