Chapter 16: PDF streams

These examples were written in the context of Chapter 16 of the book "iText in Action - Second Edition".

Files: 
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
package part4.chapter16;
 
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfImage;
import com.itextpdf.text.pdf.PdfIndirectObject;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfWriter;
 
public class SpecialId {
 
    /** The resulting PDF file. */
    public static String RESULT = "results/part4/chapter16/special_id.pdf";
    /** An image file. */
    public static String RESOURCE = "resources/img/bruno.jpg";
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws    DocumentException 
     * @throws    IOException
     */
    public void createPdf(String filename) throws IOException, DocumentException {
        Document document = new Document(new Rectangle(400, 300));
        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream(filename));
        document.open();
        Image img = Image.getInstance(RESOURCE);
        img.scaleAbsolute(400, 300);
        img.setAbsolutePosition(0, 0);
        PdfImage stream = new PdfImage(img, "", null);
        stream.put(new PdfName("ITXT_SpecialId"), new PdfName("123456789"));
        PdfIndirectObject ref = writer.addToBody(stream);
        img.setDirectReference(ref.getIndirectReference());
        document.add(img);
        document.close();
    }
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new SpecialId().createPdf(RESULT);
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
package part4.chapter16;
 
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
import javax.imageio.ImageIO;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PRStream;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.parser.PdfImageObject;
 
public class ResizeImage {
 
	/** The resulting PDF file. */
    public static String RESULT = "results/part4/chapter16/resized_image.pdf";
    /** The multiplication factor for the image. */
    public static float FACTOR = 0.5f;
 
    /**
     * Manipulates a PDF file src with the file dest as result
     * @param src the original PDF
     * @param dest the resulting PDF
     * @throws IOException
     * @throws DocumentException 
     */
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfName key = new PdfName("ITXT_SpecialId");
        PdfName value = new PdfName("123456789");
        // Read the file
        PdfReader reader = new PdfReader(SpecialId.RESULT);
        int n = reader.getXrefSize();
        PdfObject object;
        PRStream stream;
        // Look for image and manipulate image stream
        for (int i = 0; i < n; i++) {
            object = reader.getPdfObject(i);
            if (object == null || !object.isStream())
                continue;
            stream = (PRStream)object;
            if (value.equals(stream.get(key))) {
                PdfImageObject image = new PdfImageObject(stream);
                BufferedImage bi = image.getBufferedImage();
                if (bi == null) continue;
                int width = (int)(bi.getWidth() * FACTOR);
                int height = (int)(bi.getHeight() * FACTOR);
                BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                AffineTransform at = AffineTransform.getScaleInstance(FACTOR, FACTOR);
                Graphics2D g = img.createGraphics();
                g.drawRenderedImage(bi, at);
                ByteArrayOutputStream imgBytes = new ByteArrayOutputStream();
                ImageIO.write(img, "JPG", imgBytes);
                stream.clear();
                stream.setData(imgBytes.toByteArray(), false, PRStream.NO_COMPRESSION);
                stream.put(PdfName.TYPE, PdfName.XOBJECT);
                stream.put(PdfName.SUBTYPE, PdfName.IMAGE);
                stream.put(key, value);
                stream.put(PdfName.FILTER, PdfName.DCTDECODE);
                stream.put(PdfName.WIDTH, new PdfNumber(width));
                stream.put(PdfName.HEIGHT, new PdfNumber(height));
                stream.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8));
                stream.put(PdfName.COLORSPACE, PdfName.DEVICERGB);
            }
        }
        // Save altered PDF
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        stamper.close();
        reader.close();
    }
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new SpecialId().createPdf(SpecialId.RESULT);
        new ResizeImage().manipulatePdf(SpecialId.RESULT, RESULT);
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
 
package part4.chapter16;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;
import java.util.TreeSet;
 
import part3.chapter11.FontTypes;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
 
public class ListUsedFonts {
 
    /** The resulting PDF file. */
    public static String RESULT
        = "results/part4/chapter16/fonts.txt";
 
    /**
     * Creates a Set containing information about the fonts in the src PDF file.
     * @param src the path to a PDF file
     * @throws IOException
     */
    public Set<String> listFonts(String src) throws IOException {
        Set<String> set = new TreeSet<String>();
        PdfReader reader = new PdfReader(src);
        PdfDictionary resources;
        for (int k = 1; k <= reader.getNumberOfPages(); ++k) {
            resources = reader.getPageN(k).getAsDict(PdfName.RESOURCES);
            processResource(set, resources);
        }
        reader.close();
        return set;
    }
 
    /**
     * Extracts the font names from page or XObject resources.
     * @param set the set with the font names
     * @param resources the resources dictionary
     */
    public static void processResource(Set<String> set, PdfDictionary resource) {
        if (resource == null)
            return;
        PdfDictionary xobjects = resource.getAsDict(PdfName.XOBJECT);
        if (xobjects != null) {
            for (PdfName key : xobjects.getKeys()) {
                processResource(set, xobjects.getAsDict(key));
            }
        }
        PdfDictionary fonts = resource.getAsDict(PdfName.FONT);
        if (fonts == null)
            return;
        PdfDictionary font;
        for (PdfName key : fonts.getKeys()) {
            font = fonts.getAsDict(key);
            String name = font.getAsName(PdfName.BASEFONT).toString();
            if (name.length() > 8 && name.charAt(7) == '+') {
                name = String.format("%s subset (%s)", name.substring(8), name.substring(1, 7));
            }
            else {
                name = name.substring(1);
                PdfDictionary desc = font.getAsDict(PdfName.FONTDESCRIPTOR);
                if (desc == null)
                    name += " nofontdescriptor";
                else if (desc.get(PdfName.FONTFILE) != null)
                    name += " (Type 1) embedded";
                else if (desc.get(PdfName.FONTFILE2) != null)
                    name += " (TrueType) embedded";
                else if (desc.get(PdfName.FONTFILE3) != null)
                    name += " (" + font.getAsName(PdfName.SUBTYPE).toString().substring(1) + ") embedded";
            }
            set.add(name);
        }
    }
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new FontTypes().createPdf(FontTypes.RESULT);
        Set<String> set = new ListUsedFonts().listFonts(FontTypes.RESULT);
        PrintWriter out = new PrintWriter(new FileOutputStream(RESULT));
        for (String fontname : set)
            out.println(fontname);
        out.flush();
        out.close();
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
 
package part4.chapter16;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfIndirectObject;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfStream;
import com.itextpdf.text.pdf.PdfWriter;
 
public class EmbedFontPostFacto {
 
