com.lowagie.text.pdf.PdfAction Java Examples

The following examples show how to use com.lowagie.text.pdf.PdfAction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OptionalContentTest.java    From itext2 with GNU Lesser General Public License v3.0 7 votes vote down vote up
/**
 * Demonstrates the use of layers.
 * 
 * @param args
 *            no arguments needed
 */
@Test
public void main() throws Exception {
	// step 1: creation of a document-object
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	// step 2: creation of the writer
	PdfWriter writer = PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("optionalcontent.pdf"));
	writer.setPdfVersion(PdfWriter.VERSION_1_5);
	writer.setViewerPreferences(PdfWriter.PageModeUseOC);
	// step 3: opening the document
	document.open();
	// step 4: content
	PdfContentByte cb = writer.getDirectContent();
	Phrase explanation = new Phrase(
			"Automatic layers, form fields, images, templates and actions",
			new Font(Font.HELVETICA, 18, Font.BOLD, Color.red));
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50,
			650, 0);
	PdfLayer l1 = new PdfLayer("Layer 1", writer);
	PdfLayer l2 = new PdfLayer("Layer 2", writer);
	PdfLayer l3 = new PdfLayer("Layer 3", writer);
	PdfLayer l4 = new PdfLayer("Form and XObject Layer", writer);
	PdfLayerMembership m1 = new PdfLayerMembership(writer);
	m1.addMember(l2);
	m1.addMember(l3);
	Phrase p1 = new Phrase("Text in layer 1");
	Phrase p2 = new Phrase("Text in layer 2 or layer 3");
	Phrase p3 = new Phrase("Text in layer 3");
	cb.beginLayer(l1);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0f);
	cb.endLayer();
	cb.beginLayer(m1);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0);
	cb.endLayer();
	cb.beginLayer(l3);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0);
	cb.endLayer();
	TextField ff = new TextField(writer, new Rectangle(200, 600, 300, 620),
			"field1");
	ff.setBorderColor(Color.blue);
	ff.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
	ff.setBorderWidth(TextField.BORDER_WIDTH_THIN);
	ff.setText("I'm a form field");
	PdfFormField form = ff.getTextField();
	form.setLayer(l4);
	writer.addAnnotation(form);
	Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR
			+ "pngnow.png");
	img.setLayer(l4);
	img.setAbsolutePosition(200, 550);
	cb.addImage(img);
	PdfTemplate tp = cb.createTemplate(100, 20);
	Phrase pt = new Phrase("I'm a template", new Font(Font.HELVETICA, 12,
			Font.NORMAL, Color.magenta));
	ColumnText.showTextAligned(tp, Element.ALIGN_LEFT, pt, 0, 0, 0);
	tp.setLayer(l4);
	tp.setBoundingBox(new Rectangle(0, -10, 100, 20));
	cb.addTemplate(tp, 200, 500);
	ArrayList<Object> state = new ArrayList<Object>();
	state.add("toggle");
	state.add(l1);
	state.add(l2);
	state.add(l3);
	state.add(l4);
	PdfAction action = PdfAction.setOCGstate(state, true);
	Chunk ck = new Chunk("Click here to toggle the layers", new Font(
			Font.HELVETICA, 18, Font.NORMAL, Color.yellow)).setBackground(
			Color.blue).setAction(action);
	ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(ck),
			250, 400, 0);
	cb.sanityCheck();

	// step 5: closing the document
	document.close();
}
 
Example #2
Source File: OpenApplicationTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a document with Named Actions.
 * 
 * @param args The file to open
 */
public void main(String... args) throws Exception {

	// step 1: creation of a document-object
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);

	// step 2: we create a writer that listens to the document
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("OpenApplication.pdf"));
	// step 3: we open the document
	document.open();
	// step 4: we add some content
	String application = args[0];
	Paragraph p = new Paragraph(new Chunk("Click to open " + application).setAction(
			new PdfAction(application, null, null, null)));
	document.add(p);

	// step 5: we close the document
	document.close();

}
 
