我很久以前寫的還是上大學的時候寫的:https://www.cnblogs.com/LUA123/p/5108007.html ,今天心血來潮決定更新一波。
看了下官網(https://itextpdf.com/en),出來個IText 7,但是這個要收費的,怎麼收費我也不清楚,爲了避免不必要的麻煩,還是用IText5版本玩了。
正文
首先引入依賴:(目前最新版的)
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.itextpdf.tool/xmlworker --> <dependency> <groupId>com.itextpdf.tool</groupId> <artifactId>xmlworker</artifactId> <version>5.5.13.1</version> </dependency>
示例一:HTML文件轉PDF
web.html
<div style="text-align: center"><b><span style="font-size: large">Terms and Conditions</span></b></div> <ul> <li>Prices are in AED</li> <li>All Credit Card transactions are subject to a 3.25% processing fee</li> <li>In the event production is required per customer request, 50% of the entire bill will be due prior to start of production, and the balance due upon delivery.</li> <li>All furniture will be delivered in A+ condition. In the event that the equipment is damaged, the renter shall be liable for all repair costs to restore the equipment to its state at the beginning of the rental period.</li> <li>Equipment shall be utilized for the stated purpose and at the stated location only.</li> </ul> <ul class="chinese" style="font-family: SimSun;" > <li>價格以迪拉姆爲單位</li> <li>所有信用卡交易都要支付3.25%的手續費</li> <li>如果客戶要求生產,則應支付全部賬單的50%</li> <li>在開始生產之前,以及交貨時的餘額。所有傢俱將以+狀態交付。如果設備損壞,承租人應承擔所有維修費用,以將設備恢復至租期。</li> <li>設備應僅用於規定用途和規定位置。</li> </ul>
web.css
ul li { color: #0ba79c; } .chinese li { color: #ccc920; }
代碼
package com.demo.pdf; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Utilities; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorker; import com.itextpdf.tool.xml.XMLWorkerFontProvider; import com.itextpdf.tool.xml.XMLWorkerHelper; import com.itextpdf.tool.xml.css.CssFile; import com.itextpdf.tool.xml.css.StyleAttrCSSResolver; import com.itextpdf.tool.xml.html.CssAppliers; import com.itextpdf.tool.xml.html.CssAppliersImpl; import com.itextpdf.tool.xml.html.Tags; import com.itextpdf.tool.xml.parser.XMLParser; import com.itextpdf.tool.xml.pipeline.css.CSSResolver; import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline; import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline; import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline; import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.Charset; /** * HTML轉PDF */ public class HTMLAndPDF { public static final String PDF = "pdf/web.pdf"; public static final String PDF2 = "pdf/web2.pdf"; public static final String PDF3 = "pdf/web3.pdf"; public static final String PDF4 = "pdf/web4.pdf"; public static final String HTML = "pdf/web.html"; public static final String CSS = "pdf/web.css"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(PDF); file.getParentFile().mkdirs(); new HTMLAndPDF().createPdf(PDF); file = new File(PDF2); file.getParentFile().mkdirs(); new HTMLAndPDF().createPdf2(PDF2); file = new File(PDF3); file.getParentFile().mkdirs(); new HTMLAndPDF().createPdf3(PDF3); file = new File(PDF4); file.getParentFile().mkdirs(); new HTMLAndPDF().createPdf4(PDF4); } /** * 原封不動轉換 * @param file * @throws IOException * @throws DocumentException */ public void createPdf(String file) throws IOException, DocumentException { // step 1 Document document = new Document(); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); writer.setInitialLeading(12); // step 3 document.open(); // step 4 XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(HTML), Charset.forName("UTF-8")); // step 5 document.close(); } /** * 引入額外的css * @param file * @throws IOException * @throws DocumentException */ public void createPdf2(String file) throws IOException, DocumentException { // step 1 Document document = new Document(); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); writer.setInitialLeading(12); // step 3 document.open(); // step 4 XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(HTML)); String html = Utilities.readFileToString(HTML); String css = "ul { list-style: disc } li { padding: 10px }"; PdfPTable table = new PdfPTable(1); table.setSpacingBefore(20); PdfPCell cell = new PdfPCell(); for (Element e : XMLWorkerHelper.parseToElementList(html, css)) { cell.addElement(e); } table.addCell(cell); document.add(table); // step 5 document.close(); } /** * 引入外部css * @param file * @throws IOException * @throws DocumentException */ public void createPdf3(String file) throws IOException, DocumentException { // step 1 Document document = new Document(); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); writer.setInitialLeading(12.5f); // step 3 document.open(); // step 4 // CSS CSSResolver cssResolver = new StyleAttrCSSResolver(); CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream(CSS)); cssResolver.addCss(cssFile); // HTML HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); // Pipelines PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer); HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); // XML Worker XMLWorker worker = new XMLWorker(css, true); XMLParser p = new XMLParser(worker); p.parse(new FileInputStream(HTML)); // step 5 document.close(); } /** * 處理中文(引入外部字體文件) * @param file * @throws IOException * @throws DocumentException */ public void createPdf4(String file) throws IOException, DocumentException { // step 1 Document document = new Document(); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); writer.setInitialLeading(12.5f); // step 3 document.open(); // step 4 // CSS CSSResolver cssResolver = new StyleAttrCSSResolver(); CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream(CSS)); cssResolver.addCss(cssFile); // HTML XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS); fontProvider.register("pdf/華庚少女字體.ttf", "girl"); // 字體別名,在web.html使用 CssAppliers cssAppliers = new CssAppliersImpl(fontProvider); HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers); htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); // Pipelines PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer); HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); // XML Worker XMLWorker worker = new XMLWorker(css, true); XMLParser p = new XMLParser(worker); p.parse(new FileInputStream(HTML), Charset.forName("UTF-8")); // step 5 document.close(); } }
第一個輸出:
第二個輸出:
第三個輸出:
第四個輸出:
大家可以看到中文的問題,注意點有兩個:html文件指定 font-family;如果引入外部字體文件,別名要與font-family一致。文件:https://github.com/Mysakura/DataFiles
第四個要想成功,需要將web.html文件裏的font-family修改
所以呢,如果你對字體沒啥要求,那font-family就指定常用中文字體即可,宋體,雅黑什麼的
這部分涉及的文件
合併PDF文件 & 嵌入外部字體
提示:如果你運行上面的例子,你可以發現我的輸入輸出文件都在工程根目錄的pdf文件夾裏。
代碼
package com.demo.pdf; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.*; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; /** * 合併文檔 & 嵌入字體 */ public class MergeAndAddFont { public static final String FONT = "pdf/華庚少女字體.ttf"; public static final String[] FILE_A = { "pdf/testA0.pdf", "pdf/testA1.pdf", "pdf/testA2.pdf", "pdf/testA3.pdf" }; public static final String[] CONTENT = { "琪亞娜·卡斯蘭娜", "雷電芽衣", "德麗莎·阿波卡利斯", "希兒·芙樂艾" }; public static final String MERGED_A1 = "pdf/testA_merged1.pdf"; public static final String MERGED_A2 = "pdf/testA_merged2.pdf"; public static final String MERGED_A3 = "pdf/testA_merged3.pdf"; public static void main(String[] args) throws DocumentException, IOException { File file = new File(MERGED_A1); file.getParentFile().mkdirs(); MergeAndAddFont app = new MergeAndAddFont(); for (int i = 0; i < FILE_A.length; i++) { app.createPdf(FILE_A[i], CONTENT[i], BaseFont.EMBEDDED, true); } app.mergeFiles(FILE_A, MERGED_A1,false); app.mergeFiles(FILE_A, MERGED_A2, true); app.embedFont(MERGED_A1, FONT, MERGED_A3); } public void createPdf(String filename, String text, boolean embedded, boolean subset) throws DocumentException, IOException { // step 1 Document document = new Document(); // step 2 PdfWriter.getInstance(document, new FileOutputStream(filename)); // step 3 document.open(); // step 4 BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, embedded); bf.setSubset(subset); Font font = new Font(bf, 12); document.add(new Paragraph(text, font)); // step 5 document.close(); } /** * 合併文件 * @param files * @param result * @param smart * @throws IOException * @throws DocumentException */ public void mergeFiles(String[] files, String result, boolean smart) throws IOException, DocumentException { Document document = new Document(); PdfCopy copy; if (smart) copy = new PdfSmartCopy(document, new FileOutputStream(result)); else copy = new PdfCopy(document, new FileOutputStream(result)); document.open(); PdfReader[] reader = new PdfReader[4]; for (int i = 0; i < files.length; i++) { reader[i] = new PdfReader(files[i]); copy.addDocument(reader[i]); copy.freeReader(reader[i]); reader[i].close(); } document.close(); } /** * 嵌入字體 * @param merged * @param fontfile * @param result * @throws IOException * @throws DocumentException */ private void embedFont(String merged, String fontfile, String result) throws IOException, DocumentException { // the font file RandomAccessFile raf = new RandomAccessFile(fontfile, "r"); byte fontbytes[] = new byte[(int)raf.length()]; raf.readFully(fontbytes); raf.close(); // create a new stream for the font file PdfStream stream = new PdfStream(fontbytes); stream.flateCompress(); stream.put(PdfName.LENGTH1, new PdfNumber(fontbytes.length)); // create a reader object PdfReader reader = new PdfReader(merged); int n = reader.getXrefSize(); PdfObject object; PdfDictionary font; PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(result)); PdfName fontname = new PdfName(BaseFont.createFont(fontfile, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED).getPostscriptFontName()); for (int i = 0; i < n; i++) { object = reader.getPdfObject(i); if (object == null || !object.isDictionary()) continue; font = (PdfDictionary)object; if (PdfName.FONTDESCRIPTOR.equals(font.get(PdfName.TYPE)) && fontname.equals(font.get(PdfName.FONTNAME))) { PdfIndirectObject objref = stamper.getWriter().addToBody(stream); font.put(PdfName.FONTFILE2, objref.getIndirectReference()); } } stamper.close(); reader.close(); } }
運行之後會生成7個文件。
分別是四個獨立文件和兩個合併文件,一個嵌入字體文件
條形碼
package com.demo.pdf; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * 條形碼 */ public class SmallTable { public static final String FONT = "pdf/華庚少女字體.ttf"; public static final String DEST = "pdf/small_table.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new SmallTable().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Rectangle small = new Rectangle(290,100); BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font font = new Font(bf, 12); Document document = new Document(small, 5, 5, 5, 5); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table = new PdfPTable(2); table.setTotalWidth(new float[]{ 160, 120 }); table.setLockedWidth(true); PdfContentByte cb = writer.getDirectContent(); // first row PdfPCell cell = new PdfPCell(new Phrase("條形碼", font)); cell.setFixedHeight(30); cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(2); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); table.addCell(cell); // second row Barcode128 code128 = new Barcode128(); code128.setCode("14785236987541"); code128.setCodeType(Barcode128.CODE128); Image code128Image = code128.createImageWithBarcode(cb, null, null); cell = new PdfPCell(code128Image, true); cell.setColspan(2); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setBorder(Rectangle.NO_BORDER); cell.setFixedHeight(30); table.addCell(cell); document.add(table); document.close(); } }
輸出:
單元格以圖片爲背景
package com.demo.pdf; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * 單元格圖片背景 */ public class ImageBackground { class ImageBackgroundEvent implements PdfPCellEvent { protected Image image; public ImageBackgroundEvent(Image image) { this.image = image; } public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { try { PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS]; image.scaleAbsolute(position); image.setAbsolutePosition(position.getLeft(), position.getBottom()); cb.addImage(image); } catch (DocumentException e) { throw new ExceptionConverter(e); } } } public static final String DEST = "pdf/image_background.pdf"; public static final String IMG1 = "pdf/bruno.jpg"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new ImageBackground().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table = new PdfPTable(1); table.setTotalWidth(400); table.setLockedWidth(true); PdfPCell cell = new PdfPCell(); Font font = new Font(Font.FontFamily.HELVETICA, 12, Font.NORMAL, GrayColor.GRAYWHITE); Paragraph p = new Paragraph("A cell with an image as background.", font); cell.addElement(p); Image image = Image.getInstance(IMG1); cell.setCellEvent(new ImageBackgroundEvent(image)); cell.setFixedHeight(600 * image.getScaledHeight() / image.getScaledWidth()); table.addCell(cell); document.add(table); document.close(); } }
輸出:
小示例(本人不才,簡單畫了畫)
package com.demo.pdf; import com.itextpdf.text.*; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; /** * 一些示例 */ public class Tables { public static final String FONT = "pdf/華庚少女字體.ttf"; public static final String DEST = "pdf/tables.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new Tables().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); // 使用語言包字體 BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); // 外部字體 BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font titleFont = new Font(bf, 12, Font.BOLD); titleFont.setColor(new BaseColor(76,175,80)); Font font = new Font(bf, 10); Paragraph p = new Paragraph("個人簡歷表", new Font(abf, 12, Font.BOLD)); p.setAlignment(Paragraph.ALIGN_CENTER); document.add(p); PdfPTable table = new PdfPTable(8); table.setSpacingBefore(16f); PdfPCell cell = new PdfPCell(new Phrase("姓名", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Phrase("德麗莎", font)); table.addCell(cell); cell = new PdfPCell(new Phrase("性別", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Phrase("女", font)); table.addCell(cell); cell = new PdfPCell(new Phrase("出生年月", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Phrase("3月28日", font)); table.addCell(cell); cell = new PdfPCell(new Phrase("民族", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Phrase("天命→休伯利安", font)); table.addCell(cell); cell = new PdfPCell(new Phrase("個人簡歷", titleFont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell); cell = new PdfPCell(); cell.setColspan(7); cell.addElement(new Paragraph("卡斯蘭娜家族一貫的白髮藍眸,長髮紮成馬尾披於左肩。常穿天命早期使用的女武神統一制服。德麗莎所穿的制服是特別定製款,由主教奧托親手縫製,在衣服胸口處彆着阿波卡利斯家族的家徽。", font)); table.addCell(cell); cell = new PdfPCell(new Phrase("家庭成員", titleFont)); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell.setRowspan(3); table.addCell(cell); cell = new PdfPCell(new Phrase("稱呼", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Phrase("姓名", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Phrase("關係", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setColspan(5); table.addCell(cell); cell = new PdfPCell(new Phrase("爺爺", font)); table.addCell(cell); cell = new PdfPCell(new Phrase("奧托·阿波卡利斯", font)); table.addCell(cell); cell = new PdfPCell(); cell.addElement(new Paragraph("德麗莎的造物主,奧托賜予了德麗莎名字,認其爲孫女。因爲德麗莎溫柔而又堅強的性格,讓奧托多次產生德麗莎是卡蓮的轉世的想法", font)); cell.setColspan(5); table.addCell(cell); cell = new PdfPCell(new Phrase("侄女", font)); table.addCell(cell); cell = new PdfPCell(new Phrase("琪亞娜·卡斯蘭娜", font)); table.addCell(cell); cell = new PdfPCell(); cell.addElement(new Paragraph("塞西莉亞和齊格飛的女兒,兩人讓德麗莎當琪亞娜的教母,琪亞娜這個名字也是德麗莎起的。齊格飛逃離天命的行動中,本想一起逃離天命的德麗莎爲保護備天命回收的琪亞娜,回到天命。", font)); cell.setColspan(5); table.addCell(cell); cell = new PdfPCell(new Phrase("他人評價", titleFont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell); cell = new PdfPCell(); cell.setColspan(7); cell.setPaddingLeft(8f); cell.setPaddingRight(8f); cell.setPaddingBottom(16f); // 配置行間距 cell.addElement(new Paragraph(24, "“即使離開了天命,您也依然是我們所尊敬的學園長。”——雷電芽衣\n" + "“雖然看起來很小,倒也有點本事。”——西琳 \n" + "“誒~德麗莎看起來小小的,意外地很能幹嘛。”——蘿莎莉婭·阿琳", font)); table.addCell(cell); cell = new PdfPCell(new Phrase("其它", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell); cell = new PdfPCell(new Phrase("···")); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setFixedHeight(32f); cell.setColspan(7); table.addCell(cell); document.add(table); // ------------------------------------------------ table = new PdfPTable(4); table.setSpacingBefore(32f); Font titleFont2 = new Font(bf, 14, Font.BOLD); titleFont2.setColor(new BaseColor(255,255,255)); cell = new PdfPCell(new Phrase("賬單", titleFont2)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setColspan(4); cell.setFixedHeight(32f); cell.setBackgroundColor(new BaseColor(96, 125, 139)); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); cell = new PdfPCell(new Phrase("日期", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Phrase("項目", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Phrase("金額", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Phrase("說明", titleFont)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); LocalDateTime dt = LocalDateTime.now(); for (int i = 0; i < 10; i++){ dt = dt.plusDays(1L); cell = new PdfPCell(new Phrase(dt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")), font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setPadding(8f); table.addCell(cell); cell = new PdfPCell(new Phrase("花唄還款", font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setPadding(8f); table.addCell(cell); cell = new PdfPCell(new Phrase("$100.0", font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setPadding(8f); table.addCell(cell); cell = new PdfPCell(new Phrase("——", font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setPadding(8f); table.addCell(cell); } document.add(table); // ------------------------------------------------ document.newPage(); document.add(new Paragraph("芒種\n\n" + "一想到你我就\n" + "wu~~~~\n" + "空恨別夢久\n" + "wu~~~~\n" + "燒去紙灰埋煙柳\n" + "\n" + "於鮮活的枝丫\n" + "凋零下的無暇\n" + "是收穫謎底的代價\n" + "\n" + "餘暉沾上 遠行人的發\n" + "他灑下手中牽掛\n" + "於橋下\n" + "前世遲來者~~~(擦肩而過)\n" + "掌心刻~~~~~(來生記得)\n" + "你眼中煙波滴落一滴墨 wo~~~\n" + "若佛說~~~~~(無牽無掛)\n" + "放下執着~~~~~(無相無色)\n" + "我怎能 波瀾不驚 去附和\n" + "\n" + "一想到你我就\n" + "wu~~~~~\n" + "恨情不壽 總於苦海囚\n" + "wu~~~~~\n" + "新翠徒留 落花影中游\n" + "wu~~~~~\n" + "相思無用 才笑山盟舊\n" + "wu~~~~~\n" + "謂我何求\n" + "\n" + "種一萬朵蓮花\n" + "在衆生中發芽\n" + "等紅塵一萬種解答\n" + "\n" + "念珠落進 時間的泥沙\n" + "待 割捨詮釋慈悲\n" + "的讀法\n" + "\n" + "前世遲來者~~~(擦肩而過)\n" + "掌心刻~~~~~(來生記得)\n" + "你眼中煙波滴落一滴墨 wo~~~\n" + "若佛說~~~~~(無牽無掛)\n" + "放下執着~~~~~(無相無色)\n" + "我怎能 波瀾不驚 去附和", font)); document.close(); } }
輸出