在PDF Java中创建表单数据

在PDF Java中创建表单数据,java,pdf,Java,Pdf,我想将一个对象的详细信息提取到PDF文件中,还想在导出的PDF文件中有一个工具来修改/编辑数据 例如:尝试将学生对象详细信息导出为PDF格式,如第一行的显示名称、第二行的年龄等,一旦导出数据,我也应该能够对其进行编辑(如更改检索到的年龄) 起初,我想在这个应用程序中创建一个GUI,显示数据和用户可以在执行任何操作(如打印、邮寄等)之前进行编辑,但希望避免创建GUI,并直接在那里处理导出的PDF 我对这个领域很陌生,曾尝试在谷歌上搜索,但找不到正确的解决方案 问题中的对象是一个java对象,我试图

我想将一个对象的详细信息提取到PDF文件中,还想在导出的PDF文件中有一个工具来修改/编辑数据

例如:尝试将学生对象详细信息导出为PDF格式,如第一行的显示名称、第二行的年龄等,一旦导出数据,我也应该能够对其进行编辑(如更改检索到的年龄)

起初,我想在这个应用程序中创建一个GUI,显示数据和用户可以在执行任何操作(如打印、邮寄等)之前进行编辑,但希望避免创建GUI,并直接在那里处理导出的PDF

我对这个领域很陌生,曾尝试在谷歌上搜索,但找不到正确的解决方案

问题中的对象是一个java对象,我试图在PDF文件中查看这个java对象的详细信息


任何建议都会对我大有帮助。

你看过Jasper报告了吗:


有了这个库,你可以定义PDF的模板,你可以决定你应该把你想要的所有信息放在什么地方。我使用过它,它很好地生成了报告。

听起来您试图做的是将Java对象序列化并反序列化为PDF格式。这是直截了当的,但这不是一个小的工作量

忽略XFA,在PDF中,表单是字段的层次结构树。树中的节点有名称,可能也有向下投影的值(如字段类型或默认值)。叶子都由小部件注释表示

PDF中的主要字段类型有:

  • 正文
  • 钮扣
  • 选择
  • 签名
从这里可以直接映射以下类型:

  • 数字的
  • 布尔值
  • 枚举
  • 枚举集
可以执行更复杂的类型,如Date,但没有本机支持,您需要依赖Adobe提供的未记录js库

如果您的类型只包含这些,那么它是可行的。和都可以作为后端使用,但您需要对PDF有相当好的理解才能使用iText,并且至少对JoltImage有初步的了解。例如,要在JoltImage中制作一组单选按钮,可以执行以下操作:

    // make a document
    PdfGeneratedDocument doc = new PdfGeneratedDocument();
    // add a form
    doc.setForm(new PdfForm());
    // make a page
    PdfGeneratedPage page = doc.addPage(PdfDefaultPages.letter());
    // create a font resource
    String font = doc.getResources().getFonts().addFromFontName("Arial");

    String[] values = new String[] { "Yes", "No", "Undecided" };
    PdfBounds[] bounds = new PdfBounds[] {
        new PdfBounds(72, 700, 12, 12),
        new PdfBounds(72, 680, 12, 12),
        new PdfBounds(72, 660, 12, 12)
    };
    // a RadioButtonFormField is a hierarchy of a parent field with a child
    // RadioButtonWidgetAnnotation for each choice.
    // This call, in addition to making all the fields will add the
    // annotations to the page supplied.
    RadioButtonFormField ff = RadioButtonFormField.makeRadioSet(doc.getResources(), page, "Choice", values[0], values[0],
        values, bounds);

    // Add the parent field to the form tree
    doc.getForm().getFields().add(ff);

    // add labels onto the page.
    // labels are not typically part of the widget annotation.
    for (int i = 0; i < values.length; i++)
    {
        page.getDrawingList().add(new PdfTextLine(font, 12, values[i], new PdfPoint(bounds[i].getRight() + 4, bounds[i].getBottom())));
    }
    doc.save("radiobuttons.pdf");
