多个pdf流文件拼接
import com.lowagie.text.Document;
import java.util.Iterator;
import java.util.List;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class ConcatPdfUtil {
private ConcatPdfUtil() {
throw new IllegalStateException("ConcatPdfUtil class");
}
private static final Logger LOGGER = LoggerFactory.getLogger(ConcatPdfUtil.class);
public static final int NUMBER_OF_FONTSIZE = 9;
public static final float NUMBER_OF_TEXT_X = 520;
public static final float NUMBER_OF_TEXT_Y= 5;
public static final float NUMBER_OF_ROLATION= 0;
public static final int NUMBER_OF_INIT= 0;
public static void concatPDFs(List<InputStream> pdfs, OutputStream outputStream, boolean paginate) {
Document document = new Document();
InputStream pdf = null;
try {
List<PdfReader> readers = new ArrayList<PdfReader>();
int totalPages = NUMBER_OF_INIT;
Iterator<InputStream> iteratorPDFs = pdfs.iterator();
while (iteratorPDFs.hasNext()) {
pdf = iteratorPDFs.next();
PdfReader pdfReader = new PdfReader(pdf);
readers.add(pdfReader);
totalPages += pdfReader.getNumberOfPages();
}
createPdf( readers, document, outputStream, totalPages, paginate);
if (outputStream != null) {
outputStream.flush();
}
document.close();
outputStream.close();
pdf.close();
} catch (Exception e) {
LOGGER.error("拼接pdf失败",e);
} finally {
if (document.isOpen()){
document.close();
}
for(InputStream inputStream:pdfs){
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException ioe) {
LOGGER.error("ioe输入流关闭失败",ioe);
}
}
try {
if (outputStream != null) {
outputStream.close();
}
if(pdf!=null){
pdf.close();
}
} catch (IOException ioe) {
LOGGER.error("ioe输出流关闭失败",ioe);
}
}
}
private static void createPdf(List<PdfReader> readers,Document document,
OutputStream outputStream,int totalPages,
boolean paginate) throws DocumentException, IOException {
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page;
int currentPageNumber = NUMBER_OF_INIT;
int pageOfCurrentReaderPDF = NUMBER_OF_INIT;
Iterator<PdfReader> iteratorPDFReader = readers.iterator();
while (iteratorPDFReader.hasNext()) {
PdfReader pdfReader = iteratorPDFReader.next();
while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
document.newPage();
pageOfCurrentReaderPDF++;
currentPageNumber++;
page = writer.getImportedPage(pdfReader,
pageOfCurrentReaderPDF);
cb.addTemplate(page, NUMBER_OF_ROLATION, NUMBER_OF_ROLATION);
if (paginate) {
cb.beginText();
cb.setFontAndSize(bf, NUMBER_OF_FONTSIZE);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
+ currentPageNumber + " / " + totalPages, NUMBER_OF_TEXT_X,
NUMBER_OF_TEXT_Y, NUMBER_OF_ROLATION);
cb.endText();
}
}
pageOfCurrentReaderPDF = NUMBER_OF_INIT;
}
}
}
在这里插入代码片
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>4.2.2</version>
<type>jar</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>bcmail-jdk14</artifactId>
<groupId>bouncycastle</groupId>
</exclusion>
<exclusion>
<artifactId>bcprov-jdk14</artifactId>
<groupId>bouncycastle</groupId>
</exclusion>
</exclusions>
</dependency>