com.lowagie.text.pdf.PdfAnnotation Java Examples

The following examples show how to use com.lowagie.text.pdf.PdfAnnotation. 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: PDFPage.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
protected void embedFlash( String flashPath, byte[] flashData, float x,
		float y, float height, float width, String helpText, Map params )
		throws IOException
{
	y = transformY( y, height );
	contentByte.saveState( );
	PdfFileSpecification fs = PdfFileSpecification.fileEmbedded( writer,
			flashPath, helpText, flashData );
	PdfAnnotation annot = PdfAnnotation.createScreen( writer,
			new Rectangle( x, y, x + width, y + height ), helpText, fs,
			"application/x-shockwave-flash", true );
	writer.addAnnotation( annot );
	if ( helpText != null )
	{
		showHelpText( x, y, width, height, helpText );
	}
	contentByte.restoreState( );
}
 
Example #2
Source File: DefaultPdfDataEntryFormService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void setCheckboxAppearance( PdfFormField checkboxfield, PdfContentByte canvas, float width )
{
    PdfAppearance[] onOff = new PdfAppearance[2];
    onOff[0] = canvas.createAppearance( width + 2, width + 2 );
    onOff[0].rectangle( 1, 1, width, width );
    onOff[0].stroke();
    onOff[1] = canvas.createAppearance( width + 2, width + 2 );
    onOff[1].setRGBColorFill( 255, 128, 128 );
    onOff[1].rectangle( 1, 1, width, width );
    onOff[1].fillStroke();
    onOff[1].moveTo( 1, 1 );
    onOff[1].lineTo( width + 1, width + 1 );
    onOff[1].moveTo( 1, width + 1 );
    onOff[1].lineTo( width + 1, 1 );
    onOff[1].stroke();

    checkboxfield.setAppearance( PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0] );
    checkboxfield.setAppearance( PdfAnnotation.APPEARANCE_NORMAL, "On", onOff[1] );
}
 
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: FormComboTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Generates an Acroform with a Combobox
 */
