pdfbox与表格合并pdf

时间:2014-06-24 11:53:31

标签: java pdf pdfbox

我有一个简单的pdf文件(a.pdf)和一个包含一些表格的pdf文件(checkboxes,textfields,...)。我想合并它们:

    public void merge () throws IOException, COSVisitorException, CryptographyException, InvalidPasswordException {
    PDFMergerUtility merger = new PDFMergerUtility();
    PDDocument arg0 = PDDocument.load("C:\\Users\\...\\Desktop\\a.pdf");
    PDDocument arg1 = PDDocument.load("C:\\Users\\...\\Desktop\\b.pdf");
    if (arg1.isEncrypted()) {
        arg1.decrypt("consol");
        arg1.setAllSecurityToBeRemoved(true);
    }

    merger.appendDocument(arg0, arg1);
    arg0.save("C:\\Users\\...\\Desktop\\xout.pdf");
    arg0.close();
    arg1.close();
}

这基本上有效,但表格不再包含在目标pdf文件中。你有什么提示吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

try { 
        PDFMergerUtility mpdf = new PDFMergerUtility();
        String folder = "C:/Users/.../Desktop"; 
        String destinationFileName = "output.pdf"; //say
        File folderFile = new File(folder); 
        if (folderFile.isDirectory()) { 
              mpdf.addSource(folder + "/" + "a.pdf");
              mpdf.addSource(folder+ "/"+"b.pdf"); 
              mpdf.setDestinationFileName(folder + "/"+ destinationFileName);
              mpdf.mergeDocuments();
       } catch(Exception ex) {
           logger.error(ex);
       }

希望,这会有所帮助......