Search in sources :

Example 1 with PdfDestination

use of com.lowagie.text.pdf.PdfDestination in project itext2 by albfernandez.

the class DestinationsTest method main.

/**
 * Creates a document with some goto actions.
 */
@Test
public void main() throws Exception {
    // step 1: creation of a document-object
    Document document = new Document();
    // step 2:
    PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Destinations.pdf"));
    // step 3:
    writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
    document.open();
    // step 4: we grab the ContentByte and do some stuff with it
    PdfContentByte cb = writer.getDirectContent();
    // we create a PdfTemplate
    PdfTemplate template = cb.createTemplate(25, 25);
    // we add some crosses to visualize the destinations
    template.moveTo(13, 0);
    template.lineTo(13, 25);
    template.moveTo(0, 13);
    template.lineTo(50, 13);
    template.stroke();
    // we add the template on different positions
    cb.addTemplate(template, 287, 787);
    cb.addTemplate(template, 187, 487);
    cb.addTemplate(template, 487, 287);
    cb.addTemplate(template, 87, 87);
    // we define the destinations
    PdfDestination d1 = new PdfDestination(PdfDestination.XYZ, 300, 800, 0);
    PdfDestination d2 = new PdfDestination(PdfDestination.FITH, 500);
    PdfDestination d3 = new PdfDestination(PdfDestination.FITR, 200, 300, 400, 500);
    PdfDestination d4 = new PdfDestination(PdfDestination.FITBV, 100);
    PdfDestination d5 = new PdfDestination(PdfDestination.FIT);
    // we define the outlines
    PdfOutline out1 = new PdfOutline(cb.getRootOutline(), d1, "root");
    PdfOutline out2 = new PdfOutline(out1, d2, "sub 1");
    new PdfOutline(out1, d3, "sub 2");
    new PdfOutline(out2, d4, "sub 2.1");
    new PdfOutline(out2, d5, "sub 2.2");
    // step 5: we close the document
    document.close();
}
Also used : PdfWriter(com.lowagie.text.pdf.PdfWriter) PdfDestination(com.lowagie.text.pdf.PdfDestination) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) Document(com.lowagie.text.Document) PdfTemplate(com.lowagie.text.pdf.PdfTemplate) PdfOutline(com.lowagie.text.pdf.PdfOutline) Test(org.junit.Test)

Example 2 with PdfDestination

use of com.lowagie.text.pdf.PdfDestination in project itext2 by albfernandez.

the class OutlineActionsTest method main.

/**
 * Demonstrates some PageLabel functionality.
 */
@Test
public void main() throws Exception {
    // step 1: creation of a document-object
    Document document = new Document();
    Document remote = new Document();
    // step 2:
    PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("OutlineActions.pdf"));
    PdfWriter.getInstance(remote, PdfTestBase.getOutputStream("remote.pdf"));
    // step 3:
    writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
    document.open();
    remote.open();
    // step 4:
    // we add some content
    document.add(new Paragraph("Outline action example"));
    // we add the outline
    PdfContentByte cb = writer.getDirectContent();
    PdfOutline root = cb.getRootOutline();
    PdfOutline links = new PdfOutline(root, new PdfAction("http://www.lowagie.com/iText/links.html"), "Useful links");
    links.setColor(new Color(0x00, 0x80, 0x80));
    links.setStyle(Font.BOLD);
    new PdfOutline(links, new PdfAction("http://www.lowagie.com/iText"), "Bruno's iText site");
    new PdfOutline(links, new PdfAction("http://itextpdf.sourceforge.net/"), "Paulo's iText site");
    new PdfOutline(links, new PdfAction("http://sourceforge.net/projects/itext/"), "iText @ SourceForge");
    PdfOutline other = new PdfOutline(root, new PdfDestination(PdfDestination.FIT), "other actions", false);
    other.setStyle(Font.ITALIC);
    new PdfOutline(other, new PdfAction("remote.pdf", 1), "Go to yhe first page of a remote file");
    new PdfOutline(other, new PdfAction("remote.pdf", "test"), "Go to a local destination in a remote file");
    new PdfOutline(other, PdfAction.javaScript("app.alert('Hello');\r", writer), "Say Hello");
    remote.add(new Paragraph("Some remote document"));
    remote.newPage();
    Paragraph p = new Paragraph("This paragraph contains a ");
    p.add(new Chunk("local destination").setLocalDestination("test"));
    remote.add(p);
    // step 5: we close the document
    document.close();
    remote.close();
}
Also used : PdfAction(com.lowagie.text.pdf.PdfAction) PdfWriter(com.lowagie.text.pdf.PdfWriter) Color(java.awt.Color) PdfDestination(com.lowagie.text.pdf.PdfDestination) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) Document(com.lowagie.text.Document) Chunk(com.lowagie.text.Chunk) Paragraph(com.lowagie.text.Paragraph) PdfOutline(com.lowagie.text.pdf.PdfOutline) Test(org.junit.Test)

