使用 OpenOffice 和 iText 生成 PDF
jopen
13年前
<p>此文译自<a href="/misc/goto?guid=4959500933853112831" rel="nofollow">这里</a>。</p> <p>之前我接到一个使用模板来动态创建 PDF 文档的任务,最终选择了 iText 和 OpenOffice Draw 来生成模板化的 PDF。</p> <p>下面是介绍如何使用 OpenOffice Draw 创建 PDF 表单</p> <p><a href="https://simg.open-open.com/show/d186ba3c27ee4c2b9088aa93cb842e56.jpg" rel="nofollow"><img title="OpenOfficeDesign1" alt="使用 OpenOffice 和 iText 生成 PDF" src="https://simg.open-open.com/show/acc59d508483176b586846fb59c42ee7.jpg" width="630" height="333" /></a></p> <p><a href="https://simg.open-open.com/show/ffe984325d7b2260101054629352d054.jpg" rel="nofollow"><img title="CreateTextBox" alt="使用 OpenOffice 和 iText 生成 PDF" src="https://simg.open-open.com/show/63e238bd78e010229b90c4c055d594f9.jpg" width="630" height="335" /></a><a href="https://simg.open-open.com/show/54fa570ce05af86683c8709becfcf652.jpg" rel="nofollow"><img title="EditControl" alt="使用 OpenOffice 和 iText 生成 PDF" src="https://simg.open-open.com/show/0852f5152ea4bd44ebe603869c6a5b9b.jpg" width="630" height="335" /></a><a href="https://simg.open-open.com/show/c78dc9d92e0d31db4eb37cdafef552ab.jpg" rel="nofollow"><img title="ChangeProperties" alt="使用 OpenOffice 和 iText 生成 PDF" src="https://simg.open-open.com/show/3f28662ff2f2dc0d4846ae76ca1ba610.jpg" width="630" height="335" /></a><a href="https://simg.open-open.com/show/909c56f5dea2d13efa4a0bbc1dab2896.jpg" rel="nofollow"><img title="ExportPDF" alt="使用 OpenOffice 和 iText 生成 PDF" src="https://simg.open-open.com/show/b377002f8091b67130ba3043b8f62085.jpg" width="630" height="336" /></a></p> <p>Save PDF to folder where you have your program or just change path for input source in program</p> <p><a href="https://simg.open-open.com/show/d35bc1c70ac8ceb20abe6a205d227559.jpg" rel="nofollow"><img title="savePDFFile" alt="使用 OpenOffice 和 iText 生成 PDF" src="https://simg.open-open.com/show/0b477fdb13480935fe194e32d839a02f.jpg" width="630" height="334" /></a></p> <p>然后使用以下代码通过 iText 来填充 PDF 中的变量</p> <pre class="brush:java; toolbar: false; auto-links: false;">package com.linkwithweb.reports; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.DocumentException; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfStamper; /** * @author Ashwin Kumar * */ public class ITextStamperSample { /** * @param args * @throws IOException * @throws DocumentException */ public static void main(String[] args) throws IOException, DocumentException { // createSamplePDF(); generateAshwinFriends(); } /** * */ private static void generateAshwinFriends() throws IOException, FileNotFoundException, DocumentException { PdfReader pdfTemplate = new PdfReader("mytemplate.pdf"); FileOutputStream fileOutputStream = new FileOutputStream("test.pdf"); ByteArrayOutputStream out = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(pdfTemplate, fileOutputStream); stamper.setFormFlattening(true); stamper.getAcroFields().setField("name", "Ashwin Kumar"); stamper.getAcroFields().setField("id", "1\n2\n3\n"); stamper.getAcroFields().setField("friendname", "kumar\nsirisha\nsuresh\n"); stamper.getAcroFields().setField("relation", "self\nwife\nfriend\n"); stamper.close(); pdfTemplate.close(); } }</pre> <p>接下来是 pom.xml</p> <pre class="brush:xml; toolbar: false; auto-links: false;"> <!--?XML:NAMESPACE PREFIX = [default] http://maven.apache.org/POM/4.0.0 NS = "http://maven.apache.org/POM/4.0.0" /--> <project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelversion> 4.0.0 </modelversion> <groupid> com.linkwithweb.reports </groupid> <artifactid> ReportTemplateTutorial </artifactid> <version> 0.0.1-SNAPSHOT </version> <name> ReportTemplateTutorial </name> <description> ReportTemplateTutorial </description> <dependencies> <dependency> <groupid> com.lowagie </groupid> <artifactid> itext </artifactid> <version> 2.1.7 </version> </dependency> </dependencies> </project></pre> <p>最终生成 test.pdf 如下</p> <p><a href="https://simg.open-open.com/show/6254cba2f81a3949c1e75f91e7ed768d.jpg" rel="nofollow"><img title="FinalPDF" alt="使用 OpenOffice 和 iText 生成 PDF" src="https://simg.open-open.com/show/af3b5541f20b8f72ea28828cf910499d.jpg" width="630" height="386" /></a><a href="https://simg.open-open.com/show/ffe984325d7b2260101054629352d054.jpg" rel="nofollow"><br /> </a></p>