Example #3
Source File: PdfLogicalPageDrawable.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void drawHyperlink( final RenderNode box, final String target, final String window, final String title ) {
  if ( box.isNodeVisible( getDrawArea() ) == false ) {
    return;
  }

  final PdfAction action = createActionForLink( target );

  final AffineTransform affineTransform = getGraphics().getTransform();
  final float translateX = (float) affineTransform.getTranslateX();

  final float leftX = translateX + (float) ( StrictGeomUtility.toExternalValue( box.getX() ) );
  final float rightX = translateX + (float) ( StrictGeomUtility.toExternalValue( box.getX() + box.getWidth() ) );
  final float lowerY = (float) ( globalHeight - StrictGeomUtility.toExternalValue( box.getY() + box.getHeight() ) );
  final float upperY = (float) ( globalHeight - StrictGeomUtility.toExternalValue( box.getY() ) );

  if ( action != null ) {
    final PdfAnnotation annotation = new PdfAnnotation( writer, leftX, lowerY, rightX, upperY, action );
    writer.addAnnotation( annotation );
  } else if ( StringUtils.isEmpty( title ) == false ) {
    final Rectangle rect = new Rectangle( leftX, lowerY, rightX, upperY );
    final PdfAnnotation commentAnnotation = PdfAnnotation.createText( writer, rect, "Tooltip", title, false, null );
    commentAnnotation.setAppearance( PdfAnnotation.APPEARANCE_NORMAL, writer.getDirectContent().createAppearance(
        rect.getWidth(), rect.getHeight() ) );
    writer.addAnnotation( commentAnnotation );
  }
}
 
Example #4
Source File: PdfLogicalPageDrawable.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected boolean drawPdfScript( final RenderNode box ) {
  final Object attribute =
      box.getAttributes().getAttribute( AttributeNames.Pdf.NAMESPACE, AttributeNames.Pdf.SCRIPT_ACTION );
  if ( attribute == null ) {
    return false;
  }

  final String attributeText = String.valueOf( attribute );
  final PdfAction action = PdfAction.javaScript( attributeText, writer, false );

  final AffineTransform affineTransform = getGraphics().getTransform();
  final float translateX = (float) affineTransform.getTranslateX();

  final float leftX = translateX + (float) ( StrictGeomUtility.toExternalValue( box.getX() ) );
  final float rightX = translateX + (float) ( StrictGeomUtility.toExternalValue( box.getX() + box.getWidth() ) );
  final float lowerY = (float) ( globalHeight - StrictGeomUtility.toExternalValue( box.getY() + box.getHeight() ) );
  final float upperY = (float) ( globalHeight - StrictGeomUtility.toExternalValue( box.getY() ) );
  final PdfAnnotation annotation = new PdfAnnotation( writer, leftX, lowerY, rightX, upperY, action );
  writer.addAnnotation( annotation );
  return true;
}
 
Example #5
Source File: JavaScriptActionTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a document with a javascript action.
 * 
 */
@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("JavaScriptAction.pdf"));
	// step 3: we add Javascript as Metadata and we open the document
	document.open();
	// step 4: we add some content
	Paragraph p = new Paragraph(new Chunk("Click to say Hello").setAction(PdfAction.javaScript(
			"app.alert('Hello');\r", writer)));
	document.add(p);

	// step 5: we close the document
	document.close();

}
 
Example #6
Source File: ChainedActionsTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a document with chained 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("ChainedActions.pdf"));
	// step 3: we add Javascript as Metadata and we open the document
	document.open();
	// step 4: we add some content
	PdfAction action = PdfAction.javaScript("app.alert('Welcome at my site');\r", writer);
	action.next(new PdfAction("http://www.lowagie.com/iText/"));
	Paragraph p = new Paragraph(new Chunk("Click to go to Bruno's site").setAction(action));
	document.add(p);

	// step 5: we close the document
	document.close();

}
 
Example #7
Source File: ActionsTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 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();
}
 
