Pdfbox - adding pdf embedded File and save the PDDocument to OutputStream does not keep the embedded Files

牧云@^-^@ 提交于 2019-12-24 10:55:18

问题


I'm using Pdfbox (1.8.8) to adding attachments to a pdf. My problem is when one of the attachments is of type .pdf and i'm saving the PDDocument to OutputStream the final pdf document does not include the attachments. If a save the PDDocument to a file instead an OutputStream all works just fine, and if the attachments does not include any pdf, both save to file or OutputStream works fine.

I would like to know if there is any way to add pdf embedded Files and save the PDDocument to OutputStream keeping the attached files in the final pdf that is generated.

The code i'm using is:

 private void insertAttachments(OutputStream out, ArrayList<Attachment> attachmentsResources) {

            final PDDocument doc;
            Boolean hasPdfAttach = false;
            try {
                doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray()));
                // final PDFTextStripper pdfStripper = new PDFTextStripper();
                // final String text = pdfStripper.getText(doc);
                final PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
                final Map embeddedFileMap = new HashMap();
                PDEmbeddedFile embeddedFile;
                File file = null;

                for (Attachment attach : attachmentsResources) {

                    // first create the file specification, which holds the embedded file
                    final PDComplexFileSpecification fileSpecification = new PDComplexFileSpecification();
                    fileSpecification.setFile(attach.getFilename());
                    file = AttachmentUtils.getAttachmentFile(attach);
                    final InputStream is = new FileInputStream(file.getAbsolutePath());

                    embeddedFile = new PDEmbeddedFile(doc, is);
                    // set some of the attributes of the embedded file
                    if ("application/pdf".equals(attach.getMimetype())) {
                        hasPdfAttach = true;
                    }
                    embeddedFile.setSubtype(attach.getMimetype());
                    embeddedFile.setSize((int) (long) attach.getFilesize());
                    fileSpecification.setEmbeddedFile(embeddedFile);

                    // now add the entry to the embedded file tree and set in the document.
                    embeddedFileMap.put(attach.getFilename(), fileSpecification);
                    // final String text2 = pdfStripper.getText(doc);
                }
                // final String text3 = pdfStripper.getText(doc);
                efTree.setNames(embeddedFileMap);
                // ((COSDictionary) efTree.getCOSObject()).removeItem(COSName.LIMITS); (this not work for me)
                // attachments are stored as part of the "names" dictionary in the document catalog
                final PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
                names.setEmbeddedFiles(efTree);
                doc.getDocumentCatalog().setNames(names);
                // final ByteArrayOutputStream pdfboxToDocumentStream = new ByteArrayOutputStream();
                final String tmpfile = "temporary.pdf";
                if (hasPdfAttach) {
                    final File f = new File(tmpfile);
                    doc.save(f);
                    doc.close();
                     //i have try with parser but without success too
                    // PDFParser parser = new PDFParser(new FileInputStream(tmpfile));
                    // parser.parse();
                    // PDDocument doc2 = parser.getPDDocument();
                    final PDDocument doc2 = PDDocument.loadNonSeq(f, new RandomAccessFile(new File(getHomeTMP()
                            + "tempppp.pdf"), "r"));
                    doc2.save(out);
                    doc2.close();
                } else {
                    doc.save(out);
                    doc.close();
                }
                 //that does not work too
                // final InputStream in = new FileInputStream(tmpfile);
                // IOUtils.copy(in, out);
                // out = new FileOutputStream(tmpFile);
                // doc.save (out);

            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

Best regards

Solution:

