Embedded files

This is a code example of iText PDF, discover more.

31st May 2016
iText PDF

Switch code for this example

AddEmbeddedFile.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/26648462/how-to-delete-attachment-of-pdf-using-itext
  11.  * This is part one, there's also a part two named RemoveEmbeddedFile (or RemoveEmbeddedFiles)
  12.  */
  13. package com.itextpdf.samples.sandbox.annotations;
  14.  
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfReader;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.filespec.PdfFileSpec;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.FileOutputStream;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. @Category(SampleTest.class)
  29. public class AddEmbeddedFile extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/annotations/add_embedded_file.pdf";
  31.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new AddEmbeddedFile().manipulatePdf(DEST);
  37.     }
  38.  
  39.     @Override
  40.     protected void manipulatePdf(String dest) throws Exception {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  42.         PdfFileSpec spec = PdfFileSpec.createEmbeddedFileSpec(pdfDoc, "Some test".getBytes(), null, "test.txt", null, null, null, true);
  43.         pdfDoc.addFileAttachment("some_test", spec);
  44.         pdfDoc.close();
  45.     }
  46. }
AddEmbeddedFiles.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/27823189/add-multiple-attachments-in-a-pdf-using-itext-pdf-stamper
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfReader;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.kernel.pdf.filespec.PdfFileSpec;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.FileOutputStream;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. @Category(SampleTest.class)
  28. public class AddEmbeddedFiles extends GenericTest {
  29.     public static final String[] ATTACHMENTS = {
  30.             "hello", "world", "what", "is", "up"
  31.     };
  32.     public static final String DEST = "./target/test/resources/sandbox/annotations/add_embedded_files.pdf";
  33.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new AddEmbeddedFiles().manipulatePdf(DEST);
  39.     }
  40.  
  41.     @Override
  42.     protected void manipulatePdf(String dest) throws Exception {
  43.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  44.         for (String s : ATTACHMENTS) {
  45.             PdfFileSpec spec = PdfFileSpec.createEmbeddedFileSpec(pdfDoc,
  46.                     String.format("Some test: %s", s).getBytes(), null, String.format("%s.txt", s), null, null, null, true);
  47.             pdfDoc.addFileAttachment(String.format("Some test: %s", s), spec);
  48.         }
  49.         pdfDoc.close();
  50.     }
  51. }
RemoveEmbeddedFile.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/26648462/how-to-delete-attachment-of-pdf-using-itext
  11.  * (This is part two, there's also a part one named AddEmbeddedFile)
  12.  */
  13. package com.itextpdf.samples.sandbox.annotations;
  14.  
  15. import com.itextpdf.kernel.pdf.*;
  16. import com.itextpdf.samples.GenericTest;
  17. import com.itextpdf.test.annotations.type.SampleTest;
  18.  
  19. import org.junit.experimental.categories.Category;
  20.  
  21. import java.io.File;
  22.  
  23. @Category(SampleTest.class)
  24. public class RemoveEmbeddedFile extends GenericTest {
  25.     public static final String DEST = "./target/test/resources/sandbox/annotations/remove_embedded_file.pdf";
  26.     public static final String SRC = "./src/test/resources/pdfs/hello_with_attachment.pdf";
  27.  
  28.     public static void main(String[] args) throws Exception {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new RemoveEmbeddedFile().manipulatePdf(DEST);
  32.     }
  33.  
  34.     @Override
  35.     protected void manipulatePdf(String dest) throws Exception {
  36.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  37.         PdfDictionary root = pdfDoc.getCatalog().getPdfObject();
  38.         PdfDictionary names = root.getAsDictionary(PdfName.Names);
  39.         PdfDictionary embeddedFiles = names.getAsDictionary(PdfName.EmbeddedFiles);
  40.         PdfArray namesArray = embeddedFiles.getAsArray(PdfName.Names);
  41.         namesArray.remove(0);
  42.         namesArray.remove(0);
  43.         pdfDoc.close();
  44.     }
  45. }
RemoveEmbeddedFiles.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/26648462/how-to-delete-attachment-of-pdf-using-itext
  11.  * (This is part two, there's also a part one named AddEmbeddedFile)
  12.  */
  13. package com.itextpdf.samples.sandbox.annotations;
  14.  
  15. import com.itextpdf.kernel.pdf.*;
  16. import com.itextpdf.samples.GenericTest;
  17. import com.itextpdf.test.annotations.type.SampleTest;
  18.  
  19. import org.junit.experimental.categories.Category;
  20.  
  21. import java.io.File;
  22.  
  23. @Category(SampleTest.class)
  24. public class RemoveEmbeddedFiles extends GenericTest {
  25.     public static final String DEST = "./target/test/resources/sandbox/annotations/remove_embedded_files.pdf";
  26.     public static final String SRC = "./src/test/resources/pdfs/hello_with_attachment.pdf";
  27.  
  28.     public static void main(String[] args) throws Exception {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new RemoveEmbeddedFiles().manipulatePdf(DEST);
  32.     }
  33.  
  34.     @Override
  35.     protected void manipulatePdf(String dest) throws Exception {
  36.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  37.         PdfDictionary root = pdfDoc.getCatalog().getPdfObject();
  38.         PdfDictionary names = root.getAsDictionary(PdfName.Names);
  39.         names.remove(PdfName.EmbeddedFiles);
  40.         pdfDoc.close();
  41.     }
  42. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now