Example #8
Source File: PdfLogicalPageDrawable.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
private PdfAction createActionForLink( final String target ) {
  if ( StringUtils.isEmpty( target ) ) {
    return null;
  }
  final PdfAction action = new PdfAction();
  if ( target.startsWith( "#" ) ) {
    // its a local link ..
    action.put( PdfName.S, PdfName.GOTO );
    action.put( PdfName.D, new PdfString( target.substring( 1 ) ) );
  } else {
    action.put( PdfName.S, PdfName.URI );
    action.put( PdfName.URI, new PdfString( target ) );
  }
  return action;
}
 
Example #9
Source File: PdfAnnotationsImp.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
public static PdfAnnotation convertAnnotation(PdfWriter writer, Annotation annot, Rectangle defaultRect) throws IOException {
	switch (annot.annotationType()) {
		case Annotation.URL_NET:
			return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((URL) annot.attributes().get(Annotation.URL)));
		case Annotation.URL_AS_STRING:
			return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE)));
		case Annotation.FILE_DEST:
			return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE), (String) annot.attributes().get(Annotation.DESTINATION)));
		case Annotation.SCREEN:
			boolean sparams[] = (boolean[]) annot.attributes().get(Annotation.PARAMETERS);
			String fname = (String) annot.attributes().get(Annotation.FILE);
			String mimetype = (String) annot.attributes().get(Annotation.MIMETYPE);
			PdfFileSpecification fs;
			if (sparams[0]) {
				fs = PdfFileSpecification.fileEmbedded(writer, fname, fname, null);
			} else {
				fs = PdfFileSpecification.fileExtern(writer, fname);
			}
			PdfAnnotation ann = PdfAnnotation.createScreen(writer, new Rectangle(annot.llx(), annot.lly(), annot.urx(), annot.ury()), fname, fs, mimetype, sparams[1]);
			return ann;
		case Annotation.FILE_PAGE:
			return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE), ((Integer) annot.attributes().get(Annotation.PAGE)).intValue()));
		case Annotation.NAMED_DEST:
			return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction(((Integer) annot.attributes().get(Annotation.NAMED)).intValue()));
		case Annotation.LAUNCH:
			return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.APPLICATION), (String) annot.attributes().get(Annotation.PARAMETERS), (String) annot.attributes().get(Annotation.OPERATION), (String) annot.attributes().get(Annotation.DEFAULTDIR)));
		default:
			return new PdfAnnotation(writer, defaultRect.getLeft(), defaultRect.getBottom(), defaultRect.getRight(), defaultRect.getTop(), new PdfString(annot.title(), PdfObject.TEXT_UNICODE), new PdfString(annot.content(), PdfObject.TEXT_UNICODE));
	}
}
 
Example #10
Source File: NamedActionsTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a document with Named Actions.
 * 
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);

	// step 2: we create a writer that listens to the document
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("NamedActions.pdf"));
	// step 3: we open the document
	document.open();
	// step 4: we add some content
	Paragraph p = new Paragraph(new Chunk("Click to print").setAction(new PdfAction(PdfAction.PRINTDIALOG)));
	PdfPTable table = new PdfPTable(4);
	table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell(new Phrase(new Chunk("First Page").setAction(new PdfAction(PdfAction.FIRSTPAGE))));
	table.addCell(new Phrase(new Chunk("Prev Page").setAction(new PdfAction(PdfAction.PREVPAGE))));
	table.addCell(new Phrase(new Chunk("Next Page").setAction(new PdfAction(PdfAction.NEXTPAGE))));
	table.addCell(new Phrase(new Chunk("Last Page").setAction(new PdfAction(PdfAction.LASTPAGE))));
	for (int k = 1; k <= 10; ++k) {
		document.add(new Paragraph("This is page " + k));
		document.add(Chunk.NEWLINE);
		document.add(table);
		document.add(p);
		document.newPage();
	}

	// step 5: we close the document
	document.close();

}
 
