Embedded files

Tags: embedded filesfile attachment annotationannotationsremove attachmentsattachmentsiText 5

These examples were written in answer to the question How to delete attachments in PDF using iText?

Files: 
/*
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/26648462/how-to-delete-attachment-of-pdf-using-itext
 * This is part one, there's also a part two named RemoveEmbeddedFile (or RemoveEmbeddedFiles)
 */
package sandbox.annotations;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class AddEmbeddedFile {
 
    public static final String SRC = "resources/pdfs/hello.pdf";
    public static final String DEST = "results/annotations/hello_with_attachment.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new AddEmbeddedFile().manipulatePdf(SRC, DEST);
    }
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
                stamper.getWriter(), null, "test.txt", "Some test".getBytes());
        stamper.addFileAttachment("some test file", fs);
        stamper.close();
    }
}
/*
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/27823189/add-multiple-attachments-in-a-pdf-using-itext-pdf-stamper
 */
package sandbox.annotations;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class AddEmbeddedFiles {
 
    public static final String SRC = "resources/pdfs/hello.pdf";
    public static final String DEST = "results/annotations/hello_with_attachments.pdf";
    public static final String[] ATTACHMENTS = {
        "hello", "world", "what", "is", "up"
    };
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new AddEmbeddedFiles().manipulatePdf(SRC, DEST);
    }
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        for (String s : ATTACHMENTS) {
            PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
                stamper.getWriter(), null, String.format("%s.txt", s),
                String.format("Some test: %s", s).getBytes());
            stamper.addFileAttachment(String.format("Content: %s", s), fs);
        }
        stamper.close();
    }
}
/*
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/26648462/how-to-delete-attachment-of-pdf-using-itext
 * (This is part two, there's also a part one named AddEmbeddedFile)
 */
package sandbox.annotations;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class RemoveEmbeddedFile {
 
    public static final String SRC = "resources/pdfs/hello_with_attachment.pdf";
    public static final String DEST = "results/annotations/hello_without_attachment.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RemoveEmbeddedFile().manipulatePdf(SRC, DEST);
    }
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfDictionary root = reader.getCatalog();
        PdfDictionary names = root.getAsDict(PdfName.NAMES);
        PdfDictionary embeddedFiles = names.getAsDict(PdfName.EMBEDDEDFILES);
        PdfArray namesArray = embeddedFiles.getAsArray(PdfName.NAMES);
        namesArray.remove(0);
        namesArray.remove(0);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        stamper.close();
    }
}
/*
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/26648462/how-to-delete-attachment-of-pdf-using-itext
 * (This is part two, there's also a part one named AddEmbeddedFile)
 */
package sandbox.annotations;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class RemoveEmbeddedFiles {
 
    public static final String SRC = "resources/pdfs/hello_with_attachment.pdf";
    public static final String DEST = "results/annotations/hello_without_attachments.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RemoveEmbeddedFiles().manipulatePdf(SRC, DEST);
    }
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfDictionary root = reader.getCatalog();
        PdfDictionary names = root.getAsDict(PdfName.NAMES);
        names.remove(PdfName.EMBEDDEDFILES);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        stamper.close();
    }
}
Resources: 
File nameRaw URLUpdated
hello.pdfhello.pdf2015-10-11 3:52 pm
hello_with_attachment.pdfhello_with_attachment.pdf2015-11-06 4:50 pm