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();
		}
				
	}



  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用 iText 或者 Apache PDFBox 等 JavaPDF 库来生成图表和表格PDF 文件iText 是一个广泛使用的 Java PDF 库,支持文本、表格、图像、图表等多种元素。你可以使用 iText 中的 PdfPTable 类来生成表格,使用 ChartFactory.createXXXChart() 等方法生成图表,然后将它们添加PDF 中。 以下是使用 iText 生成表格和图表的 PDF 的示例代码: ```java Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); document.open(); // 添加表格 PdfPTable table = new PdfPTable(3); table.addCell("Header 1"); table.addCell("Header 2"); table.addCell("Header 3"); table.addCell("1.1"); table.addCell("1.2"); table.addCell("1.3"); table.addCell("2.1"); table.addCell("2.2"); table.addCell("2.3"); document.add(table); // 添加图表 JFreeChart chart = ChartFactory.createBarChart( "Chart Title", "X Axis", "Y Axis", dataset, PlotOrientation.VERTICAL, true, true, false ); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(400, 300); Graphics2D graphics2D = template.createGraphics(400, 300, new DefaultFontMapper()); Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, 400, 300); chart.draw(graphics2D, rectangle2D); graphics2D.dispose(); contentByte.addTemplate(template, 0, 0); document.newPage(); document.close(); ``` Apache PDFBox 是另一个 Java PDF 库,它也支持多种元素的生成。你可以使用 PDFBox 中的 PDPageContentStream 类来添加表格和图表。 以下是使用 PDFBox 生成表格和图表的 PDF 的示例代码: ```java PDDocument document = new PDDocument(); PDPage page = new PDPage(); document.addPage(page); // 添加表格 PDPageContentStream contentStream = new PDPageContentStream(document, page); PDTable table = new PDTable(); PDPageContentStreamTableDrawer drawer = new PDPageContentStreamTableDrawer(contentStream, table); table.addCell(new PDCell().addParagraph(new PDParagraph("Header 1"))); table.addCell(new PDCell().addParagraph(new PDParagraph("Header 2"))); table.addCell(new PDCell().addParagraph(new PDParagraph("Header 3"))); table.addCell(new PDCell().addParagraph(new PDParagraph("1.1"))); table.addCell(new PDCell().addParagraph(new PDParagraph("1.2"))); table.addCell(new PDCell().addParagraph(new PDParagraph("1.3"))); table.addCell(new PDCell().addParagraph(new PDParagraph("2.1"))); table.addCell(new PDCell().addParagraph(new PDParagraph("2.2"))); table.addCell(new PDCell().addParagraph(new PDParagraph("2.3"))); drawer.drawTable(100, 700, 400, 0); // 添加图表 JFreeChart chart = ChartFactory.createBarChart( "Chart Title", "X Axis", "Y Axis", dataset, PlotOrientation.VERTICAL, true, true, false ); BufferedImage image = chart.createBufferedImage(400, 300); PDImageXObject ximage = LosslessFactory.createFromImage(document, image); contentStream.drawImage(ximage, 100, 400, 400, 300); contentStream.close(); document.save("output.pdf"); document.close(); ``` 这些示例代码仅供参考,具体实现方式还需要根据你的需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值