Example #11
Source File: PdfAnnotationsImp.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static PdfAnnotation convertAnnotation(PdfWriter writer, Annotation annot, Rectangle defaultRect) throws IOException {
     switch(annot.annotationType()) {
        case Annotation.URL_NET:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((URL) annot.attributes().get(Annotation.URL)));
        case Annotation.URL_AS_STRING:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE)));
        case Annotation.FILE_DEST:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE), (String) annot.attributes().get(Annotation.DESTINATION)));
        case Annotation.SCREEN:
            boolean sparams[] = (boolean[])annot.attributes().get(Annotation.PARAMETERS);
            String fname = (String) annot.attributes().get(Annotation.FILE);
            String mimetype = (String) annot.attributes().get(Annotation.MIMETYPE);
            PdfFileSpecification fs;
            if (sparams[0])
                fs = PdfFileSpecification.fileEmbedded(writer, fname, fname, null);
            else
                fs = PdfFileSpecification.fileExtern(writer, fname);
            PdfAnnotation ann = PdfAnnotation.createScreen(writer, new Rectangle(annot.llx(), annot.lly(), annot.urx(), annot.ury()),
                    fname, fs, mimetype, sparams[1]);
            return ann;
        case Annotation.FILE_PAGE:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE), ((Integer) annot.attributes().get(Annotation.PAGE)).intValue()));
        case Annotation.NAMED_DEST:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction(((Integer) annot.attributes().get(Annotation.NAMED)).intValue()));
        case Annotation.LAUNCH:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.APPLICATION),(String) annot.attributes().get(Annotation.PARAMETERS),(String) annot.attributes().get(Annotation.OPERATION),(String) annot.attributes().get(Annotation.DEFAULTDIR)));
        default:
     	   return new PdfAnnotation(writer, defaultRect.getLeft(), defaultRect.getBottom(), defaultRect.getRight(), defaultRect.getTop(), new PdfString(annot.title(), PdfObject.TEXT_UNICODE), new PdfString(annot.content(), PdfObject.TEXT_UNICODE));
    }
}
 
Example #12
Source File: TableEvents2Test.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable, float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
 */
public void tableLayout(PdfPTable table, float[][] width, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
	
	// widths of the different cells of the first row
    float widths[] = width[0];
    
    PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
    cb.saveState();
	// border for the complete table
    cb.setLineWidth(2);
    cb.setRGBColorStroke(255, 0, 0);
    cb.rectangle(widths[0], heights[heights.length - 1], widths[widths.length - 1] - widths[0], heights[0] - heights[heights.length - 1]);
    cb.stroke();
    
    // border for the header rows
    if (headerRows > 0) {
        cb.setRGBColorStroke(0, 0, 255);
        cb.rectangle(widths[0], heights[headerRows], widths[widths.length - 1] - widths[0], heights[0] - heights[headerRows]);
        cb.stroke();
    }
    cb.restoreState();
    
    cb = canvases[PdfPTable.BASECANVAS];
    cb.saveState();
    // border for the cells
    cb.setLineWidth(.5f);
    // loop over the rows
    for (int line = 0; line < heights.length - 1; ++line) {
        widths = width[line];
    	// loop over the columns
        for (int col = 0; col < widths.length - 1; ++col) {
            if (line == 0 && col == 0)
                cb.setAction(new PdfAction("http://www.lowagie.com/iText/"),
                    widths[col], heights[line + 1], widths[col + 1], heights[line]);
            cb.setRGBColorStrokeF((float)Math.random(), (float)Math.random(), (float)Math.random());
            // horizontal borderline
            cb.moveTo(widths[col], heights[line]);
            cb.lineTo(widths[col + 1], heights[line]);
            cb.stroke();
            // vertical borderline
            cb.setRGBColorStrokeF((float)Math.random(), (float)Math.random(), (float)Math.random());
            cb.moveTo(widths[col], heights[line]);
            cb.lineTo(widths[col], heights[line + 1]);
            cb.stroke();
        }
    }
    cb.restoreState();
}
 
