Create pdf and merge with pdfbox

后端 未结 4 981
眼角桃花
眼角桃花 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:06

    You can use this way also:-
    1) Create List of InputStream
    2) Instantiate PDFMergerUtility class
    3) Set Destination Output Stream
    4) Add all InputStreams to PDFMerger as Source files which needs to be merged.
    5) Merge the documents by calling "PDFmerger.mergeDocuments();"

       List<InputStream> locations=new ArrayList<InputStream>();
            locations.add(new FileInputStream("E:/Filenet Project Support/MergePDFs_Sample_Code/Attorney_new_form.pdf"));
            locations.add(new FileInputStream("E:/Filenet Project Support/MergePDFs_Sample_Code/JH.pdf"));
            locations.add(new FileInputStream("E:/Filenet Project Support/MergePDFs_Sample_Code/Interpreter_new_form.pdf"));
            //Instantiating PDFMergerUtility class
            PDFMergerUtility PDFmerger = new PDFMergerUtility();
            //Setting Destination Output Stream
            OutputStream out = new FileOutputStream("E:/Filenet Project Support/MergePDFs_Sample_Code/merged.pdf");
            //Adding all InputStreams to PDFMerger as Source files which needs to be merged.
            PDFmerger.addSources(locations);
            //Setting Destination Output Stream
            PDFmerger.setDestinationStream(out);
            //Merging the two documents
            PDFmerger.mergeDocuments();
            System.out.println("Documents merged");
    
    0 讨论(0)
  • 2021-02-10 16:07

    You just need to use the PdfMergeUtility.addSource(InputStream) method to add source from an inputstream and not from a physical file.

    With a quick glance at the API, what you could do is use the PDDocument.save(OutputStream) method to write the file into a byte array in memory, something like this should work.

    static byte[] doIt(String message) {
       PDDocument doc = new PDDocument();
       // add the message
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       doc.save(baos);
       return baos.toByteArray();
    }
    
    void main(String args[]) {
       byte[] pdf1 = doIt("hello");
       byte[] pdf2 = doIt("world");
       PDFMergerUtility merger = new PDFMergerUtility();
       merger.addSource(new ByteArrayInputStream(pdf1));
       merger.addSource(new ByteArrayInputStream(pdf2));
       // do the rest with the merger
    }
    
    0 讨论(0)
  • 2021-02-10 16:20

    Using REST and PDFBOX

    @RequestMapping(value = "/getMergePdf", method = RequestMethod.GET)
        public ResponseEntity<byte[]> getMergePdf(@RequestParam(value = "filePath", required = true) String filePath,
                @RequestParam(value = "newFileName", required = true) String newFileName) throws IOException {
    
                // Step 1: Loading an Existing PDF Document
            File file = new File(filePath);
            File[] listFile = file.listFiles();
    
            // Step 2: Instantiating the PDFMergerUtility class
            PDFMergerUtility mergePdf = new PDFMergerUtility();
    
            // Step 3: Setting the source files
            for (File pdfName : listFile) {
                mergePdf.addSource(pdfName);
            }
    
            // Step 4: Setting the destination file
            ByteArrayOutputStream pdfDocOutputstream = new ByteArrayOutputStream();
            mergePdf.setDestinationFileName(newFileName + ".pdf");
            mergePdf.setDestinationStream(pdfDocOutputstream);
            mergePdf.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());
    
            // Step 5: write in Response
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_PDF);
    
            // Here you have to set the actual filename of your pdf
            headers.setContentDispositionFormData(mergePdf.getDestinationFileName(), mergePdf.getDestinationFileName());
            headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
            ResponseEntity<byte[]> response = new ResponseEntity<>(pdfDocOutputstream.toByteArray(), headers, HttpStatus.OK);
            return response;
    
    
        }
    
    0 讨论(0)
  • 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());
    
    0 讨论(0)
提交回复
热议问题