将PDF附加到现有PDF

时间:2013-08-06 20:23:59

标签: java java-ee pdf-generation itext

我想将pdf文件附加到特定位置的现有PDF文件中,并使用大型匿名JAVA api。任何人都可以帮忙,我该怎么办?

1 个答案:

答案 0 :(得分:2)

您可以使用开源库{{3p>

此示例显示如何合并两个PDF。

public class Merge
{
    public static final String  SOURCE_PDF      = "a.pdf";
    public static final String  APPENDED_PDF    = "b.pdf";
    public static final String  MERGED_RESULT   = "c.pdf";

    public static void main(String[] args) throws IOException, DocumentException, SQLException
    {
        PdfReader sourcePdf = new PdfReader(SOURCE_PDF);
        PdfReader appendedPdf = new PdfReader(APPENDED_PDF);

        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(MERGED_RESULT));
        document.open();

        for (PdfReader reader : Arrays.asList(sourcePdf, appendedPdf))
        {
            for (int page = 1; page <= reader.getNumberOfPages(); page++)
            {
                copy.addPage(copy.getImportedPage(reader, page));
            }
            copy.freeReader(reader);
            reader.close();
        }

        document.close();

    }
}

iText复制。

相关问题