Java合并PDF

在网上找的 只是个人记录下来 转载地址 https://blog.csdn.net/qq_30764991/article/details/85235631
一:创建maven项目,加入依赖

com.itextpdf itextpdf 5.4.2 com.itextpdf itext-asian 5.2.0 二:具体实现代码如下:写的很清楚了:2个静态方法,第一个就是生成一个pdf方法,第二个是个合并的方法

package com.demo3;

import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

public class Test1 {
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(“D:\pdfDemo\66.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");
	}

}

// *********合并 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 {
	test1();//调用test1静态方法,生成pdf D:\\pdfDemo\\66.pdf
	String fillTemplate1 = "D:\\pdfDemo\\66.pdf";
	String fillTemplate2 = "D:\\pdfDemo\\66.pdf";
	String[] st = { fillTemplate1, fillTemplate2 };
	try {
		combinPdf(st, "D:\\pdfDemo\\合并2.pdf");

	} catch (Exception e) {
		e.printStackTrace();
	}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值