Chapter 15: Page content and structure

These examples were written in the context of Chapter 15 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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfLayer;
import com.itextpdf.text.pdf.PdfWriter;
 
/**
 * This example was written by Bruno Lowagie. It is part of the book 'iText in
 * Action' by Manning Publications. 
 * ISBN: 1932394796
 * http://www.1t3xt.com/docs/book.php 
 * http://www.manning.com/lowagie/
 */
 
public class PeekABoo {
 
    /** The first resulting PDF. */
    public static String RESULT1 = "results/part4/chapter15/peek-a-boo1.pdf";
    /** The second resulting PDF. */
    public static String RESULT2 = "results/part4/chapter15/peek-a-boo2.pdf";
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws DocumentException 
     * @throws IOException 
     */
    public void createPdf(String filename, boolean on) throws DocumentException, IOException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document,
            new FileOutputStream(filename));
        writer.setViewerPreferences(PdfWriter.PageModeUseOC);
        writer.setPdfVersion(PdfWriter.VERSION_1_5);
        // step 3
        document.open();
        // step 4
        PdfLayer layer = new PdfLayer("Do you see me?", writer);
        layer.setOn(on);
        BaseFont bf = BaseFont.createFont();
        PdfContentByte cb = writer.getDirectContent();
        cb.beginText();
        cb.setFontAndSize(bf, 18);
        cb.showTextAligned(Element.ALIGN_LEFT, "Do you see me?", 50, 790, 0);
        cb.beginLayer(layer);
        cb.showTextAligned(Element.ALIGN_LEFT, "Peek-a-Boo!!!", 50, 766, 0);
        cb.endLayer();
        cb.endText();
        // step 5
        document.close();
    }
 
    /**
     * A simple example with optional content.
     * 
     * @param args
     *            no arguments needed here
     * @throws IOException 
     * @throws DocumentException 
     */
    public static void main(String[] args) throws DocumentException, IOException {
        PeekABoo peekaboo = new PeekABoo();
        peekaboo.createPdf(RESULT1, true);
        peekaboo.createPdf(RESULT2, false);
    }
}
/*
 * 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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfLayer;
import com.itextpdf.text.pdf.PdfWriter;
 
public class OptionalContentExample {
 
    /** The resulting PDF. */
    public static String RESULT = "results/part4/chapter15/layer_structure.pdf";
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws DocumentException 
     * @throws IOException 
     */
    public void createPdf(String filename)
        throws DocumentException, IOException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer =
            PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        writer.setPdfVersion(PdfWriter.VERSION_1_5);
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();
        PdfLayer nested = new PdfLayer("Nested layers", writer);
        PdfLayer nested_1 = new PdfLayer("Nested layer 1", writer);
        PdfLayer nested_2 = new PdfLayer("Nested layer 2", writer);
        nested.addChild(nested_1);
        nested.addChild(nested_2);
        writer.lockLayer(nested_2);
        cb.beginLayer(nested);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
                "nested layers"), 50, 775, 0);
        cb.endLayer();
        cb.beginLayer(nested_1);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
                "nested layer 1"), 100, 800, 0);
        cb.endLayer();
        cb.beginLayer(nested_2);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
                "nested layer 2"), 100, 750, 0);
        cb.endLayer();
 
        PdfLayer group = PdfLayer.createTitle("Grouped layers", writer);
        PdfLayer layer1 = new PdfLayer("Group: layer 1", writer);
        PdfLayer layer2 = new PdfLayer("Group: layer 2", writer);
        group.addChild(layer1);
        group.addChild(layer2);
        cb.beginLayer(layer1);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
                "layer 1 in the group"), 50, 700, 0);
        cb.endLayer();
        cb.beginLayer(layer2);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
                "layer 2 in the group"), 50, 675, 0);
        cb.endLayer();
 
        PdfLayer radiogroup = PdfLayer.createTitle("Radio group", writer);
        PdfLayer radio1 = new PdfLayer("Radiogroup: layer 1", writer);
        radio1.setOn(true);
        PdfLayer radio2 = new PdfLayer("Radiogroup: layer 2", writer);
        radio2.setOn(false);
        PdfLayer radio3 = new PdfLayer("Radiogroup: layer 3", writer);
        radio3.setOn(false);
        radiogroup.addChild(radio1);
        radiogroup.addChild(radio2);
        radiogroup.addChild(radio3);
        ArrayList<PdfLayer> options = new ArrayList<PdfLayer>();
        options.add(radio1);
        options.add(radio2);
        options.add(radio3);
        writer.addOCGRadioGroup(options);
        cb.beginLayer(radio1);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
                "option 1"), 50, 600, 0);
        cb.endLayer();
        cb.beginLayer(radio2);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
                "option 2"), 50, 575, 0);
        cb.endLayer();
        cb.beginLayer(radio3);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
                "option 3"), 50, 550, 0);
        cb.endLayer();
 
        PdfLayer not_printed = new PdfLayer("not printed", writer);
        not_printed.setOnPanel(false);
        not_printed.setPrint("Print", false);
        cb.beginLayer(not_printed);
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(
                "PRINT THIS PAGE"), 300, 700, 90);
        cb.endLayer();
 
        PdfLayer zoom = new PdfLayer("Zoom 0.75-1.25", writer);
        zoom.setOnPanel(false);
        zoom.setZoom(0.75f, 1.25f);
        cb.beginLayer(zoom);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(
                "Only visible if the zoomfactor is between 75 and 125%"), 30,
                530, 90);
        cb.endLayer();
 
        // step 5
        document.close();
    }
 
    /**
     * A simple example with optional content.
     * 
     * @param args
     *            no arguments needed here
     * @throws IOException
     * @throws DocumentException
     */
    public static void main(String[] args) throws DocumentException,
            IOException {
        new OptionalContentExample().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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfLayer;
import com.itextpdf.text.pdf.PdfLayerMembership;
import com.itextpdf.text.pdf.PdfWriter;
 
public class LayerMembershipExample1 {
 
    /** The resulting PDF. */
    public static String RESULT = "results/part4/chapter15/layer_membership1.pdf";
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws DocumentException 
     * @throws IOException 
     */
    public void createPdf(String filename) throws DocumentException, IOException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(
                document, new FileOutputStream(RESULT));
        writer.setPdfVersion(PdfWriter.VERSION_1_5);
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();
 
        PdfLayer dog = new PdfLayer("layer 1", writer);
        PdfLayer tiger = new PdfLayer("layer 2", writer);
        PdfLayer lion = new PdfLayer("layer 3", writer);
        PdfLayerMembership cat = new PdfLayerMembership(writer);
        cat.addMember(tiger);
        cat.addMember(lion);
        PdfLayerMembership no_cat = new PdfLayerMembership(writer);
        no_cat.addMember(tiger);
        no_cat.addMember(lion);
        no_cat.setVisibilityPolicy(PdfLayerMembership.ALLOFF);
        cb.beginLayer(dog);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("dog"),
                50, 775, 0);
        cb.endLayer();
        cb.beginLayer(tiger);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("tiger"),
                50, 750, 0);
        cb.endLayer();
        cb.beginLayer(lion);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("lion"),
                50, 725, 0);
        cb.endLayer();
        cb.beginLayer(cat);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("cat"),
                50, 700, 0);
        cb.endLayer();
        cb.beginLayer(no_cat);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT,
                new Phrase("no cat"), 50, 700, 0);
        cb.endLayer();
 
        // step 5
        document.close();
    }
 
    /**
     * A simple example with optional content.
     * 
     * @param args
     *            no arguments needed here
     * @throws IOException
     * @throws DocumentException
     */
    public static void main(String[] args) throws DocumentException,
            IOException {
        new LayerMembershipExample1().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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfLayer;
import com.itextpdf.text.pdf.PdfLayerMembership;
import com.itextpdf.text.pdf.PdfVisibilityExpression;
import com.itextpdf.text.pdf.PdfWriter;
 
public class LayerMembershipExample2 {
 
    /** The resulting PDF. */
    public static String RESULT = "results/part4/chapter15/layer_membership2.pdf";
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws DocumentException 
     * @throws IOException 
     */
    public void createPdf(String filename) throws DocumentException,
            IOException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(
                document, new FileOutputStream(RESULT));
        writer.setPdfVersion(PdfWriter.VERSION_1_6);
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();
 
        PdfLayer dog = new PdfLayer("layer 1", writer);
        PdfLayer tiger = new PdfLayer("layer 2", writer);
        PdfLayer lion = new PdfLayer("layer 3", writer);
        PdfLayerMembership cat = new PdfLayerMembership(writer);
        PdfVisibilityExpression ve1 = new PdfVisibilityExpression(PdfVisibilityExpression.OR);
        ve1.add(tiger);
        ve1.add(lion);
        cat.setVisibilityExpression(ve1);
        PdfLayerMembership no_cat = new PdfLayerMembership(writer);
        PdfVisibilityExpression ve2 = new PdfVisibilityExpression(PdfVisibilityExpression.NOT);
        ve2.add(ve1);
        no_cat.setVisibilityExpression(ve2);
        cb.beginLayer(dog);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("dog"),
                50, 775, 0);
        cb.endLayer();
        cb.beginLayer(tiger);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("tiger"),
                50, 750, 0);
        cb.endLayer();
        cb.beginLayer(lion);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("lion"),
                50, 725, 0);
        cb.endLayer();
        cb.beginLayer(cat);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("cat"),
                50, 700, 0);
        cb.endLayer();
        cb.beginLayer(no_cat);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT,
                new Phrase("no cat"), 50, 700, 0);
        cb.endLayer();
 
        // step 5
        document.close();
    }
 
    /**
     * A simple example with optional content.
     * 
     * @param args
     *            no arguments needed here
     * @throws IOException
     * @throws DocumentException
     */
    public static void main(String[] args) throws DocumentException,
            IOException {
        new LayerMembershipExample2().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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfLayer;
import com.itextpdf.text.pdf.PdfWriter;
 
public class OptionalContentActionExample {
 
    /** The resulting PDF. */
    public static String RESULT = "results/part4/chapter15/layer_actions.pdf";
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws DocumentException 
     * @throws IOException 
     */
    public void createPdf(String filename) throws DocumentException, IOException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(
                document, new FileOutputStream(RESULT));
        writer.setPdfVersion(PdfWriter.VERSION_1_5);
        // step 3
        document.open();
        // step 4
        PdfLayer a1 = new PdfLayer("answer 1", writer);
        PdfLayer a2 = new PdfLayer("answer 2", writer);
        PdfLayer a3 = new PdfLayer("answer 3", writer);
        a1.setOn(false);
        a2.setOn(false);
        a3.setOn(false);
 
        BaseFont bf = BaseFont.createFont();
        PdfContentByte cb = writer.getDirectContent();
        cb.beginText();
        cb.setFontAndSize(bf, 18);
        cb.showTextAligned(Element.ALIGN_LEFT,
            "Q1: Who is the director of the movie 'Paths of Glory'?", 50, 766, 0);
        cb.showTextAligned(Element.ALIGN_LEFT,
            "Q2: Who directed the movie 'Lawrence of Arabia'?", 50, 718, 0);
        cb.showTextAligned(Element.ALIGN_LEFT,
            "Q3: Who is the director of 'House of Flying Daggers'?", 50, 670, 0);
        cb.endText();
        cb.saveState();
        cb.setRGBColorFill(0xFF, 0x00, 0x00);
        cb.beginText();
        cb.beginLayer(a1);
        cb.showTextAligned(Element.ALIGN_LEFT,
                "A1: Stanley Kubrick", 50, 742, 0);
        cb.endLayer();
        cb.beginLayer(a2);
        cb.showTextAligned(Element.ALIGN_LEFT,
                "A2: David Lean", 50, 694, 0);
        cb.endLayer();
        cb.beginLayer(a3);
        cb.showTextAligned(Element.ALIGN_LEFT,
                "A3: Zhang Yimou", 50, 646, 0);
        cb.endLayer();
        cb.endText();
        cb.restoreState();
 
        ArrayList<Object> stateOn = new ArrayList<Object>();
        stateOn.add("ON");
        stateOn.add(a1);
        stateOn.add(a2);
        stateOn.add(a3);
        PdfAction actionOn = PdfAction.setOCGstate(stateOn, true);
        ArrayList<Object> stateOff = new ArrayList<Object>();
        stateOff.add("OFF");
        stateOff.add(a1);
        stateOff.add(a2);
        stateOff.add(a3);
        PdfAction actionOff = PdfAction.setOCGstate(stateOff, true);
        ArrayList<Object> stateToggle = new ArrayList<Object>();
        stateToggle.add("Toggle");
        stateToggle.add(a1);
        stateToggle.add(a2);
        stateToggle.add(a3);
        PdfAction actionToggle = PdfAction.setOCGstate(stateToggle, true);
        Phrase p = new Phrase("Change the state of the answers:");
        Chunk on = new Chunk(" on ").setAction(actionOn);
        p.add(on);
        Chunk off = new Chunk("/ off ").setAction(actionOff);
        p.add(off);
        Chunk toggle = new Chunk("/ toggle").setAction(actionToggle);
        p.add(toggle);
        document.add(p);
        // step 5
        document.close();
    }
 
    /**
     * A simple example with optional content.
     * 
     * @param args
     *            no arguments needed here
     * @throws IOException
     * @throws DocumentException
     */
    public static void main(String[] args) throws DocumentException,
            IOException {
        new OptionalContentActionExample().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.chapter15;
 
import java.awt.Graphics2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.svg.SVGDocument;
 
import com.itextpdf.awt.PdfGraphics2D;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
 
public class SvgToPdf {
 
    /** The resulting PDF. */
    public static final String RESULT = "results/part4/chapter15/foobar.pdf";
    /** The map (shapes). */
    public static final String CITY = "resources/xml/foobarcity.svg";
    /** The map (text = street names in English). */
    public static final String STREETS = "resources/xml/foobarstreets.svg";
 
    /** The SVG document factory. */
    protected SAXSVGDocumentFactory factory;
    /** The SVG bridge context. */
    protected BridgeContext ctx;
    /** The GVT builder */
    protected GVTBuilder builder;
 
    /** Creates an SvgToPdf object. */
    public SvgToPdf() {
        String parser = XMLResourceDescriptor.getXMLParserClassName();
        factory = new SAXSVGDocumentFactory(parser);
 
        UserAgent userAgent = new UserAgentAdapter();
        DocumentLoader loader = new DocumentLoader(userAgent);
        ctx = new BridgeContext(userAgent, loader);
        ctx.setDynamicState(BridgeContext.DYNAMIC);
 
        builder = new GVTBuilder();
    }
 
    /**
     * Draws an SVG file to a PdfTemplate.
     * @param map      the template to which the SVG has to be drawn.
     * @param resource the SVG content.
     * @throws IOException
     */
    public void drawSvg(PdfTemplate map, String resource) throws IOException {
        Graphics2D g2d = new PdfGraphics2D(map, 6000, 6000);
        SVGDocument city = factory.createSVGDocument(new File(resource).toURL()
                .toString());
        GraphicsNode mapGraphics = builder.build(ctx, city);
        mapGraphics.paint(g2d);
        g2d.dispose();
    }
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws DocumentException 
     * @throws IOException
     * @throws SQLException 
     */
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
    	Document document = new Document(new Rectangle(6000, 6000));
        // step 2
    	PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        // step 3
    	document.open();
    	// step 4
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate map = cb.createTemplate(6000, 6000);
        drawSvg(map, CITY);
        cb.addTemplate(map, 0, 0);
        PdfTemplate streets = cb.createTemplate(6000, 6000);
        drawSvg(streets, STREETS);
        cb.addTemplate(streets, 0, 0);
        // step 5
        document.close();
    }
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new SvgToPdf().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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
 
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfLayer;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
 
public class SvgLayers extends SvgToPdf {
 
    /** The resulting PDF. */
    public static final String RESULT = "results/part4/chapter15/foobarcity.pdf";
    /** The map. */
    public static final String RUES = "resources/xml/foobarrues.svg";
    /** The map. */
    public static final String STRATEN = "resources/xml/foobarstraten.svg";
 
 
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws DocumentException 
     * @throws IOException
     * @throws SQLException 
     */
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
    	Document document = new Document(new Rectangle(6000, 6000));
        // step 2
    	PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        writer.setViewerPreferences(PdfWriter.PageModeUseOC | PdfWriter.FitWindow);
        writer.setPdfVersion(PdfWriter.VERSION_1_5);
        // step 3
        document.open();
        // step 4
        // CREATE GRID LAYER
        PdfLayer gridLayer = new PdfLayer("Grid", writer);
        gridLayer.setZoom(0.2f, 1);
        gridLayer.setOnPanel(false);
        // CREATE STREET LAYERS
        PdfLayer streetlayer = PdfLayer.createTitle(
                "Streets / Rues / Straten", writer);
        PdfLayer streetlayer_en = new PdfLayer("English", writer);
        streetlayer_en.setOn(true);
        streetlayer_en.setLanguage("en", true);
        PdfLayer streetlayer_fr = new PdfLayer("Fran\u00e7ais", writer);
        streetlayer_fr.setOn(false);
        streetlayer_fr.setLanguage("fr", false);
        PdfLayer streetlayer_nl = new PdfLayer("Nederlands", writer);
        streetlayer_nl.setOn(false);
        streetlayer_nl.setLanguage("nl", false);
        streetlayer.addChild(streetlayer_en);
        streetlayer.addChild(streetlayer_fr);
        streetlayer.addChild(streetlayer_nl);
        ArrayList<PdfLayer> radio = new ArrayList<PdfLayer>();
        radio.add(streetlayer_en);
        radio.add(streetlayer_fr);
        radio.add(streetlayer_nl);
        writer.addOCGRadioGroup(radio);
        // CREATE MAP
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate map = cb.createTemplate(6000, 6000);
        // DRAW CITY
        drawSvg(map, CITY);
        cb.addTemplate(map, 0, 0);
        PdfTemplate streets = cb.createTemplate(6000, 6000);
        // DRAW STREETS
        drawSvg(streets, STREETS);
        streets.setLayer(streetlayer_en);
        cb.addTemplate(streets, 0, 0);
        PdfTemplate rues = cb.createTemplate(6000, 6000);
        drawSvg(rues, RUES);
        rues.setLayer(streetlayer_fr);
        cb.addTemplate(rues, 0, 0);
        PdfTemplate straten = cb.createTemplate(6000, 6000);
        drawSvg(straten, STRATEN);
        straten.setLayer(streetlayer_nl);
        cb.addTemplate(straten, 0, 0);
        // DRAW GRID
        cb.saveState();
        cb.beginLayer(gridLayer);
        cb.setGrayStroke(0.7f);
        cb.setLineWidth(2);
        for (int i = 0; i < 8; i++) {
            cb.moveTo(1250, 1500 + i * 500);
            cb.lineTo(4750, 1500 + i * 500);
        }
        for (int i = 0; i < 8; i++) {
            cb.moveTo(1250 + i * 500, 1500);
            cb.lineTo(1250 + i * 500, 5000);
        }
        cb.stroke();
        cb.endLayer();
        cb.restoreState();
 
        // CREATE INFO LAYERS
        PdfLayer cityInfoLayer = new PdfLayer("Foobar Info", writer);
        cityInfoLayer.setOn(false);
        PdfLayer hotelLayer = new PdfLayer("Hotel", writer);
        hotelLayer.setOn(false);
        cityInfoLayer.addChild(hotelLayer);
        PdfLayer parkingLayer = new PdfLayer("Parking", writer);
        parkingLayer.setOn(false);
        cityInfoLayer.addChild(parkingLayer);
        PdfLayer businessLayer = new PdfLayer("Industry", writer);
        businessLayer.setOn(false);
        cityInfoLayer.addChild(businessLayer);
        PdfLayer cultureLayer = PdfLayer.createTitle("Leisure and Culture",
                writer);
        PdfLayer goingoutLayer = new PdfLayer("Going out", writer);
        goingoutLayer.setOn(false);
        cultureLayer.addChild(goingoutLayer);
        PdfLayer restoLayer = new PdfLayer("Restaurants", writer);
        restoLayer.setOn(false);
        goingoutLayer.addChild(restoLayer);
        PdfLayer theatreLayer = new PdfLayer("(Movie) Theatres", writer);
        theatreLayer.setOn(false);
        goingoutLayer.addChild(theatreLayer);
        PdfLayer monumentLayer = new PdfLayer("Museums and Monuments",
                writer);
        monumentLayer.setOn(false);
        cultureLayer.addChild(monumentLayer);
        PdfLayer sportsLayer = new PdfLayer("Sports", writer);
        sportsLayer.setOn(false);
        cultureLayer.addChild(sportsLayer);
        // DRAW INFO
        BaseFont font = BaseFont.createFont(
                "c:/windows/fonts/webdings.ttf", BaseFont.WINANSI,
                BaseFont.EMBEDDED);
        cb.saveState();
        cb.beginText();
        cb.setRGBColorFill(0x00, 0x00, 0xFF);
        cb.setFontAndSize(font, 36);
        cb.beginLayer(cityInfoLayer);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x69), 2700, 3100, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x69), 3000, 2050, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x69), 3100, 2550, 0);
        cb.beginLayer(hotelLayer);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 2000, 1900, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 2100, 1950, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 2200, 2200, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 2700, 3000, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 2750, 3050, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 2500, 3500, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 2300, 2000, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 3250, 2200, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 3300, 2300, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 3400, 3050, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 3250, 3200, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 2750, 3800, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 2900, 3800, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 3000, 2400, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 2000, 2800, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe3), 2600, 3200, 0);
        cb.endLayer(); // hotelLayer
        cb.beginLayer(parkingLayer);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe8), 2400, 2000, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe8), 2100, 2600, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe8), 3300, 2250, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe8), 3000, 3900, 0);
        cb.endLayer(); // parkingLayer
        cb.beginLayer(businessLayer);
        cb.setRGBColorFill(0xC0, 0xC0, 0xC0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x46), 3050, 3600, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x46), 3200, 3900, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x46), 3150, 3700, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x46), 3260, 3610, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x46), 3350, 3750, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x46), 3500, 4000, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x46), 3500, 3800, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x46), 3450, 3700, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x46), 3450, 3600, 0);
        cb.endLayer(); // businessLayer
        cb.endLayer(); // cityInfoLayer
        cb.beginLayer(goingoutLayer);
        cb.beginLayer(restoLayer);
        cb.setRGBColorFill(0xFF, 0x14, 0x93);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe4), 2650, 3500, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe4), 2400, 1900, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe4), 2750, 3850, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe4), 2700, 3200, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe4), 2900, 3100, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe4), 2850, 3000, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe4), 2800, 2900, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe4), 2300, 2900, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe4), 1950, 2650, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe4), 1800, 2750, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe4), 3350, 3150, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe4), 3400, 3100, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xe4), 3250, 3450, 0);
        cb.endLayer(); // restoLayer
        cb.beginLayer(theatreLayer);
        cb.setRGBColorFill(0xDC, 0x14, 0x3C);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xae), 2850, 3300, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xae), 3050, 2900, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xae), 2650, 2900, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xae), 2750, 2600, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xB8), 2800, 3350, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xB8), 2550, 2850, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xB8), 2850, 3300, 0);
        cb.endLayer(); // theatreLayer
        cb.endLayer(); // goingoutLayer
        cb.beginLayer(monumentLayer);
        cb.setRGBColorFill(0x00, 0x00, 0x00);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x47), 3250, 2750, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x47), 2750, 2900, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x47), 2850, 3500, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xad), 2150, 3550, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xad), 3300, 2730, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xad), 2200, 2000, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xad), 2900, 3300, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0xad), 2080, 3000, 0);
        cb.endLayer(); // monumentLayer
        cb.beginLayer(sportsLayer);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x53), 2700, 4050, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x53), 2700, 3900, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x53), 2800, 3980, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x53), 1950, 2800, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, String
                .valueOf((char) 0x53), 3700, 2450, 0);
        cb.endLayer(); // sportsLayer
        cb.endText();
        cb.restoreState();
        // step 5
        document.close();
    }
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     * @throws SQLException 
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new SvgLayers().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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfString;
import com.itextpdf.text.pdf.PdfStructureElement;
import com.itextpdf.text.pdf.PdfStructureTreeRoot;
import com.itextpdf.text.pdf.PdfWriter;
import com.lowagie.database.DatabaseConnection;
import com.lowagie.database.HsqldbConnection;
import com.lowagie.filmfestival.Director;
import com.lowagie.filmfestival.Movie;
import com.lowagie.filmfestival.PojoFactory;
 