@Test
public void main() throws Exception {

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

	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("combo.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	PdfContentByte cb = writer.getDirectContent();
	cb.moveTo(0, 0);
	String options[] = { "Red", "Green", "Blue" };
	PdfFormField field = PdfFormField.createCombo(writer, true, options, 0);
	field.setWidget(new Rectangle(100, 700, 180, 720), PdfAnnotation.HIGHLIGHT_INVERT);
	field.setFieldName("ACombo");
	field.setValueAsString("Red");
	writer.addAnnotation(field);

	// step 5: we close the document
	document.close();
}
 
Example #6
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 #7
Source File: PDFPage.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
protected void showHelpText( float x, float y, float width, float height,
		String helpText )
{
	Rectangle rectangle = new Rectangle( x, y, x + width, y + height );
	PdfAnnotation annotation = PdfAnnotation.createSquareCircle( writer,
			rectangle, helpText, true );
	PdfBorderDictionary borderStyle = new PdfBorderDictionary( 0,
			PdfBorderDictionary.STYLE_SOLID, null );
	annotation.setBorderStyle( borderStyle );
	annotation.setFlags( 288 );
	writer.addAnnotation( annotation );
}
 
Example #8
Source File: PDFPage.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private void createHyperlink( String hyperlink, String bookmark,
		String targetWindow, int type, float x, float y, float width,
		float height )
{
	y = transformY( y, height );
	writer.addAnnotation( new PdfAnnotation( writer, x, y, x + width, y
			+ height, createPdfAction( hyperlink, bookmark, targetWindow,
			type ) ) );
}
 
Example #9
Source File: FormPushButtonTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Generates an Acroform with a PushButton
 */
@Test
public void main() throws Exception {

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

	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("pushbutton.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	PdfFormField pushbutton = PdfFormField.createPushButton(writer);
	PdfContentByte cb = writer.getDirectContent();
	cb.moveTo(0, 0);
	PdfAppearance normal = cb.createAppearance(100, 50);
	normal.setColorFill(Color.GRAY);
	normal.rectangle(5, 5, 90, 40);
	normal.fill();
	PdfAppearance rollover = cb.createAppearance(100, 50);
	rollover.setColorFill(Color.RED);
	rollover.rectangle(5, 5, 90, 40);
	rollover.fill();
	PdfAppearance down = cb.createAppearance(100, 50);
	down.setColorFill(Color.BLUE);
	down.rectangle(5, 5, 90, 40);
	down.fill();
	pushbutton.setFieldName("PushMe");
	pushbutton.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, normal);
	pushbutton.setAppearance(PdfAnnotation.APPEARANCE_ROLLOVER, rollover);
	pushbutton.setAppearance(PdfAnnotation.APPEARANCE_DOWN, down);
	pushbutton.setWidget(new Rectangle(100, 700, 200, 750), PdfAnnotation.HIGHLIGHT_PUSH);
	writer.addAnnotation(pushbutton);

	// step 5: we close the document
	document.close();
}
 
Example #10
Source File: PdfAnnotationsImp.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void addAnnotation(PdfAnnotation annot) {
    if (annot.isForm()) {
        PdfFormField field = (PdfFormField)annot;
        if (field.getParent() == null)
            addFormFieldRaw(field);
    }
    else
        annotations.add(annot);
}
 
Example #11
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 #12
Source File: PdfAnnotationsImp.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
public void addAnnotation(PdfAnnotation annot) {
	if (annot.isForm()) {
		PdfFormField field = (PdfFormField) annot;
		if (field.getParent() == null) {
			addFormFieldRaw(field);
		}
	} else {
		annotations.add(annot);
	}
}
 
Example #13
Source File: PdfAnnotationsImp.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void addPlainAnnotation(PdfAnnotation annot) {
	annotations.add(annot);
}
 
Example #14
Source File: PdfAnnotationsImp.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
public PdfArray rotateAnnotations(PdfWriter writer, Rectangle pageSize) {
    PdfArray array = new PdfArray();
    int rotation = pageSize.getRotation() % 360;
    int currentPage = writer.getCurrentPageNumber();
    for (int k = 0; k < annotations.size(); ++k) {
        PdfAnnotation dic = (PdfAnnotation)annotations.get(k);
        int page = dic.getPlaceInPage();
        if (page > currentPage) {
            delayedAnnotations.add(dic);
            continue;
        }
        if (dic.isForm()) {
            if (!dic.isUsed()) {
                HashMap templates = dic.getTemplates();
                if (templates != null)
                    acroForm.addFieldTemplates(templates);
            }
            PdfFormField field = (PdfFormField)dic;
            if (field.getParent() == null)
                acroForm.addDocumentField(field.getIndirectReference());
        }
        if (dic.isAnnotation()) {
            array.add(dic.getIndirectReference());
            if (!dic.isUsed()) {
                PdfRectangle rect = (PdfRectangle)dic.get(PdfName.RECT);
                if (rect != null) {
                	switch (rotation) {
                    	case 90:
                    		dic.put(PdfName.RECT, new PdfRectangle(
                    				pageSize.getTop() - rect.bottom(),
						rect.left(),
						pageSize.getTop() - rect.top(),
						rect.right()));
                    		break;
                    	case 180:
                    		dic.put(PdfName.RECT, new PdfRectangle(
                    				pageSize.getRight() - rect.left(),
						pageSize.getTop() - rect.bottom(),
						pageSize.getRight() - rect.right(),
						pageSize.getTop() - rect.top()));
                    		break;
                    	case 270:
                    		dic.put(PdfName.RECT, new PdfRectangle(
                    				rect.bottom(),
						pageSize.getRight() - rect.left(),
						rect.top(),
						pageSize.getRight() - rect.right()));
                    		break;
                	}
                }
            }
        }
        if (!dic.isUsed()) {
            dic.setUsed();
            try {
                writer.addToBody(dic, dic.getIndirectReference());
            }
            catch (IOException e) {
                throw new ExceptionConverter(e);
            }
        }
    }
    return array;
}
 
Example #15
Source File: FormCheckboxTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Generates an Acroform with a Checkbox
 */
@Test
public void main() throws Exception {

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

	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("checkbox.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	PdfContentByte cb = writer.getDirectContent();
	cb.moveTo(0, 0);
	PdfFormField field = PdfFormField.createCheckBox(writer);
	PdfAppearance tpOff = cb.createAppearance(20, 20);
	PdfAppearance tpOn = cb.createAppearance(20, 20);
	tpOff.rectangle(1, 1, 18, 18);
	tpOff.stroke();

	tpOn.setRGBColorFill(255, 128, 128);
	tpOn.rectangle(1, 1, 18, 18);
	tpOn.fillStroke();
	tpOn.moveTo(1, 1);
	tpOn.lineTo(19, 19);
	tpOn.moveTo(1, 19);
	tpOn.lineTo(19, 1);
	tpOn.stroke();

	field.setWidget(new Rectangle(100, 700, 120, 720), PdfAnnotation.HIGHLIGHT_INVERT);
	field.setFieldName("Urgent");
	field.setValueAsName("Off");
	field.setAppearanceState("Off");
	field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpOff);
	field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", tpOn);
	writer.addAnnotation(field);

	// step 5: we close the document
	document.close();
}
 
Example #16
Source File: FormTextFieldTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Generates an Acroform with a TextField
 */
@Test
public void main() throws Exception {

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

	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("textfield.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	BaseFont helv = BaseFont.createFont("Helvetica", "winansi", false);
	PdfContentByte cb = writer.getDirectContent();
	cb.moveTo(0, 0);
	String text = "Some start text";
	float fontSize = 12;
	Color textColor = new GrayColor(0f);
	PdfFormField field = PdfFormField.createTextField(writer, false, false, 0);
	field.setWidget(new Rectangle(171, 750, 342, 769), PdfAnnotation.HIGHLIGHT_INVERT);
	field.setFlags(PdfAnnotation.FLAGS_PRINT);
	field.setFieldName("ATextField");
	field.setValueAsString(text);
	field.setDefaultValueAsString(text);
	field.setBorderStyle(new PdfBorderDictionary(2, PdfBorderDictionary.STYLE_SOLID));
	field.setPage();
	PdfAppearance tp = cb.createAppearance(171, 19);
	PdfAppearance da = (PdfAppearance) tp.getDuplicate();
	da.setFontAndSize(helv, fontSize);
	da.setColorFill(textColor);
	field.setDefaultAppearanceString(da);
	tp.beginVariableText();
	tp.saveState();
	tp.rectangle(2, 2, 167, 15);
	tp.clip();
	tp.newPath();
	tp.beginText();
	tp.setFontAndSize(helv, fontSize);
	tp.setColorFill(textColor);
	tp.setTextMatrix(4, 5);
	tp.showText(text);
	tp.endText();
	tp.restoreState();
	tp.endVariableText();
	field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
	writer.addAnnotation(field);

	// step 5: we close the document
	document.close();
}
 
Example #17
Source File: FormRadioButtonTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Generates an Acroform with a RadioButton
 */
@Test
public void main() throws Exception {

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

	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("radiobutton.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	PdfContentByte cb = writer.getDirectContent();
	cb.moveTo(0, 0);
	PdfFormField radio = PdfFormField.createRadioButton(writer, true);
	PdfAppearance tpOff = cb.createAppearance(20, 20);
	PdfAppearance tpOn = cb.createAppearance(20, 20);

	tpOff.circle(10, 10, 9);
	tpOff.stroke();

	tpOn.circle(10, 10, 9);
	tpOn.stroke();
	tpOn.circle(10, 10, 3);
	tpOn.fillStroke();

	radio.setFieldName("CreditCard");
	radio.setValueAsName("MasterCard");

	PdfFormField radio1 = PdfFormField.createEmpty(writer);
	radio1.setWidget(new Rectangle(100, 700, 120, 720), PdfAnnotation.HIGHLIGHT_INVERT);
	radio1.setAppearanceState("MasterCard");
	radio1.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpOff);
	radio1.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "MasterCard", tpOn);
	radio.addKid(radio1);

	PdfFormField radio2 = PdfFormField.createEmpty(writer);
	radio2.setWidget(new Rectangle(100, 660, 120, 680), PdfAnnotation.HIGHLIGHT_INVERT);
	radio2.setAppearanceState("Off");
	radio2.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpOff);
	radio2.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Visa", tpOn);
	radio.addKid(radio2);

	PdfFormField radio3 = PdfFormField.createEmpty(writer);
	radio3.setWidget(new Rectangle(100, 620, 120, 640), PdfAnnotation.HIGHLIGHT_INVERT);
	radio3.setAppearanceState("Off");
	radio3.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpOff);
	radio3.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "American", tpOn);
	radio.addKid(radio3);

	writer.addAnnotation(radio);

	// step 5: we close the document
	document.close();
}
 