    /** The first resulting PDF file. */
    public static String RESULT1 = "results/part4/chapter16/without_font.pdf";
    /** The second resulting PDF file. */
    public static String RESULT2 = "results/part4/chapter16/with_font.pdf";
    /** A special font. */
    public static String FONT = "resources/fonts/wds011402.ttf";
    /** The name of the special font. */
    public static String FONTNAME = "WaltDisneyScriptv4.1";
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws    DocumentException 
     * @throws    IOException
     */
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4:
        Font font = new Font(BaseFont.createFont(FONT, "", BaseFont.NOT_EMBEDDED), 60);
        document.add(new Paragraph("iText in Action", font));
        // step 5
        document.close();
    }
 
    /**
     * Manipulates a PDF file src with the file dest as result
     * @param src the original PDF
     * @param dest the resulting PDF
     * @throws IOException
     * @throws DocumentException 
     */
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        // the font file
        RandomAccessFile raf = new RandomAccessFile(FONT, "r");
        byte fontfile[] = new byte[(int)raf.length()];
        raf.readFully(fontfile);
        raf.close();
        // create a new stream for the font file
        PdfStream stream = new PdfStream(fontfile);
        stream.flateCompress();
        stream.put(PdfName.LENGTH1, new PdfNumber(fontfile.length));
        // create a reader object
        PdfReader reader = new PdfReader(src);
        int n = reader.getXrefSize();
        PdfObject object;
        PdfDictionary font;
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        PdfName fontname = new PdfName(FONTNAME);
        for (int i = 0; i < n; i++) {
            object = reader.getPdfObject(i);
            if (object == null || !object.isDictionary())
                continue;
            font = (PdfDictionary)object;
            if (PdfName.FONTDESCRIPTOR.equals(font.get(PdfName.TYPE))
                && fontname.equals(font.get(PdfName.FONTNAME))) {
                PdfIndirectObject objref = stamper.getWriter().addToBody(stream);
                font.put(PdfName.FONTFILE2, objref.getIndirectReference());
            }
        }
        stamper.close();
        reader.close();
    }
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        EmbedFontPostFacto example = new EmbedFontPostFacto();
        example.createPdf(RESULT1);
        example.manipulatePdf(RESULT1, RESULT2);
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
package part4.chapter16;
 
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Set;
import java.util.TreeSet;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PRStream;
import com.itextpdf.text.pdf.PdfAnnotation;
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.PdfWriter;
import com.lowagie.database.DatabaseConnection;
import com.lowagie.database.HsqldbConnection;
import com.lowagie.filmfestival.Movie;
import com.lowagie.filmfestival.PojoFactory;
 
/**
 * Creates a PDF listing Stanley Kubrick movies and the corresponding posters.
 * JPGs of the movie posters are added as file attachment annotations.
 * The attachments can be extracted.
 */
public class KubrickDvds {
 
    /** Path to the resources. */
    public static final String RESOURCE
        = "resources/posters/%s.jpg";
    /** The filename of the resulting PDF. */
    public static final String FILENAME = "kubrick_dvds.pdf";
    /** The path to the resulting PDFs */
    public static final String PATH = "results/part4/chapter16/%s";
    /** The filename of the PDF with the movies by Stanley Kubrick. */
    public static final String RESULT = String.format(PATH, FILENAME);
 
    /**
     * Creates a PDF listing Stanley Kubrick movies in my DVD Collection. 
     * @throws IOException 
     * @throws DocumentException 
     * @throws SQLException
     */
    public static void main(String[] args) throws IOException, DocumentException, SQLException {
        KubrickDvds kubrick = new KubrickDvds();
        FileOutputStream os = new FileOutputStream(RESULT);
        os.write(kubrick.createPdf());
        os.flush();
        os.close();
        kubrick.extractAttachments(RESULT);
    }
 
    /**
     * Extracts attachments from an existing PDF.
     * @param src   the path to the existing PDF
     * @param dest  where to put the extracted attachments
     * @throws IOException
     */
    public void extractAttachments(String src) throws IOException {
        PdfReader reader = new PdfReader(src);
        PdfArray array;
        PdfDictionary annot;
        PdfDictionary fs;
        PdfDictionary refs;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            array = reader.getPageN(i).getAsArray(PdfName.ANNOTS);
            if (array == null) continue;
            for (int j = 0; j < array.size(); j++) {
                annot = array.getAsDict(j);
                if (PdfName.FILEATTACHMENT.equals(annot.getAsName(PdfName.SUBTYPE))) {
                    fs = annot.getAsDict(PdfName.FS);
                    refs = fs.getAsDict(PdfName.EF);
                    for (PdfName name : refs.getKeys()) {
                        FileOutputStream fos
                            = new FileOutputStream(String.format(PATH, fs.getAsString(name).toString()));
                        fos.write(PdfReader.getStreamBytes((PRStream)refs.getAsStream(name)));
                        fos.flush();
                        fos.close();
                    }
                }
            }
        }
        reader.close();
    }
 
    /**
     * Creates the PDF.
     * @return the bytes of a PDF file.
     * @throws DocumentExcetpion
     * @throws IOException
     * @throws SQLException 
     */
    public byte[] createPdf() throws DocumentException, IOException, SQLException {
        // step 1
        Document document = new Document();
        // step 2
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("This is a list of Kubrick movies available in DVD stores."));
        PdfAnnotation annot;
        DatabaseConnection connection = new HsqldbConnection("filmfestival");
        Set<Movie> movies = new TreeSet<Movie>();
        movies.addAll(PojoFactory.getMovies(connection, 1));
        movies.addAll(PojoFactory.getMovies(connection, 4));
        ListItem item;
        Chunk chunk;
        List list = new List();
        for (Movie movie : movies) {
            annot = PdfAnnotation.createFileAttachment(
                    writer, null, movie.getMovieTitle(false), null,
                    String.format(RESOURCE, movie.getImdb()), String.format("%s.jpg", movie.getImdb()));
            item = new ListItem(movie.getMovieTitle(false));
            item.add("\u00a0\u00a0");
            chunk = new Chunk("\u00a0\u00a0\u00a0\u00a0");
            chunk.setAnnotation(annot);
            item.add(chunk);
            list.add(item);
        }
        document.add(list);
        // step 5
        document.close();
        connection.close();
        return baos.toByteArray();
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
 
