合并pdf
Posted 薄荷加冰透心凉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了合并pdf相关的知识,希望对你有一定的参考价值。
合并pdf
pom.xml添加Jar依赖
<!-- pdf处理jar依赖 start -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.12</version>
</dependency>
<!-- pdf处理jar依赖 end -->
<!-- pdf处理jar包依赖 start -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>4.2.1</version>
</dependency>
<!-- pdf处理jar包依赖 end -->
package com.bjpowernode.p2p.admin.pdf;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
/**
* PDF文件合并操作
*
* @author ldd
*
*/
public class PDFMergeUtil {
/**Log4j2日志记录器*/
private static final Logger logger = LogManager.getLogger(PDFMergeUtil.class);
/**
* PDF文件合并操作
*
* @param streamOfPDFFiles
* @param outputStream
* @param paginate
*/
public static void mergePDFs(List<String> inputFileList, String outputFile, boolean paginate) {
logger.info("pdf文件合并......");
//创建新的pdf文档
Document document = new Document();
PdfWriter writer = null;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//创建读取Pdf 文件对象
List<PdfReader> readers = new ArrayList<PdfReader>();
for (String filePath : inputFileList) {
fis = new FileInputStream(filePath);
PdfReader pdfReader = new PdfReader(fis);
readers.add(pdfReader);
}
//读取pdf文件
fos = new FileOutputStream(outputFile);
//生成新的文档
writer = PdfWriter.getInstance(document, fos);
//打开文档
document.open();
//设置字体
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page = null;
int currentPageNumber = 0;
int pageOfCurrentReaderPDF = 0;
Iterator<PdfReader> iteratorPDFReader = readers.iterator();
//依次读取pdf
while (iteratorPDFReader.hasNext()) {
PdfReader pdfReader = iteratorPDFReader.next();
// Create a new page in the target for each source page.
while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
document.newPage();
pageOfCurrentReaderPDF++;
currentPageNumber++; //创建当前页码
page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
cb.addTemplate(page, 0, 0);
// Code for pagination.
if (paginate) {
cb.beginText();
cb.setFontAndSize(bf, 9);//字体
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String.valueOf(currentPageNumber), 290, 50, 0);
cb.endText();
}
}
pageOfCurrentReaderPDF = 0;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (document.isOpen()) {
document.close();
}
if (null != fis) {
fis.close();
}
if (null != fos) {
fos.close();
}
if (null != writer) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 文件合并测试
*
* @param args
* @throws Throwable
*/
public static void main(String[] args) throws Throwable {
List<String> inputFileList = new ArrayList<String>();
inputFileList.add("C:\\dev\\eclipse\\workspace\\p2p-admin\\src\\main\\resources\\sb_loan_contract_borrow.pdf");
inputFileList.add("C:\\dev\\eclipse\\workspace\\p2p-admin\\src\\main\\resources\\sb_loan_contract_borrow.pdf");
String outputFile = "C:\\dev\\eclipse\\workspace\\p2p-admin\\src\\main\\resources\\sb_loan_contract_borrow_merge.pdf";
//合并PDF
PDFMergeUtil.mergePDFs(inputFileList, outputFile, true);
}
}
-------------------------------------------华丽的分割线----------------------------------------------------------
List<String> pdfList = new ArrayList<String>();
pdfList.add(outPutFileName);//公司签章pdf
pdfList.add(outFile_table);//表格pdf
//pdf文件合并
PDFMergeUtil.mergePDFs(pdfList, outFile_final, true);
以上是关于合并pdf的主要内容,如果未能解决你的问题,请参考以下文章
[pdf文件合并软件]pdf合并软件 两个或多个pdf合并成一个pdf文件
[pdf分割合并工具]PDF分割合并工具免费版|PDF分割合并工具 V1.0 绿色免费版 下载