Example #18
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 #19
Source File: ChartPdfHandler.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void exportElement(JRPdfExporterContext exporterContext,
		JRGenericPrintElement element)
{
	try
	{
		PdfWriter writer = exporterContext.getPdfWriter();
		PdfIndirectObject swfRef;
		boolean newContext = !existingContexts.containsKey(exporterContext);
		if (newContext)
		{
			// add the Adobe 1.7 extensions catalog dictionary
			PdfDictionary extensions = new PdfDictionary();
			PdfDictionary adobeExtension = new PdfDictionary();
			adobeExtension.put(new PdfName("BaseVersion"), PdfWriter.PDF_VERSION_1_7);
			adobeExtension.put(new PdfName("ExtensionLevel"), new PdfNumber(3));
			extensions.put(new PdfName("ADBE"), adobeExtension);
			writer.getExtraCatalog().put(new PdfName("Extensions"), extensions);
			
			// add the swf file
			byte[] swfData = readSwf();
			PdfFileSpecification swfFile = PdfFileSpecification.fileEmbedded(writer, 
					null, "Open Flash Chart", swfData);
			swfRef = writer.addToBody(swfFile);
			existingContexts.put(exporterContext, swfRef);
		}
		else
		{
			swfRef = existingContexts.get(exporterContext);
		}
		
		Rectangle rect = new Rectangle(element.getX() + exporterContext.getOffsetX(), 
				exporterContext.getExportedReport().getPageHeight() - element.getY() - exporterContext.getOffsetY(), 
				element.getX() + exporterContext.getOffsetX() + element.getWidth(),
				exporterContext.getExportedReport().getPageHeight() - element.getY() - exporterContext.getOffsetY() - element.getHeight());
		PdfAnnotation ann = new PdfAnnotation(writer, rect);
		ann.put(PdfName.SUBTYPE, new PdfName("RichMedia"));
		
		PdfDictionary settings = new PdfDictionary();
		PdfDictionary activation = new PdfDictionary();
		activation.put(new PdfName("Condition"), new PdfName("PV"));
		settings.put(new PdfName("Activation"), activation);
		ann.put(new PdfName("RichMediaSettings"), settings);
		
		PdfDictionary content = new PdfDictionary();
		
		HashMap<String, PdfIndirectReference> assets = new HashMap<String, PdfIndirectReference>();
		assets.put("map.swf", swfRef.getIndirectReference());
		PdfDictionary assetsDictionary = PdfNameTree.writeTree(assets, writer);
		content.put(new PdfName("Assets"), assetsDictionary);
		
		PdfArray configurations = new PdfArray();
		PdfDictionary configuration = new PdfDictionary();
		
		PdfArray instances = new PdfArray();
		PdfDictionary instance = new PdfDictionary();
		instance.put(new PdfName("Subtype"), new PdfName("Flash"));
		PdfDictionary params = new PdfDictionary();
		
		String chartData = (String) element.getParameterValue(PARAMETER_CHART_DATA);
		String vars = "inline_data=" + chartData;
		params.put(new PdfName("FlashVars"), new PdfString(vars));
		instance.put(new PdfName("Params"), params);
		instance.put(new PdfName("Asset"), swfRef.getIndirectReference());
		PdfIndirectObject instanceRef = writer.addToBody(instance);
		instances.add(instanceRef.getIndirectReference());
		configuration.put(new PdfName("Instances"), instances);
		
		PdfIndirectObject configurationRef = writer.addToBody(configuration);
		configurations.add(configurationRef.getIndirectReference());
		content.put(new PdfName("Configurations"), configurations);
		
		ann.put(new PdfName("RichMediaContent"), content);
		
		writer.addAnnotation(ann);
	}
	catch (Exception e)
	{
		throw new RuntimeException(e);
	}
}
 
