Embedded files

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

6th November 2015
admin-marketing

Switch code for this example

AddEmbeddedFile.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/26648462/how-to-delete-attachment-of-pdf-using-itext
  4.  * This is part one, there's also a part two named RemoveEmbeddedFile (or RemoveEmbeddedFiles)
  5.  */
  6. package sandbox.annotations;
  7.  
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.PdfFileSpecification;
  10. import com.itextpdf.text.pdf.PdfReader;
  11. import com.itextpdf.text.pdf.PdfStamper;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import sandbox.WrapToTest;
  17.  
  18. @WrapToTest
  19. public class AddEmbeddedFile {
  20.  
  21.     public static final String SRC = "resources/pdfs/hello.pdf";
  22.     public static final String DEST = "results/annotations/hello_with_attachment.pdf";
  23.    
  24.     public static void main(String[] args) throws IOException, DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new AddEmbeddedFile().manipulatePdf(SRC, DEST);
  28.     }
  29.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  30.         PdfReader reader = new PdfReader(src);
  31.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  32.         PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
  33.                 stamper.getWriter(), null, "test.txt", "Some test".getBytes());
  34.         stamper.addFileAttachment("some test file", fs);
  35.         stamper.close();
  36.     }
  37. }
AddEmbeddedFiles.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/27823189/add-multiple-attachments-in-a-pdf-using-itext-pdf-stamper
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfFileSpecification;
  9. import com.itextpdf.text.pdf.PdfReader;
  10. import com.itextpdf.text.pdf.PdfStamper;
  11.  
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import sandbox.WrapToTest;
  16.  
  17. @WrapToTest
  18. public class AddEmbeddedFiles {
  19.  
  20.     public static final String SRC = "resources/pdfs/hello.pdf";
  21.     public static final String DEST = "results/annotations/hello_with_attachments.pdf";
  22.     public static final String[] ATTACHMENTS = {
  23.         "hello", "world", "what", "is", "up"
  24.     };
  25.    
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new AddEmbeddedFiles().manipulatePdf(SRC, DEST);
  30.     }
  31.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  32.         PdfReader reader = new PdfReader(src);
  33.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  34.         for (String s : ATTACHMENTS) {
  35.             PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
  36.                 stamper.getWriter(), null, String.format("%s.txt", s),
  37.                 String.format("Some test: %s", s).getBytes());
  38.             stamper.addFileAttachment(String.format("Content: %s", s), fs);
  39.         }
  40.         stamper.close();
  41.     }
  42. }
RemoveEmbeddedFile.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/26648462/how-to-delete-attachment-of-pdf-using-itext
  4.  * (This is part two, there's also a part one named AddEmbeddedFile)
  5.  */
  6. package sandbox.annotations;
  7.  
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.PdfArray;
  10. import com.itextpdf.text.pdf.PdfDictionary;
  11. import com.itextpdf.text.pdf.PdfName;
  12. import com.itextpdf.text.pdf.PdfReader;
  13. import com.itextpdf.text.pdf.PdfStamper;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class RemoveEmbeddedFile {
  22.  
  23.     public static final String SRC = "resources/pdfs/hello_with_attachment.pdf";
  24.     public static final String DEST = "results/annotations/hello_without_attachment.pdf";
  25.    
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new RemoveEmbeddedFile().manipulatePdf(SRC, DEST);
  30.     }
  31.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  32.         PdfReader reader = new PdfReader(src);
  33.         PdfDictionary root = reader.getCatalog();
  34.         PdfDictionary names = root.getAsDict(PdfName.NAMES);
  35.         PdfDictionary embeddedFiles = names.getAsDict(PdfName.EMBEDDEDFILES);
  36.         PdfArray namesArray = embeddedFiles.getAsArray(PdfName.NAMES);
  37.         namesArray.remove(0);
  38.         namesArray.remove(0);
  39.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  40.         stamper.close();
  41.     }
  42. }
RemoveEmbeddedFiles.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/26648462/how-to-delete-attachment-of-pdf-using-itext
  4.  * (This is part two, there's also a part one named AddEmbeddedFile)
  5.  */
  6. package sandbox.annotations;
  7.  
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.PdfDictionary;
  10. import com.itextpdf.text.pdf.PdfName;
  11. import com.itextpdf.text.pdf.PdfReader;
  12. import com.itextpdf.text.pdf.PdfStamper;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class RemoveEmbeddedFiles {
  21.  
  22.     public static final String SRC = "resources/pdfs/hello_with_attachment.pdf";
  23.     public static final String DEST = "results/annotations/hello_without_attachments.pdf";
  24.    
  25.     public static void main(String[] args) throws IOException, DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new RemoveEmbeddedFiles().manipulatePdf(SRC, DEST);
  29.     }
  30.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  31.         PdfReader reader = new PdfReader(src);
  32.         PdfDictionary root = reader.getCatalog();
  33.         PdfDictionary names = root.getAsDict(PdfName.NAMES);
  34.         names.remove(PdfName.EMBEDDEDFILES);
  35.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  36.         stamper.close();
  37.     }
  38. }
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