public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader,
            new FileOutputStream(dest));
    AcroFields fields = stamper.getAcroFields();
    fields.setField("student", "Anil Pradhan");
    fields.setField("age", "25");
    stamper.close();
    reader.close();
}
//制作文档
PdfGeneratedDocument doc=新的PdfGeneratedDocument();
//添加表单
doc.setForm(新的PdfForm());
//翻页
PdfGeneratedPage=doc.addPage(PdfDefaultPages.letter());
//创建字体资源
字符串font=doc.getResources().getFonts().addFromFontName(“Arial”);
字符串[]值=新字符串[]{“是”、“否”、“未决定”};
PdfBounds[]边界=新PdfBounds[]{
新的PdfBounds(72700,12,12),
新的PdfBounds(72680,12,12),
新PdfBounds(72660,12,12)
};
//RadioButtonFormField是父字段与子字段的层次结构
//RadioButtonWidgetAnnotation用于每个选项。
//此调用,除了使所有字段
//对所提供页面的注释。
RadioButtonFormField ff=RadioButtonFormField.makeRadioSet(doc.getResources(),第页,“选择”,值[0],值[0],
值、边界);
//将父字段添加到表单树
doc.getForm().getFields().add(ff);
//在页面上添加标签。
//标签通常不是小部件注释的一部分。
对于(int i=0;i
下载并阅读第6.3.5节中有关使用OpenOffice(或LibreOffice)创建表单的部分。这是一个模板,您可以在其中为学生的
“姓名”
“年龄”
等定义字段

一旦您有了带有字段
“name”
“age”
的模板,您就可以使用iText按如下方式填写此表单:

    // make a document
    PdfGeneratedDocument doc = new PdfGeneratedDocument();
    // add a form
    doc.setForm(new PdfForm());
    // make a page
    PdfGeneratedPage page = doc.addPage(PdfDefaultPages.letter());
    // create a font resource
    String font = doc.getResources().getFonts().addFromFontName("Arial");

    String[] values = new String[] { "Yes", "No", "Undecided" };
    PdfBounds[] bounds = new PdfBounds[] {
        new PdfBounds(72, 700, 12, 12),
        new PdfBounds(72, 680, 12, 12),
        new PdfBounds(72, 660, 12, 12)
    };
    // a RadioButtonFormField is a hierarchy of a parent field with a child
    // RadioButtonWidgetAnnotation for each choice.
    // This call, in addition to making all the fields will add the
    // annotations to the page supplied.
    RadioButtonFormField ff = RadioButtonFormField.makeRadioSet(doc.getResources(), page, "Choice", values[0], values[0],
        values, bounds);

    // Add the parent field to the form tree
    doc.getForm().getFields().add(ff);

    // add labels onto the page.
    // labels are not typically part of the widget annotation.
    for (int i = 0; i < values.length; i++)
    {
        page.getDrawingList().add(new PdfTextLine(font, 12, values[i], new PdfPoint(bounds[i].getRight() + 4, bounds[i].getBottom())));
    }
    doc.save("radiobuttons.pdf");
public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader,
            new FileOutputStream(dest));
    AcroFields fields = stamper.getAcroFields();
    fields.setField("student", "Anil Pradhan");
    fields.setField("age", "25");
    stamper.close();
    reader.close();
}
在本例中,
src
是指向空模板的路径,
dest
是指向已填写的新创建表单的路径

如果您有
dest
,还可以通过编程方式检索字段的内容。例如:

PdfReader reader = new PdfReader(src);
AcroFields fields = reader.getAcroFields();
String name = fields.getField("name");
String age = fields.getField("age");

就这么简单。你可以找到更多的例子,一定要下载免费电子书,以了解更多关于其他选项的信息。

你找到了什么解决方案?为什么它们不适合你的需要?你如何将字符串直接放入Jasper报告中?根据我的(小)研究Jasper Reports,您无法将字符串直接输入到报表中,您需要先将其作为数据库中的一个字段,然后才能从查询中调用它。@GherbiHicham对于响应太晚感到抱歉,很遗憾,我不再在使用Jasper Reports的项目中工作。但是,您可以使用Jasper将报表与h是一个Java类,在那里你可以为你想要的字段设置值。然后你通过Jasper Reports链接它们。我建议使用Eclipse插件来做这件事,因为它有一个很好的界面来处理Jasper Reports。谢谢你的回复,是的,我使用Jasper report Beans和JRDatasources做了这件事,这给了我不愉快的感觉结果,我提高了对Jasper报告工作原理的理解。