一个使用itext导出pdf的例子

  1. Document doc =  new  Document (PageSize.A4);  
  2. PdfWriter.getInstance (doc, new  FileOutputStream ( "c:/test/pdf/test.pdf" ));  
  3. doc.open ();  
  4.   
  5. //标题字体   
  6. BaseFont bfTitle = BaseFont.createFont("STSong-Light" ,   
  7.   "UniGB-UCS2-H" , BaseFont.NOT_EMBEDDED);  
  8.   Font titleFont = new  Font(bfTitle,  18 , Font.NORMAL);  
  9.   
  10.    //内容字体   
  11. BaseFont bfComic = BaseFont.createFont("STSong-Light" ,   
  12.   "UniGB-UCS2-H" , BaseFont.NOT_EMBEDDED);  
  13.   Font font = new  Font(bfComic,  9 , Font.NORMAL);  
  14.     
  15. Paragraph titleP=new  Paragraph( "儿童信息 Child Information/n/n" ,titleFont);  
  16. titleP.setAlignment(titleP.ALIGN_CENTER);  
  17. doc.add(titleP);  
  18. //生成4列的表格   
  19. PdfPTable table = new  PdfPTable ( 4 );  
  20. table.setWidthPercentage(100 );  
  21. table.setWidthPercentage(100 );  
  22. table.addCell (new  Paragraph ( "Children-id" ,font));  
  23. PdfPCell cell = new  PdfPCell ( new  Paragraph ( "09140800002" ,font));  
  24. cell.setColspan (3 );  
  25. table.addCell (cell);  
  26. // 添加第一行   
  27. table.addCell (new  Paragraph ( "Name(CN)" ,font));  
  28. table.addCell (new  Paragraph ( "党宁生" ,font));  
  29. table.addCell (new  Paragraph ( "Name(EN)" ,font));  
  30. table.addCell (new  Paragraph ( "DANG NING SHENG" ,font));  
  31.   
  32. //添加第二行   
  33. table.addCell (new  Paragraph ( "Sex(CN)" ,font));  
  34. table.addCell (new  Paragraph ( "男" ,font));  
  35. table.addCell (new  Paragraph ( "Sex(EN)" ,font));  
  36. table.addCell (new  Paragraph ( "MALE" ,font));  
  37. //添加第8行   
  38. table.addCell (new  Paragraph ( "Note" ,font));  
  39. cell = new  PdfPCell ( new  Paragraph ( "儿童资料" ,font));  
  40. cell.setColspan (3 );  
  41. table.addCell (cell);  
  42.   
  43. //添加第9行   
  44. table.addCell (new  Paragraph ( "Pictures" ,font));  
  45. Image photo=Image.getInstance("c:/test/pdf/1246588678828.jpg" );  
  46. cell = new  PdfPCell (photo);  
  47. cell.setColspan (3 );  
  48. table.addCell (cell);  
  49.   
  50. for (PdfPRow row:(ArrayList<PdfPRow>)table.getRows()){  
  51.  for (PdfPCell cells:row.getCells()){  
  52.   if (cells!= null ){  
  53.    cells.setPadding(10 .0f);  
  54.   }  
  55.  }  
  56. }  
  57.   
  58. doc.add (table);  
  59. doc.newPage();  
  60.   
  61. //插入图片   
  62. doc.newPage();  
  63. Image image1 = Image.getInstance ("c:/test/pdf/1246588315281.jpg" );  
  64. image1.setAlignment(image1.ALIGN_CENTER);  
  65. image1.scaleToFit( PageSize.A4.getHeight(),PageSize.A4.getWidth());  
  66. doc.add (image1);  
  67.   
  68. doc.close (); 
### 使用iTextPDF库在Java中导出表格到PDF 为了实现将表格数据导出PDF文件,在Java编程环境中可以利用iTextPDF这一强大的工具库。此库提供了创建和操作PDF文档的能力,其中包括向PDF添加表格的功能。 下面是一个简单的例子来展示如何使用iTextPDF创建带有表格的PDF文档: ```java import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Table; public class ExportTableToPdf { public static void main(String[] args) throws Exception { String dest = "./exported_table.pdf"; PdfWriter writer = new PdfWriter(dest); PdfDocument pdfDoc = new PdfDocument(writer); Document document = new Document(pdfDoc); Table table = new Table(new float[]{1, 2, 3}); table.addCell("Column 1 Header"); table.addCell("Column 2 Header"); table.addCell("Column 3 Header"); // Adding some sample rows into the table. for (int r = 0; r < 5; ++r) { for (int c = 0; c < 3; ++c) { table.addCell("Row " + r + ", Col " + c); } } document.add(table); document.close(); } } ``` 这段代码首先初始化了一个新的`PdfDocument`实例并指定了目标路径用于保存最终生成的PDF文件。接着通过`new Table()`方法定义了一张具有三列宽度不同的表,并设置了各列头部文字以及填充了几行测试数据。最后关闭document对象完成整个过程[^1]。 值得注意的是,上述示例仅展示了基本功能;实际应用时可能还需要考虑更多细节,比如设置字体样式、边框线型等属性以满足特定需求。 #### 关于依赖管理 对于Maven项目来说,可以在pom.xml文件里加入如下配置以便引入最新版本的iText7核心模块作为项目的编译依赖项: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>kernel</artifactId> <version>7.x.x</version><!-- Replace with actual version --> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>layout</artifactId> <version>7.x.x</version><!-- Replace with actual version --> </dependency> ``` 确保替换掉上面占位符中的版本号为当前最新的稳定版。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值