Example #13
Source File: SimpleAnnotationsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates documents with some simple annotations.
 * 
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document1 = new Document(PageSize.A4, 10, 10, 10, 10);
	Document document2 = new Document(PageSize.A4, 10, 10, 10, 10);

	// step 2:
	PdfWriter writer1 = PdfWriter.getInstance(document1, PdfTestBase.getOutputStream("SimpleAnnotations1.pdf"));
	PdfWriter writer2 = PdfWriter.getInstance(document2, PdfTestBase.getOutputStream("SimpleAnnotations2.pdf"));
	// step 3:
	writer2.setPdfVersion(PdfWriter.VERSION_1_5);
	document1.open();
	document2.open();
	// step 4:
	document1.add(new Paragraph("Each square on this page represents an annotation."));
	// document1
	PdfContentByte cb1 = writer1.getDirectContent();
	Annotation a1 = new Annotation("authors",
			"Maybe it's because I wanted to be an author myself that I wrote iText.", 250f, 700f, 350f, 800f);
	document1.add(a1);
	Annotation a2 = new Annotation(250f, 550f, 350f, 650f, new URL("http://www.lowagie.com/iText/"));
	document1.add(a2);
	Annotation a3 = new Annotation(250f, 400f, 350f, 500f, "http://www.lowagie.com/iText");
	document1.add(a3);
	Image image = Image.getInstance(PdfTestBase.RESOURCES_DIR + "iText.gif");
	image.setAnnotation(a3);
	document1.add(image);
	Annotation a4 = new Annotation(250f, 250f, 350f, 350f, PdfAction.LASTPAGE);
	document1.add(a4);
	// draw rectangles to show where the annotations were added
	cb1.rectangle(250, 700, 100, 100);
	cb1.rectangle(250, 550, 100, 100);
	cb1.rectangle(250, 400, 100, 100);
	cb1.rectangle(250, 250, 100, 100);
	cb1.stroke();
	// more content
	document1.newPage();
	for (int i = 0; i < 5; i++) {
		document1.add(new Paragraph("blahblahblah"));
	}
	document1.add(new Annotation("blahblah", "Adding an annotation without specifying coordinates"));
	for (int i = 0; i < 3; i++) {
		document1.add(new Paragraph("blahblahblah"));
	}
	document1.newPage();
	document1.add(new Chunk("marked chunk").setLocalDestination("mark"));
	
	File videoFile = new File(PdfTestBase.RESOURCES_DIR + "cards.mpg");
	File license = new File("LICENSE");

	// document2
	document2.add(new Paragraph("Each square on this page represents an annotation."));
	PdfContentByte cb2 = writer2.getDirectContent();
	Annotation a5 = new Annotation(100f, 700f, 200f, 800f, videoFile.getAbsolutePath(), "video/mpeg", true);
	document2.add(a5);
	Annotation a6 = new Annotation(100f, 550f, 200f, 650f, "SimpleAnnotations1.pdf", "mark");
	document2.add(a6);
	Annotation a7 = new Annotation(100f, 400f, 200f, 500f, "SimpleAnnotations1.pdf", 2);
	document2.add(a7);
	Annotation a8 = new Annotation(100f, 250f, 200f, 350f, license.getAbsolutePath(), null, null, null);
	document2.add(a8);
	// draw rectangles to show where the annotations were added
	cb2.rectangle(100, 700, 100, 100);
	cb2.rectangle(100, 550, 100, 100);
	cb2.rectangle(100, 400, 100, 100);
	cb2.rectangle(100, 250, 100, 100);
	cb2.stroke();

	// step 5: we close the document
	document1.close();
	document2.close();
}
 
Example #14
Source File: AnnotationsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 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();
}
 
Example #15
Source File: OutlineActionsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 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();
}
 
Example #16
Source File: TableEvents1Test.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable,
 *      float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
 */