package part4.chapter16;
 
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.SQLException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PRStream;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.xml.XMLUtil;
import com.lowagie.database.DatabaseConnection;
import com.lowagie.database.HsqldbConnection;
import com.lowagie.filmfestival.Movie;
import com.lowagie.filmfestival.PojoFactory;
 
public class KubrickDocumentary {
    /** The filename of the PDF. */
    public static final String FILENAME = "kubrick_documentary.pdf";
    /** The path to the resulting PDFs */
    public static final String PATH = "results/part4/chapter16/%s";
    /** The filename of the PDF with the movies by Stanley Kubrick. */
    public static final String RESULT = String.format(PATH, FILENAME);
 
    /**
     * Creates the PDF.
     * @return the bytes of a PDF file.
     * @throws DocumentExcetpion
     * @throws IOException
     * @throws SQLException 
     */
    public byte[] createPdf() throws DocumentException, IOException, SQLException {
        DatabaseConnection connection = new HsqldbConnection("filmfestival");
        java.util.List<Movie> movies = PojoFactory.getMovies(connection, 1);
        connection.close();
        // step 1
        Document document = new Document();
        // step 2
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph(
            "'Stanley Kubrick: A Life in Pictures' is a documentary about Stanley Kubrick and his films:"));
 
        ByteArrayOutputStream txt = new ByteArrayOutputStream();
        PrintStream out = new PrintStream(txt);
        out.println("<movies>");
        List list = new List(List.UNORDERED, 20);
        ListItem item;
        for (Movie movie : movies) {
            out.println("<movie>");
            out.println(
                String.format("<title>%s</title>", XMLUtil.escapeXML(movie.getMovieTitle(), true)));
            out.println(String.format("<year>%s</year>", movie.getYear()));
            out.println(String.format("<duration>%s</duration>", movie.getDuration()));
            out.println("</movie>");
            item = new ListItem(movie.getMovieTitle());
            list.add(item);
        }
        document.add(list);
        out.print("</movies>");
        out.flush();
        out.close();
        PdfFileSpecification fs
          = PdfFileSpecification.fileEmbedded(writer,
                  null, "kubrick.xml", txt.toByteArray());
        writer.addFileAttachment(fs);
        // step 5
        document.close();
        return baos.toByteArray();
    }
 
    /**
     * Extracts document level attachments
     * @param filename     a file from which document level attachments will be extracted
     * @throws IOException
     */
    public void extractDocLevelAttachments(String filename) throws IOException {
        PdfReader reader = new PdfReader(filename);
        PdfDictionary root = reader.getCatalog();
        PdfDictionary documentnames = root.getAsDict(PdfName.NAMES);
        PdfDictionary embeddedfiles = documentnames.getAsDict(PdfName.EMBEDDEDFILES);
        PdfArray filespecs = embeddedfiles.getAsArray(PdfName.NAMES);
        PdfDictionary filespec;
        PdfDictionary refs;
        FileOutputStream fos;
        PRStream stream;
        for (int i = 0; i < filespecs.size(); ) {
          filespecs.getAsString(i++);
          filespec = filespecs.getAsDict(i++);
          refs = filespec.getAsDict(PdfName.EF);
          for (PdfName key : refs.getKeys()) {
            fos = new FileOutputStream(String.format(PATH, filespec.getAsString(key).toString()));
            stream = (PRStream) PdfReader.getPdfObject(refs.getAsIndirectObject(key));
            fos.write(PdfReader.getStreamBytes(stream));
            fos.flush();
            fos.close();
          }
        }
        reader.close();
    }
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     * @throws SQLException
     */
    public static void main(String[] args) throws IOException, DocumentException, SQLException {
        KubrickDocumentary kubrick = new KubrickDocumentary();
        FileOutputStream os = new FileOutputStream(RESULT);
        os.write(kubrick.createPdf());
        os.flush();
        os.close();
        kubrick.extractDocLevelAttachments(RESULT);
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
package part4.chapter16;
 
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Set;
import java.util.TreeSet;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfDestination;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.collection.PdfTargetDictionary;
import com.lowagie.database.DatabaseConnection;
import com.lowagie.database.HsqldbConnection;
import com.lowagie.filmfestival.Movie;
import com.lowagie.filmfestival.PojoFactory;
 
/**
 * This example explains the use of portable collections,
 * a new feature introduced in PDF 1.7
 */
public class KubrickBox {
 
    /** The resulting PDF. */
    public static final String RESULT = "results/part4/chapter16/kubrick_box.pdf";
    /** The path to an image used in the example. */
    public static final String IMG_BOX = "resources/img/kubrick_box.jpg";
    /** Path to the resources. */
    public static final String RESOURCE = "resources/posters/%s.jpg";
    /** The relative widths of the PdfPTable columns. */
    public static final float[] WIDTHS = { 1 , 7 };
 
    /**
     * Creates the PDF.
     * @return the bytes of a PDF file.
     * @throws DocumentException
     * @throws IOException
     * @throws SQLException 
     */
    public void createPdf(String filename) throws DocumentException, IOException, SQLException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        Image img = Image.getInstance(IMG_BOX);
        document.add(img);
        List list = new List(List.UNORDERED, 20);
        PdfDestination dest = new PdfDestination(PdfDestination.FIT);
        dest.addFirst(new PdfNumber(1));
        PdfTargetDictionary target;
        Chunk chunk;
        ListItem item;
        PdfAction action = null;
        DatabaseConnection connection = new HsqldbConnection("filmfestival");
        Set<Movie> box = new TreeSet<Movie>();
        box.addAll(PojoFactory.getMovies(connection, 1));
        box.addAll(PojoFactory.getMovies(connection, 4));
        connection.close();
        PdfFileSpecification fs;
        for (Movie movie : box) {
            if (movie.getYear() > 1960) {
                fs = PdfFileSpecification.fileEmbedded(writer, null,
                        String.format("kubrick_%s.pdf", movie.getImdb()),
                        createMoviePage(movie));
                fs.addDescription(movie.getTitle(), false);
                writer.addFileAttachment(fs);
                item = new ListItem(movie.getMovieTitle());
                target = new PdfTargetDictionary(true);
                target.setEmbeddedFileName(movie.getTitle());
                action = PdfAction.gotoEmbedded(null, target, dest, true);
                chunk = new Chunk(" (see info)");
                chunk.setAction(action);
                item.add(chunk);
                list.add(item);
            }
        }
        document.add(list);
        // step 5
        document.close();
    }
 
    /**
     * Creates the PDF.
     * @return the bytes of a PDF file.
     * @throws DocumentExcetpion
     * @throws IOException
     * @throws SQLException 
     */
    public byte[] createMoviePage(Movie movie) throws DocumentException, IOException {
        // step 1
        Document document = new Document();
        // step 2
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);
        // step 3
        document.open();
        // step 4
        Paragraph p = new Paragraph(movie.getMovieTitle(),
                FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, 16));
        document.add(p);
        document.add(Chunk.NEWLINE);
        PdfPTable table = new PdfPTable(WIDTHS);
        table.addCell(Image.getInstance(String.format(RESOURCE, movie.getImdb())));
        PdfPCell cell = new PdfPCell();
        cell.addElement(new Paragraph("Year: " + movie.getYear()));
        cell.addElement(new Paragraph("Duration: " + movie.getDuration()));
        table.addCell(cell);
        document.add(table);
        PdfDestination dest = new PdfDestination(PdfDestination.FIT);
        dest.addFirst(new PdfNumber(1));
        PdfTargetDictionary target = new PdfTargetDictionary(false);
        Chunk chunk = new Chunk("Go to original document");
        PdfAction action = PdfAction.gotoEmbedded(null, target, dest, false);
        chunk.setAction(action);
        document.add(chunk);
        // step 5
        document.close();
        return baos.toByteArray();
    }
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     * @throws SQLException
     */
    public static void main(String[] args) throws DocumentException, IOException, SQLException {
        new KubrickBox().createPdf(RESULT);
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
package part4.chapter16;
 
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfString;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.collection.PdfCollection;
import com.itextpdf.text.pdf.collection.PdfCollectionField;
import com.itextpdf.text.pdf.collection.PdfCollectionItem;
import com.itextpdf.text.pdf.collection.PdfCollectionSchema;
import com.itextpdf.text.pdf.collection.PdfCollectionSort;
import com.itextpdf.text.pdf.collection.PdfTargetDictionary;
import com.lowagie.database.DatabaseConnection;
import com.lowagie.database.HsqldbConnection;
import com.lowagie.filmfestival.Movie;
import com.lowagie.filmfestival.PojoFactory;
 
public class KubrickMovies {
 
    /** Path to the resources. */
    public static final String RESOURCE
        = "resources/posters/%s.jpg";
    /** The relative widths of the PdfPTable columns. */
    public static final float[] WIDTHS = { 1 , 7 };
 
    /** The filename of the PDF. */
    public static final String FILENAME = "kubrick_movies.pdf";
    /** The filename of the PDF with the movies by Stanley Kubrick. */
    public static final String RESULT = "results/part4/chapter16/" + FILENAME;
 
    /**
     * Creates a Collection schema that can be used to organize the movies of Stanley Kubrick
     * in a collection: year, title, duration, DVD acquisition, filesize (filename is present, but hidden).
     * @return    a collection schema
     */
    private static PdfCollectionSchema getCollectionSchema() {
        PdfCollectionSchema schema = new PdfCollectionSchema();
 
        PdfCollectionField size = new PdfCollectionField("File size", PdfCollectionField.SIZE);
        size.setOrder(4);
        schema.addField("SIZE", size);
 
        PdfCollectionField filename = new PdfCollectionField("File name", PdfCollectionField.FILENAME);
        filename.setVisible(false);
        schema.addField("FILE", filename);
 
        PdfCollectionField title = new PdfCollectionField("Movie title", PdfCollectionField.TEXT);
        title.setOrder(1);
        schema.addField("TITLE", title);
 
        PdfCollectionField duration = new PdfCollectionField("Duration", PdfCollectionField.NUMBER);
        duration.setOrder(2);
        schema.addField("DURATION", duration);
 
        PdfCollectionField year = new PdfCollectionField("Year", PdfCollectionField.NUMBER);
        year.setOrder(0);
        schema.addField("YEAR", year);
 
        return schema;
    }
 
    /**
     * Creates the PDF.
     * @return the bytes of a PDF file.
     * @throws SQLException 
     */
    public byte[] createPdf() throws DocumentException, IOException, SQLException {
        // step 1
        Document document = new Document();
        // step 2
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph(
            "This document contains a collection of PDFs, one per Stanley Kubrick movie."));
 
        PdfCollection collection = new PdfCollection(PdfCollection.DETAILS);
        PdfCollectionSchema schema = getCollectionSchema(); 
        collection.setSchema(schema);
        PdfCollectionSort sort = new PdfCollectionSort("YEAR");
        sort.setSortOrder(false);
        collection.setSort(sort);
        collection.setInitialDocument("Eyes Wide Shut");
        writer.setCollection(collection);
 
        PdfFileSpecification fs;
        PdfCollectionItem item;
        DatabaseConnection connection = new HsqldbConnection("filmfestival");
        java.util.List<Movie> movies = PojoFactory.getMovies(connection, 1);
        connection.close();
        for (Movie movie : movies) {
            fs = PdfFileSpecification.fileEmbedded(writer, null,
                String.format("kubrick_%s.pdf", movie.getImdb()),
                createMoviePage(movie));
            fs.addDescription(movie.getTitle(), false);
 
            item = new PdfCollectionItem(schema);
            item.addItem("TITLE", movie.getMovieTitle(false));
            if (movie.getMovieTitle(true) != null) {
                item.setPrefix("TITLE", movie.getMovieTitle(true));
            }
            item.addItem("DURATION", movie.getDuration());
            item.addItem("YEAR", movie.getYear());
            fs.addCollectionItem(item);
            writer.addFileAttachment(fs);
        }
        // step 5
        document.close();
        return baos.toByteArray();
    }
 
    /**
     * Creates the PDF.
     * @return the bytes of a PDF file.
     */
    public byte[] createMoviePage(Movie movie) throws DocumentException, IOException {
        // step 1
        Document document = new Document();
        // step 2
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);
        // step 3
        document.open();
        // step 4
        Paragraph p = new Paragraph(movie.getMovieTitle(),
            FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, 16));
        document.add(p);
        document.add(Chunk.NEWLINE);
        PdfPTable table = new PdfPTable(WIDTHS);
        table.addCell(Image.getInstance(String.format(RESOURCE, movie.getImdb())));
        PdfPCell cell = new PdfPCell();
        cell.addElement(new Paragraph("Year: " + movie.getYear()));
        cell.addElement(new Paragraph("Duration: " + movie.getDuration()));
        table.addCell(cell);
        document.add(table);
        PdfTargetDictionary target = new PdfTargetDictionary(false);
        target.setAdditionalPath(new PdfTargetDictionary(false));
        Chunk chunk = new Chunk("Go to original document");
        PdfAction action = PdfAction.gotoEmbedded(null, target, new PdfString("movies"), false);
        chunk.setAction(action);
        document.add(chunk);
        // step 5
        document.close();
        return baos.toByteArray();
    }
 
    public static void main(String[] args) throws IOException, DocumentException, SQLException {
        KubrickMovies kubrick = new KubrickMovies();
        FileOutputStream os = new FileOutputStream(RESULT);
        os.write(kubrick.createPdf());
        os.flush();
        os.close();
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
package part4.chapter16;
 
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.SQLException;
import java.util.Set;
import java.util.TreeSet;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfDestination;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.collection.PdfCollection;
import com.itextpdf.text.pdf.collection.PdfCollectionField;
import com.itextpdf.text.pdf.collection.PdfCollectionItem;
import com.itextpdf.text.pdf.collection.PdfCollectionSchema;
import com.itextpdf.text.pdf.collection.PdfCollectionSort;
import com.itextpdf.text.pdf.collection.PdfTargetDictionary;
import com.lowagie.database.DatabaseConnection;
import com.lowagie.database.HsqldbConnection;
import com.lowagie.filmfestival.Movie;
import com.lowagie.filmfestival.PojoFactory;
 
/**
 * This example explains the use of portable collections,
 * a new feature introduced in PDF 1.7
 */
public class KubrickCollection {
 
    /** An image file used in this example */
    public static final String IMG_BOX = "resources/img/kubrick_box.jpg";
    /** An image file used in this example */
    public static final String IMG_KUBRICK = "resources/img/kubrick.jpg";
    /** The resulting PDF file. */
    public static final String RESULT = "results/part4/chapter16/kubrick_collection.pdf";
    /** The name of a field in the collection schema. */
    public static final String TYPE_FIELD        = "TYPE";
    /** A caption of a field in the collection schema. */
    public static final String TYPE_CAPTION        = "File type";
    /** The name of a field in the collection schema. */
    public static final String FILE_FIELD        = "FILE";
    /** A caption of a field in the collection schema. */
    public static final String FILE_CAPTION        = "File name";
    /** Sort order for the keys in the collection. */
    public String[] KEYS = { TYPE_FIELD, FILE_FIELD };
 
    /**
     * Creates the PDF.
     * @return the bytes of a PDF file.
     * @throws SQLException 
     */
    public void createPdf(String filename) throws DocumentException, IOException, SQLException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        PdfCollection collection = new PdfCollection(PdfCollection.HIDDEN);
        PdfCollectionSchema schema = getCollectionSchema();
        collection.setSchema(schema);
        PdfCollectionSort sort = new PdfCollectionSort(KEYS);
        collection.setSort(sort);
        writer.setCollection(collection);
 
        PdfCollectionItem collectionitem = new PdfCollectionItem(schema);
        PdfFileSpecification fs;
        fs = PdfFileSpecification.fileEmbedded(writer, IMG_KUBRICK, "kubrick.jpg", null);
        fs.addDescription("Stanley Kubrick", false);
        collectionitem.addItem(TYPE_FIELD, "JPEG");
        fs.addCollectionItem(collectionitem);
        writer.addFileAttachment(fs);
 
        ByteArrayOutputStream txt = new ByteArrayOutputStream();
        PrintStream out = new PrintStream(txt);
        Image img = Image.getInstance(IMG_BOX);
        document.add(img);
        List list = new List(List.UNORDERED, 20);
        PdfDestination dest = new PdfDestination(PdfDestination.FIT);
        dest.addFirst(new PdfNumber(1));
        PdfTargetDictionary intermediate;
        PdfTargetDictionary target;
        Chunk chunk;
        ListItem item;
        PdfAction action = null;
        DatabaseConnection connection = new HsqldbConnection("filmfestival");
        Set<Movie> box = new TreeSet<Movie>();
        java.util.List<Movie> movies = PojoFactory.getMovies(connection, 1);
        box.addAll(movies);
        box.addAll(PojoFactory.getMovies(connection, 4));
        connection.close();
        for (Movie movie : box) {
            if (movie.getYear() > 1960) {
                out.println(String.format(
                    "%s;%s;%s", movie.getMovieTitle(), movie.getYear(), movie.getDuration()));
                item = new ListItem(movie.getMovieTitle());
                if (!"0278736".equals(movie.getImdb())) {
                    target = new PdfTargetDictionary(true);
                    target.setEmbeddedFileName(movie.getTitle());
                    intermediate = new PdfTargetDictionary(true);
                    intermediate.setFileAttachmentPage(1);
                    intermediate.setFileAttachmentIndex(1);
                    intermediate.setAdditionalPath(target);
                    action = PdfAction.gotoEmbedded(null, intermediate, dest, true);
                    chunk = new Chunk(" (see info)");
                    chunk.setAction(action);
                    item.add(chunk);
                }
                list.add(item);
            }
        }
        out.flush();
        out.close();
        document.add(list);
        fs = PdfFileSpecification.fileEmbedded(writer, null, "kubrick.txt", txt.toByteArray());
        fs.addDescription("Kubrick box: the movies", false);
        collectionitem.addItem(TYPE_FIELD, "TXT");
        fs.addCollectionItem(collectionitem);
        writer.addFileAttachment(fs);
 
        PdfPTable table = new PdfPTable(1);
        table.setSpacingAfter(10);
        PdfPCell cell = new PdfPCell(new Phrase("All movies by Kubrick"));
        cell.setBorder(PdfPCell.NO_BORDER);
        fs = PdfFileSpecification.fileEmbedded(
            writer, null, KubrickMovies.FILENAME, new KubrickMovies().createPdf());
        collectionitem.addItem(TYPE_FIELD, "PDF");
        fs.addCollectionItem(collectionitem);
        target = new PdfTargetDictionary(true);
        target.setFileAttachmentPagename("movies");
        target.setFileAttachmentName("The movies of Stanley Kubrick");
        cell.setCellEvent(new PdfActionEvent(writer, PdfAction.gotoEmbedded(null, target, dest, true)));
        cell.setCellEvent(new FileAttachmentEvent(writer, fs, "The movies of Stanley Kubrick"));
        cell.setCellEvent(new LocalDestinationEvent(writer, "movies"));
        table.addCell(cell);
        writer.addFileAttachment(fs);
 
        cell = new PdfPCell(new Phrase("Kubrick DVDs"));
        cell.setBorder(PdfPCell.NO_BORDER);
        fs = PdfFileSpecification.fileEmbedded(
            writer, null, KubrickDvds.FILENAME, new KubrickDvds().createPdf());
        collectionitem.addItem(TYPE_FIELD, "PDF");
        fs.addCollectionItem(collectionitem);
        cell.setCellEvent(new FileAttachmentEvent(writer, fs, "Kubrick DVDs"));
        table.addCell(cell);
        writer.addFileAttachment(fs);
 
        cell = new PdfPCell(new Phrase("Kubrick documentary"));
        cell.setBorder(PdfPCell.NO_BORDER);
        fs = PdfFileSpecification.fileEmbedded(
            writer, null, KubrickDocumentary.FILENAME, new KubrickDocumentary().createPdf());
        collectionitem.addItem(TYPE_FIELD, "PDF");
        fs.addCollectionItem(collectionitem);
        cell.setCellEvent(new FileAttachmentEvent(writer, fs, "Kubrick Documentary"));
        table.addCell(cell);
        writer.addFileAttachment(fs);
 
        document.newPage();
        document.add(table);
        // step 5
        document.close();
    }
 
    /**
     * Creates a Collection schema that can be used to organize the movies of Stanley Kubrick
     * in a collection: year, title, duration, DVD acquisition, filesize (filename is present, but hidden).
     * @return    a collection schema
     */
    private static PdfCollectionSchema getCollectionSchema() {
        PdfCollectionSchema schema = new PdfCollectionSchema();
 
        PdfCollectionField type = new PdfCollectionField(TYPE_CAPTION, PdfCollectionField.TEXT);
        type.setOrder(0);
        schema.addField(TYPE_FIELD, type);
 
        PdfCollectionField filename = new PdfCollectionField(FILE_FIELD, PdfCollectionField.FILENAME);
        filename.setOrder(1);
        schema.addField(FILE_FIELD, filename);
 
        return schema;
    }
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     * @throws SQLException
     */
    public static void main(String[] args) throws DocumentException, IOException, SQLException {
        new KubrickCollection().createPdf(RESULT);
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
 
package part4.chapter16;
 
import java.io.IOException;
 
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfWriter;
 
public class FileAttachmentEvent implements PdfPCellEvent {
 
    /** The PdfWriter to which the file has to be attached. */
    protected PdfWriter writer;
    /** The file specification that will be used to create an annotation. */
    protected PdfFileSpecification fs;
    /** The description that comes with the annotation. */
    protected String description;
 
    /**
     * Creates a FileAttachmentEvent.
     * 
     * @param writer      the writer to which the file attachment has to be added.
     * @param fs          the file specification.
     * @param description a description for the file attachment.
     */
    public FileAttachmentEvent(PdfWriter writer, PdfFileSpecification fs, String description) {
        this.writer = writer;
        this.fs = fs;
        this.description = description;
    }
 
    /**
     * Implementation of the cellLayout method.
     * @see com.itextpdf.text.pdf.PdfPCellEvent#cellLayout(
     *     com.itextpdf.text.pdf.PdfPCell, com.itextpdf.text.Rectangle, com.itextpdf.text.pdf.PdfContentByte[])
     */
    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
        try {
            PdfAnnotation annotation = PdfAnnotation.createFileAttachment(writer,
                new Rectangle(position.getLeft() - 20, position.getTop() - 15,
                    position.getLeft() - 5, position.getTop() - 5),
                description, fs);
            annotation.setName(description);
            writer.addAnnotation(annotation);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
package part4.chapter16;
 
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfWriter;
 
public class PdfActionEvent implements PdfPCellEvent {
 
    /** The writer to which we are going to add the action. */
    protected PdfWriter writer;
    /** The action we're going to add. */
    protected PdfAction action;
 
    /** Creates a new Action event. */
    public PdfActionEvent(PdfWriter writer, PdfAction action) {
        this.writer = writer;
        this.action = action;
    }
 
    /** Implementation of the cellLayout method. */
    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
        writer.addAnnotation(new PdfAnnotation(writer,
            position.getLeft(), position.getBottom(), position.getRight(), position.getTop(),
            action));
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
 
package part4.chapter16;
 
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfDestination;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfWriter;
 
public class LocalDestinationEvent implements PdfPCellEvent {
 
    /** The writer to which the local destination will be added. */
    protected PdfWriter writer;
    /** The name of the local destination. */
    protected String name;
 
    /** Constructs a local destination event. */
    public LocalDestinationEvent(PdfWriter writer, String name) {
        this.writer = writer;
        this.name = name;
    }
 
    /** Implementation of the cellLayout method. */
    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
        writer.getDirectContent().localDestination(name,
            new PdfDestination(PdfDestination.FITH, position.getTop()));
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
 
package part4.chapter16;
 
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfWriter;
 
public class MovieAnnotation {
 
    /** The resulting PDF file. */
    public static final String RESULT
        = "results/part4/chapter16/movie.pdf";
    /** One of the resources. */
    public static final String RESOURCE
        = "resources/img/foxdog.mpg";
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws    DocumentException 
     * @throws    IOException
     */
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer
          = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(writer,
                RESOURCE, "foxdog.mpg", null);
        writer.addAnnotation(PdfAnnotation.createScreen(writer,
                new Rectangle(200f, 700f, 400f, 800f), "Fox and Dog", fs,
                "video/mpeg", true));
        // step 5
        document.close();
    }
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException 
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new MovieAnnotation().createPdf(RESULT);
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
 
package part4.chapter16;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfAppearance;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfIndirectObject;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfStream;
import com.itextpdf.text.pdf.PdfString;
import com.itextpdf.text.pdf.PdfWriter;
 
public class Pdf3D {
 
    /** The resulting PDF file. */
    public static String RESULT = "results/part4/chapter16/pdf3d.pdf";
    /** The path to a 3D file. */
    public static String RESOURCE = "resources/img/teapot.u3d";
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new Pdf3D().createPdf(RESULT);
    }
 
    /**
     * Creates the PDF.
     * @param  filename   the path to the resulting PDF file
     * @throws DocumentException
     * @throws IOException
     */
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
    	Document document = new Document();
    	// step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        Rectangle rect = new Rectangle(100, 400, 500, 800);
        rect.setBorder(Rectangle.BOX);
        rect.setBorderWidth(0.5f);
        rect.setBorderColor(new BaseColor(0xFF, 0x00, 0x00));
        document.add(rect);
 
        PdfStream stream3D = new PdfStream(new FileInputStream(RESOURCE), writer);
        stream3D.put(PdfName.TYPE, new PdfName("3D"));
        stream3D.put(PdfName.SUBTYPE, new PdfName("U3D"));
        stream3D.flateCompress();
        PdfIndirectObject streamObject = writer.addToBody(stream3D);
        stream3D.writeLength();
 
        PdfDictionary dict3D = new PdfDictionary();
        dict3D.put(PdfName.TYPE, new PdfName("3DView"));
        dict3D.put(new PdfName("XN"), new PdfString("Default"));
        dict3D.put(new PdfName("IN"), new PdfString("Unnamed"));
        dict3D.put(new PdfName("MS"), PdfName.M);
        dict3D.put(new PdfName("C2W"),
            new PdfArray(new float[] { 1, 0, 0, 0, 0, -1, 0, 1, 0, 3, -235, 28 } ));
        dict3D.put(PdfName.CO, new PdfNumber(235));
 
        PdfIndirectObject dictObject = writer.addToBody(dict3D); 
 
        PdfAnnotation annot = new PdfAnnotation(writer, rect);
        annot.put(PdfName.CONTENTS, new PdfString("3D Model"));
        annot.put(PdfName.SUBTYPE, new PdfName("3D"));
        annot.put(PdfName.TYPE, PdfName.ANNOT);
        annot.put(new PdfName("3DD"), streamObject.getIndirectReference());
        annot.put(new PdfName("3DV"), dictObject.getIndirectReference());
        PdfAppearance ap = writer.getDirectContent().createAppearance(rect.getWidth(), rect.getHeight());
        annot.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, ap);
        annot.setPage();
 
        writer.addAnnotation(annot);
        // step 5
        document.close();
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
package part4.chapter16;
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfDeveloperExtension;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfIndirectReference;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.richmedia.RichMediaActivation;
import com.itextpdf.text.pdf.richmedia.RichMediaAnnotation;
import com.itextpdf.text.pdf.richmedia.RichMediaConfiguration;
import com.itextpdf.text.pdf.richmedia.RichMediaInstance;
import com.itextpdf.text.pdf.richmedia.RichMediaParams;
 
 
public class FestivalCalendar1 {
 
    /** The resulting PDF file. */
    public static final String RESULT = "results/part4/chapter16/festival_calendar1.pdf";
    /** The path to a Flash application. */
    public static final String RESOURCE = "resources/swf/FestivalCalendar1.swf";
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
        writer.addDeveloperExtension(PdfDeveloperExtension.ADOBE_1_7_EXTENSIONLEVEL3);
        // step 3
        document.open();
        // step 4
        // we prepare a RichMediaAnnotation
        RichMediaAnnotation richMedia = new RichMediaAnnotation(writer, new Rectangle(36, 400, 559,806));
        // we embed the swf file
        PdfFileSpecification fs
            = PdfFileSpecification.fileEmbedded(writer, RESOURCE, "FestivalCalendar1.swf", null);
        // we declare the swf file as an asset
        PdfIndirectReference asset = richMedia.addAsset("FestivalCalendar1.swf", fs);
        // we create a configuration
        RichMediaConfiguration configuration = new RichMediaConfiguration(PdfName.FLASH);
        RichMediaInstance instance = new RichMediaInstance(PdfName.FLASH);
        RichMediaParams flashVars = new RichMediaParams();
        String vars = new String("&day=2011-10-13");
        flashVars.setFlashVars(vars);
        instance.setParams(flashVars);
        instance.setAsset(asset);
        configuration.addInstance(instance);
        // we add the configuration to the annotation
        PdfIndirectReference configurationRef = richMedia.addConfiguration(configuration);
        // activation of the rich media
        RichMediaActivation activation = new RichMediaActivation();
        activation.setConfiguration(configurationRef);
        richMedia.setActivation(activation);
        // we add the annotation
        PdfAnnotation richMediaAnnotation = richMedia.createAnnotation();
        richMediaAnnotation.setFlags(PdfAnnotation.FLAGS_PRINT);
        writer.addAnnotation(richMediaAnnotation);
        // step 5
        document.close();
    }
}
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
package part4.chapter16;
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Utilities;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfBorderDictionary;
import com.itextpdf.text.pdf.PdfDeveloperExtension;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfIndirectReference;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfString;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PushbuttonField;
import com.itextpdf.text.pdf.TextField;
import com.itextpdf.text.pdf.richmedia.RichMediaActivation;
import com.itextpdf.text.pdf.richmedia.RichMediaAnnotation;
import com.itextpdf.text.pdf.richmedia.RichMediaCommand;
import com.itextpdf.text.pdf.richmedia.RichMediaConfiguration;
import com.itextpdf.text.pdf.richmedia.RichMediaExecuteAction;
import com.itextpdf.text.pdf.richmedia.RichMediaInstance;
 
 
public class FestivalCalendar2 {
 
    /** The resulting PDF file. */
    public static final String RESULT = "results/part4/chapter16/festival_calendar2.pdf";
    /** The path to a Flash application. */
    public static final String RESOURCE = "resources/swf/FestivalCalendar2.swf";
    /** The path to a JavaScript file. */
    public static final String JS = "resources/js/show_date.js";
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        // step 1
    	Document document = new Document();
    	// step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
        writer.addDeveloperExtension(PdfDeveloperExtension.ADOBE_1_7_EXTENSIONLEVEL3);
        // step 3
        document.open();
        // step 4
        writer.addJavaScript(Utilities.readFileToString(JS));
        // we prepare a RichMediaAnnotation
        RichMediaAnnotation richMedia = new RichMediaAnnotation(writer, new Rectangle(36, 560, 561, 760));
        // we embed the swf file
        PdfFileSpecification fs
            = PdfFileSpecification.fileEmbedded(writer, RESOURCE, "FestivalCalendar2.swf", null);
        // we declare the swf file as an asset
        PdfIndirectReference asset = richMedia.addAsset("FestivalCalendar2.swf", fs);
        // we create a configuration
        RichMediaConfiguration configuration = new RichMediaConfiguration(PdfName.FLASH);
        RichMediaInstance instance = new RichMediaInstance(PdfName.FLASH);
        instance.setAsset(asset);
        configuration.addInstance(instance);
        // we add the configuration to the annotation
        PdfIndirectReference configurationRef = richMedia.addConfiguration(configuration);
        // activation of the rich media
        RichMediaActivation activation = new RichMediaActivation();
        activation.setConfiguration(configurationRef);
        richMedia.setActivation(activation);
        // we add the annotation
        PdfAnnotation richMediaAnnotation = richMedia.createAnnotation();
        richMediaAnnotation.setFlags(PdfAnnotation.FLAGS_PRINT);
        writer.addAnnotation(richMediaAnnotation);
 
        String[] days = new String[]{"2011-10-12", "2011-10-13", "2011-10-14", "2011-10-15",
                "2011-10-16", "2011-10-17", "2011-10-18", "2011-10-19"};
        for (int i = 0; i < days.length; i++) {
            Rectangle rect = new Rectangle(36 + (65 * i), 765, 100 + (65 * i), 780);
            PushbuttonField button = new PushbuttonField(writer, rect, "button" + i);
            button.setBackgroundColor(new GrayColor(0.75f));
            button.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
            button.setTextColor(GrayColor.GRAYBLACK);
            button.setFontSize(12);
            button.setText(days[i]);
            button.setLayout(PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT);
            button.setScaleIcon(PushbuttonField.SCALE_ICON_ALWAYS);
            button.setProportionalIcon(true);
            button.setIconHorizontalAdjustment(0);
            PdfFormField field = button.getField();
            RichMediaCommand command = new RichMediaCommand(new PdfString("getDateInfo"));
            command.setArguments(new PdfString(days[i]));
            RichMediaExecuteAction action
                = new RichMediaExecuteAction(richMediaAnnotation.getIndirectReference(), command);
            field.setAction(action);
            writer.addAnnotation(field);
        }
        TextField text = new TextField(writer, new Rectangle(36, 785, 559, 806), "date");
        text.setOptions(TextField.READ_ONLY);
        writer.addAnnotation(text.getTextField());
        // step 5
        document.close();
    }
}
File nameRaw URLUpdated
SpecialId.javaSpecialId.java2015-10-10 3:28 pm
ResizeImage.javaResizeImage.java2015-10-10 3:28 pm
ListUsedFonts.javaListUsedFonts.java2015-10-10 3:28 pm
EmbedFontPostFacto.javaEmbedFontPostFacto.java2015-10-10 3:28 pm
KubrickDvds.javaKubrickDvds.java2015-10-10 3:28 pm
KubrickDocumentary.javaKubrickDocumentary.java2015-10-10 3:28 pm
KubrickBox.javaKubrickBox.java2015-10-10 3:28 pm
KubrickMovies.javaKubrickMovies.java2015-10-10 3:28 pm
KubrickCollection.javaKubrickCollection.java2015-10-10 3:28 pm
FileAttachmentEvent.javaFileAttachmentEvent.java2015-10-10 3:28 pm
PdfActionEvent.javaPdfActionEvent.java2015-10-10 3:28 pm
LocalDestinationEvent.javaLocalDestinationEvent.java2015-10-10 3:28 pm
MovieAnnotation.javaMovieAnnotation.java2015-10-10 3:28 pm
Pdf3D.javaPdf3D.java2015-10-10 3:28 pm
FestivalCalendar1.javaFestivalCalendar1.java2015-10-10 3:28 pm
FestivalCalendar2.javaFestivalCalendar2.java2015-10-10 3:28 pm