Java开源工具iText在PDF文件中创建表格
来源:互联网 发布:贪吃蛇大战下载软件 编辑:程序博客网 时间:2024/06/04 23:27
上篇文章 Java开源工具iText生成PDF文件,介绍了如何利用IText生成PDF文档,本篇文章将说明如何在PDF中添加Table。
1、代码示例
import com.itextpdf.text.*;import com.itextpdf.text.pdf.PdfPCell;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfWriter;import java.io.FileNotFoundException;import java.io.FileOutputStream;/** * 使用iText生成PDF文件 * 在PDF文件中创建表格 */public class CreateTablePdf { /** * Creates a table; widths are set with setWidths(). * * @return a PdfPTable * @throws DocumentException */ public static PdfPTable createTable1() throws DocumentException { PdfPTable table = new PdfPTable(3); table.setWidthPercentage(288 / 5.23f); table.setWidths(new int[] { 2, 1, 1 }); PdfPCell cell; cell = new PdfPCell(new Phrase("Table 1")); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Phrase("Cell with rowspan 2")); cell.setRowspan(2); table.addCell(cell); table.addCell("row 1; cell 1"); table.addCell("row 1; cell 2"); table.addCell("row 2; cell 1"); table.addCell("row 2; cell 2"); return table; } /** * Creates a table; widths are set with setWidths(). * * @return a PdfPTable * @throws DocumentException */ public static PdfPTable createTable2() throws DocumentException { PdfPTable table = new PdfPTable(3); table.setTotalWidth(288); table.setLockedWidth(true); table.setWidths(new float[] { 2, 1, 1 }); PdfPCell cell; cell = new PdfPCell(new Phrase("Table 2")); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Phrase("Cell with rowspan 2")); cell.setRowspan(2); table.addCell(cell); table.addCell("row 1; cell 1"); table.addCell("row 1; cell 2"); table.addCell("row 2; cell 1"); table.addCell("row 2; cell 2"); return table; } /** * Creates a table; widths are set in the constructor. * * @return a PdfPTable * @throws DocumentException */ public static PdfPTable createTable3() throws DocumentException { PdfPTable table = new PdfPTable(new float[] { 2, 1, 1 }); table.setWidthPercentage(55.067f); PdfPCell cell; cell = new PdfPCell(new Phrase("Table 3")); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Phrase("Cell with rowspan 2")); cell.setRowspan(2); table.addCell(cell); table.addCell("row 1; cell 1"); table.addCell("row 1; cell 2"); table.addCell("row 2; cell 1"); table.addCell("row 2; cell 2"); return table; } /** * Creates a table; widths are set with special setWidthPercentage() method. * * @return a PdfPTable * @throws DocumentException */ public static PdfPTable createTable4() throws DocumentException { PdfPTable table = new PdfPTable(3); Rectangle rect = new Rectangle(523, 770); table.setWidthPercentage(new float[] { 144, 72, 72 }, rect); PdfPCell cell; cell = new PdfPCell(new Phrase("Table 4")); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Phrase("Cell with rowspan 2")); cell.setRowspan(2); table.addCell(cell); table.addCell("row 1; cell 1"); table.addCell("row 1; cell 2"); table.addCell("row 2; cell 1"); table.addCell("row 2; cell 2"); return table; } /** * Creates a table; widths are set with setTotalWidth(). * * @return a PdfPTable * @throws DocumentException */ public static PdfPTable createTable5() throws DocumentException { PdfPTable table = new PdfPTable(3); table.setTotalWidth(new float[] { 144, 72, 72 }); table.setLockedWidth(true); PdfPCell cell; cell = new PdfPCell(new Phrase("Table 5")); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Phrase("Cell with rowspan 2")); cell.setRowspan(2); table.addCell(cell); table.addCell("row 1; cell 1"); table.addCell("row 1; cell 2"); table.addCell("row 2; cell 1"); table.addCell("row 2; cell 2"); return table; } public static PdfPTable createTable6() throws DocumentException{ PdfPTable table = new PdfPTable(10); table.setTotalWidth(595); //table.setLockedWidth(true); PdfPCell cell; cell = new PdfPCell(new Phrase("Table 6")); cell.setColspan(10); table.addCell(cell); for (int i = 1; i < 100; i++) { cell = new PdfPCell(new Phrase(String.valueOf(i))); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); table.addCell(cell); } return table; } public void createPDF(String filename) { Document document = new Document(PageSize.A4); try { PdfWriter.getInstance(document, new FileOutputStream(filename)); document.addTitle("table of PDF"); document.addAuthor("eff666"); document.addSubject("This is the subject of the PDF file."); document.addKeywords("This is the keyword of the PDF file."); document.open(); PdfPTable table = createTable1(); document.add(table); table = createTable2(); table.setSpacingBefore(5); table.setSpacingAfter(5); document.add(table); table = createTable3(); document.add(table); table = createTable4(); table.setSpacingBefore(5); table.setSpacingAfter(5); document.add(table); table = createTable5(); document.add(table); table = createTable6(); document.add(table); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } finally { document.close(); } } public static void main(String[] args) { CreateTablePdf pdf = new CreateTablePdf(); String filename = "F:\\pdf\\pdf_table_001.pdf"; pdf.createPDF(filename); }}
0 0
- Java开源工具iText在PDF文件中创建表格
- 利用itext创建pdf表格文件(java)
- Java开源工具iText生成PDF文件
- 独家:使用iText JAR在Java中创建PDF
- 如何利用iText在java程序中生成PDF文件
- 如何利用iText在java程序中生成PDF文件
- 如何利用iText在java程序中生成PDF文件
- 利用itext创建PDF表格
- 利用java操作Itext创建pdf文件
- java 通过itext 创建pdf 添加,插入 表格,html格式
- Java iText 生成PDF表格
- iText 工具生产pdf文件
- 使用iText创建PDF文件
- 在springmvc中使用itext生成pdf文件
- 使用iText库创建PDF文件
- Java动态生成PDF的两个开源工具-iText与FOP
- Java动态生成PDF的两个开源工具-iText与FOP
- java 如何在pdf中生成表格
- nginx负载均衡之配置和策略
- Mac系统如何锁屏?(两种方法)
- 合成模式(数据结构)
- Casino Malaysia Deposit 30 free 50 Promotion(casino malaysia, casino malaysia promotion, deposit pro
- ZJOI2017蒟蒻游记2
- Java开源工具iText在PDF文件中创建表格
- QT之GUI学习笔记(十三)----QInputDialog
- pyqt5 QMessageBox小结
- 242. Valid Anagram(C++)
- Spring 整合数据访问技术
- 搭建gtest测试环境 脚本
- ORA-00911 无效字符
- NYOJ 586 疯牛 (二分搜索答案)
- unity3d和建模关系