Example #20
Source File: PdfAnnotationsImp.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
public PdfArray rotateAnnotations(PdfWriter writer, Rectangle pageSize) {
	PdfArray array = new PdfArray();
	int rotation = pageSize.getRotation() % 360;
	int currentPage = writer.getCurrentPageNumber();
	for (int k = 0; k < annotations.size(); ++k) {
		PdfAnnotation dic = (PdfAnnotation) annotations.get(k);
		int page = dic.getPlaceInPage();
		if (page > currentPage) {
			delayedAnnotations.add(dic);
			continue;
		}
		if (dic.isForm()) {
			if (!dic.isUsed()) {
				HashMap templates = dic.getTemplates();
				if (templates != null) {
					acroForm.addFieldTemplates(templates);
				}
			}
			PdfFormField field = (PdfFormField) dic;
			if (field.getParent() == null) {
				acroForm.addDocumentField(field.getIndirectReference());
			}
		}
		if (dic.isAnnotation()) {
			array.add(dic.getIndirectReference());
			if (!dic.isUsed()) {
				PdfRectangle rect = (PdfRectangle) dic.get(PdfName.RECT);
				if (rect != null) {
					switch (rotation) {
						case 90:
							dic.put(PdfName.RECT, new PdfRectangle(pageSize.getTop() - rect.bottom(), rect.left(), pageSize.getTop() - rect.top(), rect.right()));
							break;
						case 180:
							dic.put(PdfName.RECT, new PdfRectangle(pageSize.getRight() - rect.left(), pageSize.getTop() - rect.bottom(), pageSize.getRight() - rect.right(), pageSize.getTop() - rect.top()));
							break;
						case 270:
							dic.put(PdfName.RECT, new PdfRectangle(rect.bottom(), pageSize.getRight() - rect.left(), rect.top(), pageSize.getRight() - rect.right()));
							break;
					}
				}
			}
		}
		if (!dic.isUsed()) {
			dic.setUsed();
			try {
				writer.addToBody(dic, dic.getIndirectReference());
			} catch (IOException e) {
				throw new ExceptionConverter(e);
			}
		}
	}
	return array;
}
 