Example 3 with PdfDestination

use of com.lowagie.text.pdf.PdfDestination in project itext2 by albfernandez.

the class ActionsTest method main.

/**
 * Creates a document with some goto actions.
 */
@Test
public void main() throws Exception {
    // step 1: creation of a document-object
    Document document = new Document();
    Document remote = new Document();
    // step 2:
    PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Actions.pdf"));
    PdfWriter.getInstance(remote, PdfTestBase.getOutputStream("remote.pdf"));
    // step 3:
    document.open();
    remote.open();
    // step 4: we add some content
    PdfAction action = PdfAction.gotoLocalPage(2, new PdfDestination(PdfDestination.XYZ, -1, 10000, 0), writer);
    writer.setOpenAction(action);
    document.add(new Paragraph("Page 1"));
    document.newPage();
    document.add(new Paragraph("Page 2"));
    document.add(new Chunk("goto page 1").setAction(PdfAction.gotoLocalPage(1, new PdfDestination(PdfDestination.FITH, 500), writer)));
    document.add(Chunk.NEWLINE);
    document.add(new Chunk("goto another document").setAction(PdfAction.gotoRemotePage("remote.pdf", "test", false, true)));
    remote.add(new Paragraph("Some remote document"));
    remote.newPage();
    Paragraph p = new Paragraph("This paragraph contains a ");
    p.add(new Chunk("local destination").setLocalDestination("test"));
    remote.add(p);
    // step 5: we close the document
    document.close();
    remote.close();
}
Also used : PdfAction(com.lowagie.text.pdf.PdfAction) PdfWriter(com.lowagie.text.pdf.PdfWriter) PdfDestination(com.lowagie.text.pdf.PdfDestination) Document(com.lowagie.text.Document) Chunk(com.lowagie.text.Chunk) Paragraph(com.lowagie.text.Paragraph) Test(org.junit.Test)

Example 4 with PdfDestination

use of com.lowagie.text.pdf.PdfDestination in project itext2 by albfernandez.

the class AnnotationsTest method main.

/**
 * Creates a document with some PdfAnnotations.
 */
