Java IText7仅在新文档上创建表单/小部件

Java IText7仅在新文档上创建表单/小部件,java,pdf,pdf-generation,itext7,Java,Pdf,Pdf Generation,Itext7,在PdfDocument没有读取源的情况下运行此代码时,它工作正常。当我尝试从一个预先制作好的pdf文件中阅读时,它会停止创建表单/小部件,但仍然会像预期的那样添加段落。没有给出错误。有人知道为什么会这样吗 以下是我正在运行的代码: public class HelloWorld { public static final String DEST = "sampleOutput.pdf"; public static final String SRC = "sample.pdf

在PdfDocument没有读取源的情况下运行此代码时,它工作正常。当我尝试从一个预先制作好的pdf文件中阅读时,它会停止创建表单/小部件,但仍然会像预期的那样添加段落。没有给出错误。有人知道为什么会这样吗

以下是我正在运行的代码:

public class HelloWorld {

    public static final String DEST = "sampleOutput.pdf";
    public static final String SRC = "sample.pdf";
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);

        new HelloWorld().createPdf(SRC, DEST);
    }

    public void createPdf(String src, String dest) throws IOException {
        //Initialize PDF reader and writer
        PdfReader reader = new PdfReader(src);
        PdfWriter writer = new PdfWriter(dest);

        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(writer); //if i do (reader, writer) the widget isn't added to the first page anymore.

        // Initialize document
        Document document = new Document(pdf);

        HelloWorld.addAcroForm(pdf, document);

        //Close document
        document.close();
    }

    public static PdfAcroForm addAcroForm(PdfDocument pdf, Document doc) throws IOException {
        Paragraph title = new Paragraph("Test Form")
                .setTextAlignment(TextAlignment.CENTER)
                .setFontSize(16);
        doc.add(title);
        doc.add(new Paragraph("Full name:").setFontSize(12));

        //Add acroform
        PdfAcroForm form = PdfAcroForm.getAcroForm(doc.getPdfDocument(), true);
        //Create text field
        PdfTextFormField nameField = PdfFormField.createText(doc.getPdfDocument(),
                new Rectangle(99, 753, 425, 15), "name", "");

        form.addField(nameField);
        return form;

    }
}

我修改了您的代码,如下所示:

public static PdfAcroForm addAcroForm(PdfDocument pdf, Document doc) throws IOException {
    Paragraph title = new Paragraph("Test Form")
            .setTextAlignment(TextAlignment.CENTER)
            .setFontSize(16);
    doc.add(title);
    doc.add(new Paragraph("Full name:").setFontSize(12));

    PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
    PdfTextFormField nameField = PdfFormField.createText(pdf,
            new Rectangle(99, 525, 425, 15), "name", "");
    form.addField(nameField, pdf.getPage(1));
    return form;
}
您将注意到两个变化:

  • 我更改字段的Y偏移量(
    525
    而不是
    753
    )。现在,该字段添加到页面的可见区域内。在代码中,添加了字段,但它不可见
  • 我通过添加
    pdf.getPage(1)
    作为
    addField()
    方法的第二个参数,定义了需要将字段添加到哪个页面

  • 首先:感谢您提供示例代码。当人们给我们一个可以编译测试的样本时,回答问题就容易多了。我已经尝试了你的例子,但我无法重现这个问题:当我尝试你的代码时,字段被正确添加。我使用的是iText 7.0.1和一个页面大小为A4的源文件。我们可以看一下你的源文件吗。可能页面大小的定义不同。如果坐标系的原点(
    (0,0)
    )不在左下角,您可能会遇到您描述的问题。@BrunoLowagie感谢您的回答。这是一个示例文件,看到什么会让它失效吗@布鲁诺洛瓦吉是一个伟大的创意。我在玩x和y坐标,我想我只是把小部件放在了范围之外。非常感谢!期待更多地使用该工具。X和Y坐标确实是一个问题:您的页面高度为612个用户单位,但您在Y=753处添加了字段。还有第二个问题:该字段添加到第7页,您可能希望将其添加到第1页。除了跳转开始教程示例,我还没有为iText 7编写那么多表单示例,我将看看是否可以找到在第1页添加字段的方法。这是解决方案,谢谢。我有1个其他问题,我将提交,并将感谢您的知识。我无法使用引用相同表单值的两个小部件。我只在将表单值更改为单独时看到这两个小部件。