public void tableLayout(PdfPTable table, float[][] width, float[] heights, int headerRows, int rowStart,
		PdfContentByte[] canvases) {

	// widths of the different cells of the first row
	float widths[] = width[0];

	PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
	cb.saveState();
	// border for the complete table
	cb.setLineWidth(2);
	cb.setRGBColorStroke(255, 0, 0);
	cb.rectangle(widths[0], heights[heights.length - 1], widths[widths.length - 1] - widths[0], heights[0]
			- heights[heights.length - 1]);
	cb.stroke();

	// border for the header rows
	if (headerRows > 0) {
		cb.setRGBColorStroke(0, 0, 255);
		cb.rectangle(widths[0], heights[headerRows], widths[widths.length - 1] - widths[0], heights[0]
				- heights[headerRows]);
		cb.stroke();
	}
	cb.restoreState();

	cb = canvases[PdfPTable.BASECANVAS];
	cb.saveState();
	// border for the cells
	cb.setLineWidth(.5f);
	// loop over the rows
	for (int line = 0; line < heights.length - 1; ++line) {
		// loop over the columns
		for (int col = 0; col < widths.length - 1; ++col) {
			if (line == 0 && col == 0)
				cb.setAction(new PdfAction("http://www.lowagie.com/iText/"), widths[col], heights[line + 1],
						widths[col + 1], heights[line]);
			cb.setRGBColorStrokeF((float) Math.random(), (float) Math.random(), (float) Math.random());
			// horizontal borderline
			cb.moveTo(widths[col], heights[line]);
			cb.lineTo(widths[col + 1], heights[line]);
			cb.stroke();
			// vertical borderline
			cb.setRGBColorStrokeF((float) Math.random(), (float) Math.random(), (float) Math.random());
			cb.moveTo(widths[col], heights[line]);
			cb.lineTo(widths[col], heights[line + 1]);
			cb.stroke();
		}
	}
	cb.restoreState();
}
 
Example #17
Source File: PDFPage.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates a PdfAction.
 *
 * @param hyperlink
 *            the hyperlink.
 * @param bookmark
 *            the bookmark.
 * @param target
 *            if target equals "_blank", the target will be opened in a new
 *            window, else the target will be opened in the current window.
 * @return the created PdfAction.
 */
private PdfAction createPdfAction( String hyperlink, String bookmark,
		String target, int type )
{
	// patch from Ales Novy
	if ( "_top".equalsIgnoreCase( target )
			|| "_parent".equalsIgnoreCase( target )
			|| "_blank".equalsIgnoreCase( target )
			|| "_self".equalsIgnoreCase( target ) )
	// Opens the target in a new window.
	{
		if ( hyperlink == null )
			hyperlink = "";
		boolean isUrl = hyperlink.startsWith( "http" );
		if ( !isUrl )
		{
			Matcher matcher = PAGE_LINK_PATTERN.matcher( hyperlink );
			if ( matcher.find( ) )
			{
				String fileName = matcher.group( 1 );
				String pageNumber = matcher.group( matcher.groupCount( ) );
				return new PdfAction( fileName,
						Integer.valueOf( pageNumber ) );
			}
		}
		return new PdfAction( hyperlink );
	}
	else

	// Opens the target in the current window.
	{
		if ( type == IHyperlinkAction.ACTION_BOOKMARK )
		{
			return PdfAction.gotoLocalPage( bookmark, false );
		}
		else
		{
			return PdfAction.gotoRemotePage( hyperlink, bookmark, false,
					false );
		}
	}
}
 
Example #18
Source File: Chunk.java    From MesquiteCore with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
* Sets an anchor for this <CODE>Chunk</CODE>.
*
* @param url the url to link to
* @return this <CODE>Chunk</CODE>
*/

   public Chunk setAnchor(String url) {
       return setAttribute(ACTION, new PdfAction(url));
   }
 
Example #19
Source File: Chunk.java    From MesquiteCore with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
* Sets an action for this <CODE>Chunk</CODE>.
*
* @param action the action
* @return this <CODE>Chunk</CODE>
*/

   public Chunk setAction(PdfAction action) {
       return setAttribute(ACTION, action);
   }
 
Example #20
Source File: Chunk.java    From MesquiteCore with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
* Sets an anchor for this <CODE>Chunk</CODE>.
*
* @param url the <CODE>URL</CODE> to link to
* @return this <CODE>Chunk</CODE>
*/

   public Chunk setAnchor(URL url) {
       return setAttribute(ACTION, new PdfAction(url.toExternalForm()));
   }
 