private void insertAttachments(OutputStream out, ArrayList<Attachment> attachmentsResources) {

    final PDDocument doc;
    try {
        doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray()));
        ((ByteArrayOutputStream) out).reset();
        final PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
        final Map embeddedFileMap = new HashMap();
        PDEmbeddedFile embeddedFile;
        File file = null;

        for (Attachment attach : attachmentsResources) {

            // first create the file specification, which holds the embedded file
            final PDComplexFileSpecification fileSpecification = new PDComplexFileSpecification();
            fileSpecification.setFile(attach.getFilename());
            file = AttachmentUtils.getAttachmentFile(attach);
            final InputStream is = new FileInputStream(file.getAbsolutePath());

            embeddedFile = new PDEmbeddedFile(doc, is);
            // set some of the attributes of the embedded file
            embeddedFile.setSubtype(attach.getMimetype());
            embeddedFile.setSize((int) (long) attach.getFilesize());
            fileSpecification.setEmbeddedFile(embeddedFile);

            // now add the entry to the embedded file tree and set in the document.
            embeddedFileMap.put(attach.getFilename(), fileSpecification);

        }
        efTree.setNames(embeddedFileMap);
        ((COSDictionary) efTree.getCOSObject()).removeItem(COSName.LIMITS);
        // attachments are stored as part of the "names" dictionary in the document catalog
        final PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
        names.setEmbeddedFiles(efTree);
        doc.getDocumentCatalog().setNames(names);
        ((COSDictionary) efTree.getCOSObject()).removeItem(COSName.LIMITS);
        doc.save(out);
        doc.close();

    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (Exception e2) {
        e2.printStackTrace();
    }
}

回答1:


You store the new PDF after the original PDF in out:

Look at all the uses of out in your method:

private void insertAttachments(OutputStream out, ArrayList<Attachment> attachmentsResources) {
    ...
            doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray()));
    ...
                doc2.save(out);
    ...
                doc.save(out);

So you get as input a ByteArrayOutputStream and take its current content as input (i.e. the ByteArrayOutputStream is not empty but already contains a PDF) and after some processing you append the modified PDF to the ByteArrayOutputStream. Depending on the PDF viewer you present this to, you will be shown either the original or the manipulated PDF or a (very correct) error message that the file is garbage.

If you want the ByteArrayOutputStream to contain only the manipulated PDF, simply add

((ByteArrayOutputStream) out).reset();

or (if you are not sure about the state of the stream)

out = new ByteArrayOutputStream();

right after

doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray()));

PS: According to the comments the OP tried the above proposed changes to his code without success.

I cannot run the code as presented in the question because it is not self-contained. Thus, I reduced it to the essentials to get a self-contained test:

@Test
public void test() throws IOException, COSVisitorException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (
            InputStream sourceStream = getClass().getResourceAsStream("test.pdf");
            InputStream attachStream = getClass().getResourceAsStream("artificial text.pdf"))
    {
        final PDDocument document = PDDocument.load(sourceStream);

        final PDEmbeddedFile embeddedFile = new PDEmbeddedFile(document, attachStream);
        embeddedFile.setSubtype("application/pdf");
        embeddedFile.setSize(10993);

        final PDComplexFileSpecification fileSpecification = new PDComplexFileSpecification();
        fileSpecification.setFile("artificial text.pdf");
        fileSpecification.setEmbeddedFile(embeddedFile);

        final Map<String, PDComplexFileSpecification> embeddedFileMap = new HashMap<String, PDComplexFileSpecification>();
        embeddedFileMap.put("artificial text.pdf", fileSpecification);

        final PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
        efTree.setNames(embeddedFileMap);

        final PDDocumentNameDictionary names = new PDDocumentNameDictionary(document.getDocumentCatalog());
        names.setEmbeddedFiles(efTree);
        document.getDocumentCatalog().setNames(names);

        document.save(baos);
        document.close();
    }
    Files.write(Paths.get("attachment.pdf"), baos.toByteArray());
}

As you see PDFBox here uses only streams. The result:

Thus, PDFBox without problem stores a PDF into which it has embedded a PDF file attachment.

The problem, therefore, most likely have nothing to do with this work flow as such



来源:https://stackoverflow.com/questions/27983228/pdfbox-adding-pdf-embedded-file-and-save-the-pddocument-to-outputstream-does-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!