@Test
public void main() throws Exception {
    // step 1: creation of a document-object
    Document document = new Document();
    // step 2:
    PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Annotations.pdf"));
    // step 3:
    writer.setPdfVersion(PdfWriter.VERSION_1_5);
    document.open();
    // step 4:
    PdfContentByte cb = writer.getDirectContent();
    // page 1
    PdfFileSpecification fs = PdfFileSpecification.fileExtern(writer, PdfTestBase.RESOURCES_DIR + "cards.mpg");
    writer.addAnnotation(PdfAnnotation.createScreen(writer, new Rectangle(200f, 700f, 300f, 800f), "cards.mpg", fs, "video/mpeg", true));
    PdfAnnotation a = new PdfAnnotation(writer, 200f, 550f, 300f, 650f, PdfAction.javaScript("app.alert('Hello');\r", writer));
    document.add(new Chunk("click to trigger javascript").setAnnotation(a).setLocalDestination("top"));
    writer.addAnnotation(a);
    writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f), "This is some text", "some text".getBytes(), null, "some.txt"));
    writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(200f, 400f, 300f, 500f), "Help", "This Help annotation was made with 'createText'", false, "Help"));
    writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(200f, 250f, 300f, 350f), "Help", "This Comment annotation was made with 'createText'", true, "Comment"));
    cb.rectangle(200, 700, 100, 100);
    cb.rectangle(200, 550, 100, 100);
    cb.rectangle(200, 400, 100, 100);
    cb.rectangle(200, 250, 100, 100);
    cb.stroke();
    document.newPage();
    // page 2
    writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 700f, 300f, 800f), PdfAnnotation.HIGHLIGHT_TOGGLE, PdfAction.javaScript("app.alert('Hello');\r", writer)));
    writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 550f, 300f, 650f), PdfAnnotation.HIGHLIGHT_OUTLINE, "top"));
    writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 400f, 300f, 500f), PdfAnnotation.HIGHLIGHT_PUSH, 1, new PdfDestination(PdfDestination.FIT)));
    writer.addAnnotation(PdfAnnotation.createSquareCircle(writer, new Rectangle(200f, 250f, 300f, 350f), "This Comment annotation was made with 'createSquareCircle'", false));
    document.newPage();
    // page 3
    PdfContentByte pcb = new PdfContentByte(writer);
    pcb.setColorFill(new Color(0xFF, 0x00, 0x00));
    writer.addAnnotation(PdfAnnotation.createFreeText(writer, new Rectangle(200f, 700f, 300f, 800f), "This is some free text, blah blah blah", pcb));
    writer.addAnnotation(PdfAnnotation.createLine(writer, new Rectangle(200f, 550f, 300f, 650f), "this is a line", 200, 550, 300, 650));
    writer.addAnnotation(PdfAnnotation.createStamp(writer, new Rectangle(200f, 400f, 300f, 500f), "This is a stamp", "Stamp"));
    writer.addAnnotation(PdfAnnotation.createPopup(writer, new Rectangle(200f, 250f, 300f, 350f), "Hello, I'm a popup!", true));
    cb.rectangle(200, 700, 100, 100);
    cb.rectangle(200, 550, 100, 100);
    cb.rectangle(200, 250, 100, 100);
    cb.stroke();
    // step 5: we close the document
    document.close();
}
Also used : PdfWriter(com.lowagie.text.pdf.PdfWriter) PdfAnnotation(com.lowagie.text.pdf.PdfAnnotation) PdfFileSpecification(com.lowagie.text.pdf.PdfFileSpecification) Color(java.awt.Color) Rectangle(com.lowagie.text.Rectangle) PdfDestination(com.lowagie.text.pdf.PdfDestination) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) Document(com.lowagie.text.Document) Chunk(com.lowagie.text.Chunk) Test(org.junit.Test)

Example 5 with PdfDestination

use of com.lowagie.text.pdf.PdfDestination in project itext2 by albfernandez.

the class LocalDestinationTest method main.

/**
 * Creates a document that jumps to a Local Destination upon opening.
 */
@Test
public void main() throws Exception {
    // step 1: creation of a document-object
    Document document = new Document();
    // step 2:
    PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("LocalDestination.pdf"));
    // step 3: we open the document
    document.open();
    // step 4: we add some content
    document.add(new Paragraph("Page 1"));
    document.newPage();
    document.add(new Paragraph("This PDF file jumps directly to page 2 when opened"));
    PdfContentByte cb = writer.getDirectContent();
    cb.localDestination("page2", new PdfDestination(PdfDestination.XYZ, -1, 10000, 0));
    writer.setOpenAction("page2");
    // step 5: we close the document
    document.close();
}
Also used : PdfWriter(com.lowagie.text.pdf.PdfWriter) PdfDestination(com.lowagie.text.pdf.PdfDestination) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) Document(com.lowagie.text.Document) Paragraph(com.lowagie.text.Paragraph) Test(org.junit.Test)

Aggregations

PdfDestination (com.lowagie.text.pdf.PdfDestination)16 PdfContentByte (com.lowagie.text.pdf.PdfContentByte)12 Document (com.lowagie.text.Document)11 PdfWriter (com.lowagie.text.pdf.PdfWriter)11 PdfOutline (com.lowagie.text.pdf.PdfOutline)8 Chunk (com.lowagie.text.Chunk)6 Paragraph (com.lowagie.text.Paragraph)6 PdfAction (com.lowagie.text.pdf.PdfAction)6 FileOutputStream (java.io.FileOutputStream)6 Test (org.junit.Test)5 Color (java.awt.Color)4 DocumentException (com.lowagie.text.DocumentException)3 IOException (java.io.IOException)3 Rectangle (com.lowagie.text.Rectangle)2 PdfAnnotation (com.lowagie.text.pdf.PdfAnnotation)2 PdfFileSpecification (com.lowagie.text.pdf.PdfFileSpecification)2 PdfTemplate (com.lowagie.text.pdf.PdfTemplate)2