Example #21
Source File: Chunk.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Sets an action for this <CODE>Chunk</CODE>.
 *
 * @param action the action
 * @return this <CODE>Chunk</CODE>
 */

public Chunk setAction(PdfAction action) {
	return setAttribute(ACTION, action);
}
 
Example #22
Source File: PdfPageActions.java    From itext2 with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Sets the open and close page additional action.
 * @param actionType the action type. It can be <CODE>PdfWriter.PAGE_OPEN</CODE>
 * or <CODE>PdfWriter.PAGE_CLOSE</CODE>
 * @param action the action to perform
 * @throws DocumentException if the action type is invalid
 */    
public void setPageAction(PdfName actionType, PdfAction action) throws DocumentException;
 
Example #23
Source File: PdfDocumentActions.java    From itext2 with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Additional-actions defining the actions to be taken in
 * response to various trigger events affecting the document
 * as a whole. The actions types allowed are: <CODE>DOCUMENT_CLOSE</CODE>,
 * <CODE>WILL_SAVE</CODE>, <CODE>DID_SAVE</CODE>, <CODE>WILL_PRINT</CODE>
 * and <CODE>DID_PRINT</CODE>.
 *
 * @param actionType the action type
 * @param action the action to execute in response to the trigger
 * @throws DocumentException on invalid action type
 */
public void setAdditionalAction(PdfName actionType, PdfAction action) throws DocumentException;
 
Example #24
Source File: PdfDocumentActions.java    From itext2 with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * When the document opens this <CODE>action</CODE> will be
 * invoked.
 * @param action the action to be invoked
 */
public void setOpenAction(PdfAction action);
 
Example #25
Source File: Chunk.java    From itext2 with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Sets an anchor for this <CODE>Chunk</CODE>.
 * 
 * @param url
 *            the url to link to
 * @return this <CODE>Chunk</CODE>
 */

public Chunk setAnchor(String url) {
	return setAttribute(ACTION, new PdfAction(url));
}
 
Example #26
Source File: Chunk.java    From itext2 with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Sets an anchor for this <CODE>Chunk</CODE>.
 * 
 * @param url
 *            the <CODE>URL</CODE> to link to
 * @return this <CODE>Chunk</CODE>
 */

public Chunk setAnchor(URL url) {
	return setAttribute(ACTION, new PdfAction(url.toExternalForm()));
}
 
Example #27
Source File: Chunk.java    From itext2 with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Sets an action for this <CODE>Chunk</CODE>.
 * 
 * @param action
 *            the action
 * @return this <CODE>Chunk</CODE>
 */

public Chunk setAction(PdfAction action) {
	return setAttribute(ACTION, action);
}
 
Example #28
Source File: PdfPageActions.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Sets the open and close page additional action.
 * @param actionType the action type. It can be <CODE>PdfWriter.PAGE_OPEN</CODE>
 * or <CODE>PdfWriter.PAGE_CLOSE</CODE>
 * @param action the action to perform
 * @throws DocumentException if the action type is invalid
 */    
public void setPageAction(PdfName actionType, PdfAction action) throws DocumentException;
 
Example #29
Source File: PdfDocumentActions.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Additional-actions defining the actions to be taken in
 * response to various trigger events affecting the document
 * as a whole. The actions types allowed are: <CODE>DOCUMENT_CLOSE</CODE>,
 * <CODE>WILL_SAVE</CODE>, <CODE>DID_SAVE</CODE>, <CODE>WILL_PRINT</CODE>
 * and <CODE>DID_PRINT</CODE>.
 *
 * @param actionType the action type
 * @param action the action to execute in response to the trigger
 * @throws DocumentException on invalid action type
 */
public void setAdditionalAction(PdfName actionType, PdfAction action) throws DocumentException;
 
Example #30
Source File: PdfDocumentActions.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * When the document opens this <CODE>action</CODE> will be
 * invoked.
 * @param action the action to be invoked
 */
public void setOpenAction(PdfAction action);