public class ObjectData {
 
    /** The resulting PDF. */
    public static final String RESULT = "results/part4/chapter15/objectdata.pdf";
    /** Path to the resources. */
    public static final String RESOURCE
        = "resources/posters/%s.jpg";
    /** SQL statement to get selected directors */
    public static final String SELECTDIRECTORS =
        "SELECT DISTINCT d.id, d.name, d.given_name, count(*) AS c "
        + "FROM film_director d, film_movie_director md "
        + "WHERE d.id = md.director_id AND d.id < 8 "
        + "GROUP BY d.id, d.name, d.given_name ORDER BY id";
 
    /**
     * Creates a PDF with information about the movies
     * @param    filename the name of the PDF file that will be created.
     * @throws    DocumentException 
     * @throws    IOException 
     * @throws    SQLException
     */
    public void createPdf(String filename)
        throws IOException, DocumentException, SQLException {
    	// step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        writer.setTagged();
        writer.setUserProperties(true);
        // step 3
        document.open();
        // step 4
 
        PdfStructureTreeRoot root = writer.getStructureTreeRoot();
        root.mapRole(new PdfName("Directors"), PdfName.H);
        for (int i = 1; i < 8; i++) {
        	root.mapRole(new PdfName("director" + i), PdfName.P);
        }
 
        DatabaseConnection connection = new HsqldbConnection("filmfestival");
 
        PdfStructureTreeRoot tree = writer.getStructureTreeRoot();
        PdfStructureElement top = new PdfStructureElement(tree, new PdfName("Directors"));
 
        Map<Integer,PdfStructureElement> directors = new HashMap<Integer,PdfStructureElement>();
        Statement stm = connection.createStatement();
        ResultSet rs = stm.executeQuery(SELECTDIRECTORS);
        int id;
        Director director;
        PdfStructureElement e;
        while (rs.next()) {
            id = rs.getInt("id");
            director = PojoFactory.getDirector(rs);
            e = new PdfStructureElement(top, new PdfName("director" + id));
            PdfDictionary userproperties = new PdfDictionary();
            userproperties.put(PdfName.O, PdfName.USERPROPERTIES);
            PdfArray properties = new PdfArray();
            PdfDictionary property1 = new PdfDictionary();
            property1.put(PdfName.N, new PdfString("Name"));
            property1.put(PdfName.V, new PdfString(director.getName()));            
            properties.add(property1);
            PdfDictionary property2 = new PdfDictionary();
            property2.put(PdfName.N, new PdfString("Given name"));
            property2.put(PdfName.V, new PdfString(director.getGivenName()));            
            properties.add(property2);
            PdfDictionary property3 = new PdfDictionary();
            property3.put(PdfName.N, new PdfString("Posters"));
            property3.put(PdfName.V, new PdfNumber(rs.getInt("c")));            
            properties.add(property3);
            userproperties.put(PdfName.P, properties);
            e.put(PdfName.A, userproperties);
            directors.put(id, e);
        }
 
        Map<Movie,Integer> map = new TreeMap<Movie,Integer>();
        for (int i = 1; i < 8; i++) {
            List<Movie> movies = PojoFactory.getMovies(connection, i);
            for (Movie movie : movies) {
                map.put(movie, i);
            }
        }
 
        PdfContentByte canvas = writer.getDirectContent();
        Image img;
        float x = 11.5f;
        float y = 769.7f;
        for (Map.Entry<Movie,Integer> entry : map.entrySet()) {
            img = Image.getInstance(String.format(RESOURCE, entry.getKey().getImdb()));
            img.scaleToFit(1000, 60);
            img.setAbsolutePosition(x + (45 - img.getScaledWidth()) / 2, y);
            canvas.beginMarkedContentSequence(directors.get(entry.getValue()));
            canvas.addImage(img);
            canvas.endMarkedContentSequence();
            x += 48;
            if (x > 578) {
                x = 11.5f;
                y -= 84.2f;
            }
        }
        // step 5
        document.close();
 
    }
 
 
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException 
     * @throws SQLException
     */
    public static void main(String[] args)
        throws IOException, SQLException, DocumentException {
        new ObjectData().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.chapter15;
 
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.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfString;
import com.itextpdf.text.pdf.PdfStructureElement;
import com.itextpdf.text.pdf.PdfStructureTreeRoot;
import com.itextpdf.text.pdf.PdfWriter;
 
public class ReadOutLoud {
 
    /** The resulting PDF. */
    public static String RESULT = "results/part4/chapter15/read_out_loud.pdf";
    /** A resource that is used in the PDF document. */
    public static String RESOURCE = "resources/posters/0062622.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 {
        // step 1
    	Document document = new Document();
    	// step 2
        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream(filename));
        writer.setTagged();
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
                BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        BaseFont bf2 = BaseFont.createFont("c:/windows/fonts/msgothic.ttc,1",
                BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
 
        PdfStructureTreeRoot root = writer.getStructureTreeRoot();
        PdfStructureElement div = new PdfStructureElement(root, new PdfName("Div"));
        PdfDictionary dict;
 
        cb.beginMarkedContentSequence(div);
 
        cb.beginText();
        cb.moveText(36, 788);
        cb.setFontAndSize(bf, 12);
        cb.setLeading(18);
        cb.showText("These are some famous movies by Stanley Kubrick: ");
        dict = new PdfDictionary();
        dict.put(PdfName.E, new PdfString("Doctor"));
        cb.beginMarkedContentSequence(new PdfName("Span"), dict, true);
        cb.newlineShowText("Dr.");
        cb.endMarkedContentSequence();
        cb.showText(" Strangelove or: How I Learned to Stop Worrying and Love the Bomb.");
        dict = new PdfDictionary();
        dict.put(PdfName.E, new PdfString("Eyes Wide Shut."));
        cb.beginMarkedContentSequence(new PdfName("Span"), dict, true);
        cb.newlineShowText("EWS");
        cb.endMarkedContentSequence();
        cb.endText();
        dict = new PdfDictionary();
        dict.put(PdfName.LANG, new PdfString("en-us"));
        dict.put(new PdfName("Alt"), new PdfString("2001: A Space Odyssey."));
        cb.beginMarkedContentSequence(new PdfName("Span"), dict, true);
        Image img = Image.getInstance(RESOURCE);
        img.scaleToFit(1000, 100);
        img.setAbsolutePosition(36, 640);
        cb.addImage(img);
        cb.endMarkedContentSequence();
 
        cb.beginText();
        cb.moveText(36, 620);
        cb.setFontAndSize(bf, 12);
        cb.showText("This is a movie by Akira Kurosawa: ");
        dict = new PdfDictionary();
        dict.put(PdfName.ACTUALTEXT, new PdfString("Seven Samurai."));
        cb.beginMarkedContentSequence(new PdfName("Span"), dict, true);
        cb.setFontAndSize(bf2, 12);
        cb.showText("\u4e03\u4eba\u306e\u4f8d");
        cb.endMarkedContentSequence();
        cb.endText();
 
        cb.endMarkedContentSequence();
        // step 5
        document.close();
    }
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new ReadOutLoud().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.chapter15;
import java.util.List;
 
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfStructureElement;
 
public class StructureParser extends DefaultHandler {
 
    /** The top element in the PDF structure */
    protected PdfStructureElement top;
    /** The list of structure elements */
    protected List<PdfStructureElement> elements;
 
    /** Creates a parser that will parse an XML file into a structure tree. */
    public StructureParser(PdfStructureElement top, List<PdfStructureElement> elements) {
        this.top = top;
        this.elements = elements;
    }
 
    /**
     * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
     *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        if ("chapter".equals(qName)) return;
        elements.add(new PdfStructureElement(top, new PdfName(qName)));
    }
}
/*
 * 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.chapter15;
 
import java.io.IOException;
import java.util.List;
 
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfStructureElement;
import com.itextpdf.text.pdf.PdfWriter;
 
public class ContentParser extends DefaultHandler {
 
    /** The StringBuffer that holds the characters. */
    protected StringBuffer buf = new StringBuffer();
 
    /** The document to which content parsed form XML will be added. */
    protected Document document;
    /** The writer to which PDF syntax will be written. */
    protected PdfWriter writer;
    /** The canvas to which content will be written. */
    protected PdfContentByte canvas;
    /** A list with structure elements. */
    protected List<PdfStructureElement> elements;
    /** The current structure element during the parsing process. */
    protected PdfStructureElement current;
    /** The column to which content will be added. */
    protected ColumnText column;
    /** The font used when content elements are created. */
    protected Font font;
 
    /**
     * Creates a new ContentParser
     * @param document
     * @param writer
     * @param elements
     * @throws DocumentException
     * @throws IOException
     */
    public ContentParser(Document document, PdfWriter writer, List<PdfStructureElement> elements)
        throws DocumentException, IOException {
        this.document = document;
        this.writer = writer;
        canvas = writer.getDirectContent();
        column = new ColumnText(canvas);
        column.setSimpleColumn(36, 36, 384, 569);
        this.elements = elements;
        font = new Font(
            BaseFont.createFont("c:/windows/fonts/arial.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED), 12);
    }
 
    /**
     * @see org.xml.sax.ContentHandler#characters(char[], int, int)
     */
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        for (int i = start; i < start + length; i++) {
            if (ch[i] == '\n')
                buf.append(' ');
            else
                buf.append(ch[i]);
        }
    }
 
    /**
     * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
     *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        if ("chapter".equals(qName)) return;
        current = elements.get(0);
        elements.remove(0);
        canvas.beginMarkedContentSequence(current);
    }
 
    /**
     * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
     *      java.lang.String, java.lang.String)
     */
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if ("chapter".equals(qName)) return;
        try {
            String s = buf.toString().trim();
            buf = new StringBuffer();
            if (s.length() > 0) {
                Paragraph p = new Paragraph(s, font);
                p.setAlignment(Element.ALIGN_JUSTIFIED);
                column.addElement(p);
                int status = column.go();
                while (ColumnText.hasMoreText(status)) {
                    canvas.endMarkedContentSequence();
                    document.newPage();
                    canvas.beginMarkedContentSequence(current);
                    column.setSimpleColumn(36, 36, 384, 569);
                    status = column.go();
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        canvas.endMarkedContentSequence();
    }
}
/*
 * 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.chapter15;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
 
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfStructureElement;
import com.itextpdf.text.pdf.PdfStructureTreeRoot;
import com.itextpdf.text.pdf.PdfWriter;
 
public class StructuredContent {
 
    /** The resulting PDF. */
    public static final String RESULT = "results/part4/chapter15/moby.pdf";
    /** An XML file that will be converted to PDF. */
    public static final String RESOURCE = "resources/xml/moby.xml";
 
    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     * @throws SAXException
     * @throws ParserConfigurationException
     */
    public static void main(String[] args)
        throws IOException, DocumentException, SAXException, ParserConfigurationException {
        Document document = new Document(PageSize.A5);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        writer.setTagged();
        document.open();
        PdfStructureTreeRoot root = writer.getStructureTreeRoot();
        root.mapRole(new PdfName("chapter"), PdfName.SECT);
        root.mapRole(new PdfName("title"), PdfName.H);
        root.mapRole(new PdfName("para"), PdfName.P);
        PdfStructureElement top = new PdfStructureElement(root, new PdfName("chapter"));
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        List<PdfStructureElement> elements = new ArrayList<PdfStructureElement>();
        parser.parse(
            new InputSource(new FileInputStream(RESOURCE)),
            new StructureParser(top, elements));
        parser.parse(
            new InputSource(new FileInputStream(RESOURCE)),
            new ContentParser(document, writer, elements));
        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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
 
import javax.xml.parsers.ParserConfigurationException;
 
import org.xml.sax.SAXException;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.TaggedPdfReaderTool;
 
public class ParseTaggedPdf {
 
    /** The resulting XML file. */
    public static final String RESULT
        = "results/part4/chapter15/moby_extracted.xml";
 
    /**
     * Creates a PDF file using a previous example,
     * then parses the document.
     * @param    args    no arguments needed
     */
    public static void main(String[] args)
        throws IOException, DocumentException, SAXException, ParserConfigurationException {
        StructuredContent.main(args);
        TaggedPdfReaderTool readertool = new TaggedPdfReaderTool();
        PdfReader reader = new PdfReader(StructuredContent.RESULT);
        readertool.convertToXml(reader, new FileOutputStream(RESULT));
        reader.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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
 
import part1.chapter01.HelloWorld;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.io.RandomAccessSourceFactory;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PRTokeniser;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
import com.itextpdf.text.pdf.parser.ContentByteUtils;
import com.itextpdf.text.pdf.parser.PdfContentStreamProcessor;
import com.itextpdf.text.pdf.parser.RenderListener;
 
public class ParsingHelloWorld {
 
    /** The resulting PDF. */
    public static final String PDF = "results/part4/chapter15/hello_reverse.pdf";
    /** A possible resulting after parsing the PDF. */
    public static final String TEXT1 = "results/part4/chapter15/result1.txt";
    /** A possible resulting after parsing the PDF. */
    public static final String TEXT2 = "results/part4/chapter15/result2.txt";
    /** A possible resulting after parsing the PDF. */
    public static final String TEXT3 = "results/part4/chapter15/result3.txt";
 
    /**
     * Generates a PDF file with the text 'Hello World'
     * @throws DocumentException 
     * @throws IOException 
     */
    public void createPdf(String filename) throws DocumentException, IOException {
        // step 1
    	Document document = new Document();
        // step 2
    	PdfWriter writer
          = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
    	document.open();
    	// step 4
        // we add the text to the direct content, but not in the right order
        PdfContentByte cb = writer.getDirectContent();
        BaseFont bf = BaseFont.createFont();
        cb.beginText();
        cb.setFontAndSize(bf, 12);
        cb.moveText(88.66f, 367); 
        cb.showText("ld");
        cb.moveText(-22f, 0); 
        cb.showText("Wor");
        cb.moveText(-15.33f, 0); 
        cb.showText("llo");
        cb.moveText(-15.33f, 0); 
        cb.showText("He");
        cb.endText();
        // we also add text in a form XObject
        PdfTemplate tmp = cb.createTemplate(250, 25);
        tmp.beginText();
        tmp.setFontAndSize(bf, 12);
        tmp.moveText(0, 7);
        tmp.showText("Hello People");
        tmp.endText();
        cb.addTemplate(tmp, 36, 343);
        // step 5
        document.close();
    }
 
    /**
     * Parses the PDF using PRTokeniser
     * @param src  the path to the original PDF file
     * @param dest the path to the resulting text file
     * @throws IOException
     */
    public void parsePdf(String src, String dest) throws IOException {
        PdfReader reader = new PdfReader(src);
        // we can inspect the syntax of the imported page
        byte[] streamBytes = reader.getPageContent(1);
        PRTokeniser tokenizer = new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().createSource(streamBytes)));
        PrintWriter out = new PrintWriter(new FileOutputStream(dest));
        while (tokenizer.nextToken()) {
            if (tokenizer.getTokenType() == PRTokeniser.TokenType.STRING) {
                out.println(tokenizer.getStringValue());
            }
        }
        out.flush();
        out.close();
        reader.close();
    }
 
    /**
     * Extracts text from a PDF document.
     * @param src  the original PDF document
     * @param dest the resulting text file
     * @throws IOException
     */
    public void extractText(String src, String dest) throws IOException {
        PrintWriter out = new PrintWriter(new FileOutputStream(dest));
        PdfReader reader = new PdfReader(src);
        RenderListener listener = new MyTextRenderListener(out);
        PdfContentStreamProcessor processor = new PdfContentStreamProcessor(listener);
        PdfDictionary pageDic = reader.getPageN(1);
        PdfDictionary resourcesDic = pageDic.getAsDict(PdfName.RESOURCES);
        processor.processContent(ContentByteUtils.getContentBytesForPage(reader, 1), resourcesDic);
        out.flush();
        out.close();
        reader.close();
    }
 
    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws DocumentException, IOException {
        ParsingHelloWorld example = new ParsingHelloWorld();
        HelloWorld.main(args);
        example.createPdf(PDF);
        example.parsePdf(HelloWorld.RESULT, TEXT1);
        example.parsePdf(PDF, TEXT2);
        example.extractText(PDF, TEXT3);
    }
}
/*
 * 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.chapter15;
 
import java.io.PrintWriter;
 
import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;
 
public class MyTextRenderListener implements RenderListener {
 
	/** The print writer to which the information will be written. */
    protected PrintWriter out;
 
    /**
     * Creates a RenderListener that will look for text.
     */
    public MyTextRenderListener(PrintWriter out) {
        this.out = out;
    }
 
    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#beginTextBlock()
     */
    public void beginTextBlock() {
        out.print("<");
    }
 
    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#endTextBlock()
     */
    public void endTextBlock() {
        out.println(">");
    }
 
    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#renderImage(
     *     com.itextpdf.text.pdf.parser.ImageRenderInfo)
     */
    public void renderImage(ImageRenderInfo renderInfo) {
    }
 
    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#renderText(
     *     com.itextpdf.text.pdf.parser.TextRenderInfo)
     */
    public void renderText(TextRenderInfo renderInfo) {
        out.print("<");
        out.print(renderInfo.getText());
        out.print(">");
    }
}
/*
 * 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.chapter15;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
 
import part1.chapter03.MovieTemplates;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.parser.PdfContentReaderTool;
 
public class InspectPageContent {
 
    /** Text file containing information about a PDF file. */
    public static final String RESULT = "results/part4/chapter15/calendar_info.txt";
 
    /**
     * Parses object and content information of a PDF into a text file.
     * @param pdf the original PDF
     * @param txt the resulting text
     * @throws IOException
     */
    public void inspectPdf(String pdf, String txt) throws IOException {
        PrintWriter out = new PrintWriter(new FileOutputStream(txt));
        PdfContentReaderTool.listContentStream(new File(pdf), out);
        out.flush();
        out.close();
    }
 
    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new MovieTemplates().createPdf(MovieTemplates.RESULT);
        new InspectPageContent().inspectPdf(MovieTemplates.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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
 
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
 
public class ExtractPageContent {
 
    /** The original PDF that will be parsed. */
    public static final String PREFACE = "resources/pdfs/preface.pdf";
    /** The resulting text file. */
    public static final String RESULT = "results/part4/chapter15/preface.txt";
 
    /**
     * Parses a PDF to a plain text file.
     * @param pdf the original PDF
     * @param txt the resulting text
     * @throws IOException
     */
    public void parsePdf(String pdf, String txt) throws IOException {
        PdfReader reader = new PdfReader(pdf);
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        PrintWriter out = new PrintWriter(new FileOutputStream(txt));
        TextExtractionStrategy strategy;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
            out.println(strategy.getResultantText());
        }
        out.flush();
        out.close();
        reader.close();
    }
 
    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        new ExtractPageContent().parsePdf(PREFACE, 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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
 
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.LocationTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
 
public class ExtractPageContentSorted1 {
 
    /** The original PDF that will be parsed. */
    public static final String PREFACE = "resources/pdfs/preface.pdf";
    /** The resulting text file. */
    public static final String RESULT = "results/part4/chapter15/preface_sorted1.txt";
 
    /**
     * Parses a PDF to a plain text file.
     * @param pdf the original PDF
     * @param txt the resulting text
     * @throws IOException
     */
    public void parsePdf(String pdf, String txt) throws IOException {
        PdfReader reader = new PdfReader(pdf);
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        PrintWriter out = new PrintWriter(new FileOutputStream(txt));
        TextExtractionStrategy strategy;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            strategy = parser.processContent(i, new LocationTextExtractionStrategy());
            out.println(strategy.getResultantText());
        }
        out.flush();
        out.close();
        reader.close();
    }
 
    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        new ExtractPageContentSorted1().parsePdf(PREFACE, 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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
 
public class ExtractPageContentSorted2 {
 
    /** The original PDF that will be parsed. */
    public static final String PREFACE = "resources/pdfs/preface.pdf";
    /** The resulting text file. */
    public static final String RESULT = "results/part4/chapter15/preface_sorted2.txt";
 
    /**
     * Parses a specific area of a PDF to a plain text file.
     * @param pdf the original PDF
     * @param txt the resulting text
     * @throws IOException
     */
    public void parsePdf(String pdf, String txt) throws IOException {
        PdfReader reader = new PdfReader(pdf);
        PrintWriter out = new PrintWriter(new FileOutputStream(txt));
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            out.println(PdfTextExtractor.getTextFromPage(reader, i));
        }
        out.flush();
        out.close();
        reader.close();
    }
 
    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new ExtractPageContentSorted2().parsePdf(PREFACE, 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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.FilteredTextRenderListener;
import com.itextpdf.text.pdf.parser.LocationTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import com.itextpdf.text.pdf.parser.RegionTextRenderFilter;
import com.itextpdf.text.pdf.parser.RenderFilter;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
 
public class ExtractPageContentArea {
 
    /** The original PDF that will be parsed. */
    public static final String PREFACE = "resources/pdfs/preface.pdf";
    /** The resulting text file. */
    public static final String RESULT = "results/part4/chapter15/preface_clipped.txt";
 
    /**
     * Parses a specific area of a PDF to a plain text file.
     * @param pdf the original PDF
     * @param txt the resulting text
     * @throws IOException
     */
    public void parsePdf(String pdf, String txt) throws IOException {
        PdfReader reader = new PdfReader(pdf);
        PrintWriter out = new PrintWriter(new FileOutputStream(txt));
        Rectangle rect = new Rectangle(70, 80, 490, 580);
        RenderFilter filter = new RegionTextRenderFilter(rect);
        TextExtractionStrategy strategy;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filter);
            out.println(PdfTextExtractor.getTextFromPage(reader, i, strategy));
        }
        out.flush();
        out.close();
        reader.close();
    }
 
    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new ExtractPageContentArea().parsePdf(PREFACE, 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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.TextMarginFinder;
 
public class ShowTextMargins {
 
    /** The original document. */
    public static final String PREFACE = "resources/pdfs/preface.pdf";
    /** The new document to which we've added a border rectangle. */
    public static final String RESULT = "results/part4/chapter15/margins.pdf";
 
    /**
     * Parses a PDF and ads a rectangle showing the text margin.
     * @param src the source PDF
     * @param dest the resulting PDF
     */
    public void addMarginRectangle(String src, String dest)
        throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        TextMarginFinder finder;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            finder = parser.processContent(i, new TextMarginFinder());
            PdfContentByte cb = stamper.getOverContent(i);
            cb.rectangle(finder.getLlx(), finder.getLly(),
                finder.getWidth(), finder.getHeight());
            cb.stroke();
        }
        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 ShowTextMargins().addMarginRectangle(PREFACE, 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.chapter15;
 
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.PdfImageObject;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;
 
public class MyImageRenderListener implements RenderListener {
 
    /** The new document to which we've added a border rectangle. */
    protected String path = "";
 
    /**
     * Creates a RenderListener that will look for images.
     */
    public MyImageRenderListener(String path) {
        this.path = path;
    }
 
    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#beginTextBlock()
     */
    public void beginTextBlock() {
    }
 
    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#endTextBlock()
     */
    public void endTextBlock() {
    }
 
    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#renderImage(
     *     com.itextpdf.text.pdf.parser.ImageRenderInfo)
     */
    public void renderImage(ImageRenderInfo renderInfo) {
        try {
            String filename;
            FileOutputStream os;
            PdfImageObject image = renderInfo.getImage();
            if (image == null) return;
            filename = String.format(path, renderInfo.getRef().getNumber(), image.getFileType());
            os = new FileOutputStream(filename);
            os.write(image.getImageAsBytes());
            os.flush();
            os.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
 
    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#renderText(
     *     com.itextpdf.text.pdf.parser.TextRenderInfo)
     */
    public void renderText(TextRenderInfo renderInfo) {
    }
}
/*
 * 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.chapter15;
 
import java.io.IOException;
 
import part3.chapter10.ImageTypes;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
 
/**
 * Extracts images from a PDF file.
 */
public class ExtractImages {
 
    /** The new document to which we've added a border rectangle. */
    public static final String RESULT = "results/part4/chapter15/Img%s.%s";
 
    /**
     * Parses a PDF and extracts all the images.
     * @param src the source PDF
     * @param dest the resulting PDF
     */
    public void extractImages(String filename)
        throws IOException, DocumentException {
        PdfReader reader = new PdfReader(filename);
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        MyImageRenderListener listener = new MyImageRenderListener(RESULT);
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            parser.processContent(i, listener);
        }
        reader.close();
    }
 
    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new ImageTypes().createPdf(ImageTypes.RESULT);
        new ExtractImages().extractImages(ImageTypes.RESULT);
    }
}