Java PDFBox将PDF文件作为页面插入到另一个PDF文件中

Java PDFBox将PDF文件作为页面插入到另一个PDF文件中,java,pdfbox,smb,Java,Pdfbox,Smb,在java web应用程序中使用PDFBox。 我有一个表示多页图纸或蓝图的PDF文件。还有其他PDF文件表示对父文件的修改。我想将这些页面插入到第1页和第2页之间的父PDF文件中 我找到了一些关于合并和添加页面的文章,但不完全是我在这里需要的 一些指导会很有帮助 到目前为止,我的代码是: try{ InputStream in = null; OutputStream out = null; PD

在java web应用程序中使用PDFBox。 我有一个表示多页图纸或蓝图的PDF文件。还有其他PDF文件表示对父文件的修改。我想将这些页面插入到第1页和第2页之间的父PDF文件中

我找到了一些关于合并和添加页面的文章,但不完全是我在这里需要的

一些指导会很有帮助

到目前为止,我的代码是:

try{

                InputStream in = null;
                OutputStream out = null;
                PDDocument document = null;

                String parentfile = "";
                for(DLDrawingFile dldf: dldwg.getDldrawingfileList()){
                    System.out.println("DocsVisionDrawingList: AcrobatRotateAndMerge_3: " + dldf.getDrawingnumber() + " " + dldf.getTypeID());

                    if(parentfile.equals("") && dldf.getTypeID().equals("DRAWING")){

                        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(attachmentRoot_DOMAIN,attachmentRoot_ID,attachmentRoot_PW);

                        SmbFile file = new SmbFile("smb:" + dldf.getDldrawingfile(), auth);
                        file.connect();

                        File file2 = new File(dldf.getDldrawingfile()); 

                        in = new BufferedInputStream(new SmbFileInputStream(file));

                        document = PDDocument.load(in);

                        parentfile = dldf.getDldrawingfile();
                        System.out.println("DocsVisionDrawingList: AcrobatRotateAndMerge_3: Parent: " + parentfile);
                    }
                    else{
                        System.out.println("DocsVisionDrawingList: AcrobatRotateAndMerge_3: Child: " + dldf.getDldrawingfile());
                        //this is where the child files would be iterated over.

                    }
                }

            }
            catch(Exception e){

            }
警告 一个很好的初学者解决方案。但是,在下面的评论中,MLK提到它是不完整的。此方法的另一个问题是,它仅在最后一位代码时才起作用,因为它关闭了Costream,这将阻止您添加任何未来的文本

我知道这是一个老帖子,但如果有人遇到同样的问题,这里有一个anwser为我工作。Tilman Hausherr的方法对我有效

PDDocument document = new PDDocument();
for (int numberOfPages = 0; numberOfPages < 10; numberOfPages++) {
            //Creating a blank page
            PDPage blankPage = new PDPage();
            //Adding the blank page to the document
            document.addPage(blankPage);
        }
PDPageTree mergePD = document.getPages();
PDDocument doc1 = PDDocument.load(file1);
PDDocument doc2 = PDDocument.load(file2);



//the left argument is the page number of the pdf that is being inserted
   //the right argument is the page number of the original pdf that places the inserted pdf

mergePD.insertAfter(doc1.getPage(0),document.getPage(0)); //creates a new page after page 0
mergePD.insertAfter(doc2.getPage(0),document.getPage(1)); //creates a new page after page 1
PDDocument document=新的PDDocument();
对于(int numberOfPages=0;numberOfPages<10;numberOfPages++){
//创建空白页
PDPage blankPage=新PDPage();
//将空白页添加到文档中
文件。添加页(空白页);
}
PDPageTree mergePD=document.getPages();
PDDocument doc1=PDDocument.load(file1);
PDDocument doc2=PDDocument.load(file2);
//左参数是要插入的pdf的页码
//右参数是放置插入pdf的原始pdf的页码
mergePD.insertAfter(doc1.getPage(0),document.getPage(0))//在第0页之后创建新页
mergePD.insertAfter(doc2.getPage(0),document.getPage(1))//在第1页之后创建新页

您是否尝试过
PDPageTree.insertBefore(PDPage newPage,PDPage nextPage)
?虽然这在某种程度上有效,但这是一个不完整的解决方案,特别是如果源页面包含表单字段或标记内容,因为目标文档的表单信息及其结构树都没有更新。@mkl您是对的。我更新了帖子以发出警告。我仍在考虑保持这一点,因为教程很少,而且学习曲线陡峭。你同意吗?