java动态生成pdf含表格table和 合并两个pdf文件功能

1.首先一样需要maven依赖包:

<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
		<dependency>
		    <groupId>com.itextpdf</groupId>
		    <artifactId>itextpdf</artifactId>
		    <version>5.5.10</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>
2.废话不多说,上代码,直接拿去运行测试:

public static void test1(){//生成pdf
	  BaseFont bf;
          Font font = null;
          try {
              bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H",
                      BaseFont.NOT_EMBEDDED);//创建字体
              font = new Font(bf,12);//使用字体
          } catch (Exception e) {
              e.printStackTrace();
         }
          Document document = new Document();
          try {
              PdfWriter.getInstance(document, new FileOutputStream("E:/测试.pdf"));
              document.open();
              document.add(new Paragraph("就是测试下",font));//引用字体
              document.add(new Paragraph("真的测试下",font));//引用字体
              
              float[] widths = {25f,25f,25f };// 设置表格的列宽和列数 默认是4列  
              PdfPTable table = new PdfPTable(widths);// 建立一个pdf表格  
              table.setSpacingBefore(20f);  
              table.setWidthPercentage(100);// 设置表格宽度为100%  
              
              PdfPCell cell = null;  
              cell = new PdfPCell(new Paragraph("姓名",font));//  
              cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
              cell.setHorizontalAlignment(Element.ALIGN_CENTER);
              table.addCell(cell);
              
              cell = new PdfPCell(new Paragraph("性别",font));//  
              cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
              cell.setHorizontalAlignment(Element.ALIGN_CENTER);
              table.addCell(cell);
              
              cell = new PdfPCell(new Paragraph("身份证号",font));//  
              cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
              cell.setHorizontalAlignment(Element.ALIGN_CENTER);
              table.addCell(cell);

              //以下代码的作用是创建100行数据,其中每行有四列,列依次为 编号 姓名 性别 备注
              for (int i = 1; i <=10; i++) {
                  //设置编号单元格
            	  PdfPCell cell11=new PdfPCell(new Paragraph("aa名媛",font));
            	  PdfPCell cell22=new PdfPCell(new Paragraph("bb女",font));
            	  PdfPCell cell33=new PdfPCell(new Paragraph("cc花姑娘",font));

                  //单元格水平对齐方式
                  cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
                  //单元格垂直对齐方式
                  cell11.setVerticalAlignment(Element.ALIGN_CENTER);

                  cell22.setHorizontalAlignment(Element.ALIGN_CENTER);
                  cell22.setVerticalAlignment(Element.ALIGN_CENTER);

                  cell33.setHorizontalAlignment(Element.ALIGN_CENTER);
                  cell33.setVerticalAlignment(Element.ALIGN_CENTER);


                  table.addCell(cell11);
                  table.addCell(cell22);
                  table.addCell(cell33);
              
              }
              document.add(table);  
              
              document.close();
         } catch (Exception e) {
             System.out.println("file create exception");
         }
          
     }
******************************************************************************************************

以下是合并多个pdf文件功能程序,上代码:

//*********合并  pdfFilenames为文件路径数组,targetFileName为目标pdf路径
	public static void combinPdf(String[] pdfFilenames, String targetFilename) 
			   throws Exception { 
	   PdfReader reader = null; 
	   Document doc = new Document(); 
	   PdfCopy pdfCopy = new PdfCopy(doc, new FileOutputStream(targetFilename)); 
	   int pageCount = 0; 
	   doc.open(); 
	   for (int i = 0; i < pdfFilenames.length; ++i) { 
		   System.out.println(pdfFilenames[i]);
		   reader = new PdfReader(pdfFilenames[i]); 
		   pageCount = reader.getNumberOfPages(); 
		   for (int j = 1; j <= pageCount; ++j) { 
		     pdfCopy.addPage(pdfCopy.getImportedPage(reader, j)); 
		   } 
	   } 
	   doc.close(); 
	}
这里附上测试程序:

public static void main(String[] args) throws InterruptedException {
		String fillTemplate1 = "E:/测试.pdf";
		String fillTemplate2 = "E:/测试.pdf";
		String[] st = {fillTemplate1,fillTemplate2};
		try {
		    combinPdf(st,"E:/合并.pdf");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
				
	}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值