如何使用带有 itext 的 pdf 模板导出数据集?
How to export data set using pdf template with itext?
在我的项目中,需要将一些数据集导出为PDF格式。
我了解到 iText 很有用,PdfpTable 可以完成这项工作,但它需要很多代码来处理样式。虽然使用PDF模板可以节省调整样式的时间和代码,但我只能设置模板中遗留的某些字段。
你能给我一些建议来使用 foreach 之类的命令显示数据集吗?提前致谢!
这是我使用 pdfpTable 的代码,它已经完成了工作,但代码有点难看:
PdfPTable pdfTable = createNewPDFTable();
for (int i = 0; i < dataSet.size(); i++) {
MetaObject metaObject = SystemMetaObject.forObject(dataSet.get(i));
for (String field : fields) {
Phrase phrase = new Phrase(String.valueOf(metaObject.getValue(field) != null ? metaObject.getValue(field) : "")
, PDFUtil.createChineseSong(DEFAULT_CELL_FONT_SIZE));
PdfPCell fieldCell = new PdfPCell(phrase);
fieldCell.setBorder(Rectangle.NO_BORDER);
fieldCell.setFixedHeight(DEFAULT_COLUMN_HEIGHT);
fieldCell.setHorizontalAlignment(Element.ALIGN_CENTER);
fieldCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfTable.addCell(fieldCell);
}
}
这里有一些使用 pdfp 模板的代码,是从 itext 示例中复制的,工作尚未完成,因为我还没有找到显示数据集的正确方法。
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
form.setField("text_1", "Bruno Lowagie");
form.setFieldProperty("text_1", "setfflags", PdfFormField.FF_READ_ONLY, null);
你的问题不一致。你写: PdfpTable
可以完成这项工作,但它需要很多代码来处理样式。但是,在您的第一个代码片段中,您并没有真正按照人们期望的方式创建 PDF。您无需生成大量已完成的 PDF,而是使用 PdfPTable
创建模板。我假设您随后会使用该模板创建大量已完成的 PDF。
如果您想使用模板并在之后填充它,则不应使用 iText 创建表单。手动创建它,例如使用 Open Office 或 Libre Office。例如,参见我的书 chapter 6 中的示例(第 6.3.5 节)。使用具有 GUI 的工具创建模板,然后使用 iText 多次填写该模板。
这种方法有一些缺点:所有内容都必须符合您定义的字段。所有字段在固定页面上都有固定位置。
如果 "applying styles through code" 有问题,您可能需要遵循 ZUGFeRD 书中描述的方法。在那本书中,我们首先创建HTML:Creating HTML invoices.
获得 HTML 后,将 HTML 转换为 PDF,并使用 CSS 应用样式:Creating PDF invoices.
这就是我们创建 ZUGFeRDDocument
:
的方式
ZugferdDocument pdfDocument = new ZugferdDocument(
new PdfWriter(fos), ZugferdConformanceLevel.ZUGFeRDComfort,
new PdfOutputIntent("Custom", "", "http://www.color.org",
"sRGB IEC61966-2.1", new FileInputStream(INTENT)));
pdfDocument.addFileAttachment(
"ZUGFeRD invoice", dom.toXML(), "ZUGFeRD-invoice.xml",
PdfName.ApplicationXml, new PdfDictionary(), PdfName.Alternative);
pdfDocument.setTagged();
HtmlConverter.convertToPdf(
new ByteArrayInputStream(html), pdfDocument, getProperties());
getProperties()
方法如下所示:
public ConverterProperties getProperties() {
if (properties == null) {
properties = new ConverterProperties()
.setBaseUri("resources/zugferd/");
}
return properties;
}
您可以在此处找到有关如何将 HTML 用于 PDF 的其他示例:pdfHTML add-on (read the introduction)。
请注意,您使用的是旧版本的 iText。我分享的示例使用的是 iText 7。iText 5 和 iText 7 之间存在巨大差异。
在我的项目中,需要将一些数据集导出为PDF格式。
我了解到 iText 很有用,PdfpTable 可以完成这项工作,但它需要很多代码来处理样式。虽然使用PDF模板可以节省调整样式的时间和代码,但我只能设置模板中遗留的某些字段。
你能给我一些建议来使用 foreach 之类的命令显示数据集吗?提前致谢!
这是我使用 pdfpTable 的代码,它已经完成了工作,但代码有点难看:
PdfPTable pdfTable = createNewPDFTable();
for (int i = 0; i < dataSet.size(); i++) {
MetaObject metaObject = SystemMetaObject.forObject(dataSet.get(i));
for (String field : fields) {
Phrase phrase = new Phrase(String.valueOf(metaObject.getValue(field) != null ? metaObject.getValue(field) : "")
, PDFUtil.createChineseSong(DEFAULT_CELL_FONT_SIZE));
PdfPCell fieldCell = new PdfPCell(phrase);
fieldCell.setBorder(Rectangle.NO_BORDER);
fieldCell.setFixedHeight(DEFAULT_COLUMN_HEIGHT);
fieldCell.setHorizontalAlignment(Element.ALIGN_CENTER);
fieldCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfTable.addCell(fieldCell);
}
}
这里有一些使用 pdfp 模板的代码,是从 itext 示例中复制的,工作尚未完成,因为我还没有找到显示数据集的正确方法。
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
form.setField("text_1", "Bruno Lowagie");
form.setFieldProperty("text_1", "setfflags", PdfFormField.FF_READ_ONLY, null);
你的问题不一致。你写: PdfpTable
可以完成这项工作,但它需要很多代码来处理样式。但是,在您的第一个代码片段中,您并没有真正按照人们期望的方式创建 PDF。您无需生成大量已完成的 PDF,而是使用 PdfPTable
创建模板。我假设您随后会使用该模板创建大量已完成的 PDF。
如果您想使用模板并在之后填充它,则不应使用 iText 创建表单。手动创建它,例如使用 Open Office 或 Libre Office。例如,参见我的书 chapter 6 中的示例(第 6.3.5 节)。使用具有 GUI 的工具创建模板,然后使用 iText 多次填写该模板。
这种方法有一些缺点:所有内容都必须符合您定义的字段。所有字段在固定页面上都有固定位置。
如果 "applying styles through code" 有问题,您可能需要遵循 ZUGFeRD 书中描述的方法。在那本书中,我们首先创建HTML:Creating HTML invoices.
获得 HTML 后,将 HTML 转换为 PDF,并使用 CSS 应用样式:Creating PDF invoices.
这就是我们创建 ZUGFeRDDocument
:
ZugferdDocument pdfDocument = new ZugferdDocument(
new PdfWriter(fos), ZugferdConformanceLevel.ZUGFeRDComfort,
new PdfOutputIntent("Custom", "", "http://www.color.org",
"sRGB IEC61966-2.1", new FileInputStream(INTENT)));
pdfDocument.addFileAttachment(
"ZUGFeRD invoice", dom.toXML(), "ZUGFeRD-invoice.xml",
PdfName.ApplicationXml, new PdfDictionary(), PdfName.Alternative);
pdfDocument.setTagged();
HtmlConverter.convertToPdf(
new ByteArrayInputStream(html), pdfDocument, getProperties());
getProperties()
方法如下所示:
public ConverterProperties getProperties() {
if (properties == null) {
properties = new ConverterProperties()
.setBaseUri("resources/zugferd/");
}
return properties;
}
您可以在此处找到有关如何将 HTML 用于 PDF 的其他示例:pdfHTML add-on (read the introduction)。
请注意,您使用的是旧版本的 iText。我分享的示例使用的是 iText 7。iText 5 和 iText 7 之间存在巨大差异。