合併pdf pom.xml添加Jar依賴 <!-- pdf處理jar依賴 start --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.12</version ...
合併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);