使用Itext合并PDF

时间:2015-09-25 10:43:52

标签: java itext

我在合并由IText生成的两个PDF时遇到了问题。

PDF创建的代码片段:

文档配置如下:

iText_xls_2_pdf = new Document(PageSize.A4.rotate(),10,0,30,0);

使用此配置可​​创建2个PDF。

创建这些PDF后,需要将它们合并为单个pdf。

下面的代码用于合并PDF:

Document document = new Document(PageSize.A4.rotate(),10,0,30,0);
try {
    List<InputStream> pdfs = streamOfPDFFiles;
    List<PdfReader> readers = new ArrayList<PdfReader>();
    int totalPages = 0;
    Iterator<InputStream> iteratorPDFs = pdfs.iterator();
    // Create Readers for the pdfs.
    while (iteratorPDFs.hasNext()) {
        InputStream pdf = iteratorPDFs.next();
        PdfReader pdfReader = new PdfReader(pdf);
        readers.add(pdfReader);
        totalPages += pdfReader.getNumberOfPages();
    }
    // Create a writer for the outputstream
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    document.open();
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
            BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
    // data
    PdfImportedPage page;
    int currentPageNumber = 0;
    int pageOfCurrentReaderPDF = 0;
    Iterator<PdfReader> iteratorPDFReader = readers.iterator();
    // Loop through the PDF files and add to the output.
    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, ""
                        + currentPageNumber + " of " + totalPages, 520,
                        5, 0);
                cb.endText();
            }
        }
        pageOfCurrentReaderPDF = 0;
    }
    outputStream.flush();
    document.close();
    outputStream.close();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (document.isOpen())
        document.close();
    try {
        if (outputStream != null)
            outputStream.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

这会创建合并的PDF。但因为我在文档配置中使用了.rotate,所以合并的pdf采用横向格式。我怎么能避免这种情况。

1 个答案:

答案 0 :(得分:0)

请查看chapter 6 of my book中的表格6.1,您会发现使用PdfWriter合并文档未完成。如果要合并文档,则需要使用PdfCopy

例如,请参阅以下问题:

(正如你所看到的,这个问题在之前已被多次回答过......)

一个例子:

PdfReader reader1 = new PdfReader(file1);
PdfReader reader2 = new PdfReader(file2);
Document document = new Document();
FileOutputStream fos = new FileOutputStream();
PdfCopy copy = new PdfCopy(document, fs);
document.open();
PdfImportedPage page;
PdfCopy.PageStamp stamp;
Phrase phrase;
BaseFont bf = BaseFont.createFont();
Font font = new Font(bf, 9);
int n = reader1.getNumberOfPages();
for (int i = 1; i <= reader1.getNumberOfPages(); i++) {
    page = copy.getImportedPage(reader1, i);
    stamp = copy.createPageStamp(page);
    phrase = new Phrase("page " + i, font);
    ColumnText.showTextAligned(stamp.getOverContent(), Element.ALIGN_CENTER, phrase, 520, 5, 0);
    stamp.alterContents();
    copy.addPage(page);
}
for (int i = 1; i <= reader2.getNumberOfPages(); i++) {
    page = copy.getImportedPage(reader2, i);
    stamp = copy.createPageStamp(page);
    phrase = new Phrase("page " + (n + i), font);
    ColumnText.showTextAligned(stamp.getOverContent(), Element.ALIGN_CENTER, phrase, 520, 5, 0);
    stamp.alterContents();
    copy.addPage(page);
}
document.close();
reader1.close();
reader2.close();