title: Pdfbox - adding pdf embedded File and save the PDDocument to OutputStream does not keep the embedded Files
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>The code i'm using is:</p>
  
  <pre class="lang-js prettyprint-override"><code> private void insertAttachments(OutputStream out, ArrayList&lt;Attachment&gt; 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();
              }
          }
  </code></pre>
  
   <p>Best regards</p>
  
  <p>Solution:</p>
  
  <pre class="lang-js prettyprint-override"><code>private void insertAttachments(OutputStream out, ArrayList&lt;Attachment&gt; 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();
      }
  }
  </code></pre>