Create pdf and merge with pdfbox

后端 未结 4 993
眼角桃花
眼角桃花 2021-02-10 16:03

This is what I want to do:

  1. Make 2 different pdf files using pdfbox

  2. Merge these two files together using pdfmerger

I know how

4条回答
  •  春和景丽
    2021-02-10 16:33

    I use this to merge some documents (InputStreams) and write the merged document in a HttpServletResponse.

      PDFMergerUtility mergedDoc = new PDFMergerUtility();
      ByteArrayOutputStream colDocOutputstream = new ByteArrayOutputStream();
    
      for (int i = 0; i < documentCount; i++)
      {
        ByteArrayOutputStream tempZipOutstream = new ByteArrayOutputStream();
    ...
        mergedDoc.addSource(new ByteArrayInputStream(tempZipOutstream.toByteArray()));
      }
    
      mergedDoc.setDestinationStream(colDocOutputstream);
      mergedDoc.mergeDocuments();
    
      response.setContentLength(colDocOutputstream.size());
      response.setContentType("application/pdf");
      response.setHeader("Content-Disposition", "attachment; filename=mergedDocument.pdf");
      response.setHeader("Pragma", "public");
      response.setHeader("Cache-Control", "max-age=0");
      response.addDateHeader("Expires", 0);
      response.getOutputStream().write(colDocOutputstream.toByteArray());
    

提交回复
热议问题