Example #21
Source File: PdfAnnotationsImp.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
public void addPlainAnnotation(PdfAnnotation annot) {
	annotations.add(annot);
}
 
Example #22
Source File: Chunk.java    From MesquiteCore with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
* Sets a generic annotation to this <CODE>Chunk</CODE>.
* @param annotation the annotation
* @return this <CODE>Chunk</CODE>
*/
   public Chunk setAnnotation(PdfAnnotation annotation) {
       return setAttribute(PDFANNOTATION, annotation);
   }
 
Example #23
Source File: Chunk.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Sets a generic annotation to this <CODE>Chunk</CODE>.
 *
 * @param annotation the annotation
 * @return this <CODE>Chunk</CODE>
 */
public Chunk setAnnotation(PdfAnnotation annotation) {
	return setAttribute(PDFANNOTATION, annotation);
}
 
Example #24
Source File: PdfAnnotations.java    From itext2 with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Use this methods to add a <CODE>PdfAnnotation</CODE> or a <CODE>PdfFormField</CODE>
 * to the document. Only the top parent of a <CODE>PdfFormField</CODE>
 * needs to be added.
 * @param annot the <CODE>PdfAnnotation</CODE> or the <CODE>PdfFormField</CODE> to add
 */
public void addAnnotation(PdfAnnotation annot);
 
Example #25
Source File: Chunk.java    From itext2 with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Sets a generic annotation to this <CODE>Chunk</CODE>.
 * 
 * @param annotation
 *            the annotation
 * @return this <CODE>Chunk</CODE>
 */
public Chunk setAnnotation(PdfAnnotation annotation) {
	return setAttribute(PDFANNOTATION, annotation);
}
 
Example #26
Source File: PdfAnnotations.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Use this methods to add a <CODE>PdfAnnotation</CODE> or a <CODE>PdfFormField</CODE>
 * to the document. Only the top parent of a <CODE>PdfFormField</CODE>
 * needs to be added.
 * @param annot the <CODE>PdfAnnotation</CODE> or the <CODE>PdfFormField</CODE> to add
 */
public void addAnnotation(PdfAnnotation annot);