com.lowagie.text.Annotation Java Examples

The following examples show how to use com.lowagie.text.Annotation. 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: AnnotatedImageTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Adds some annotated images to a PDF file.
 */
@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("annotated_images.pdf"));
	// step 3: we open the document
	document.open();
	// step 4: we add some content
	Image jpeg = Image.getInstance(PdfTestBase.RESOURCES_DIR + "otsoe.jpg");
	jpeg.setAnnotation(new Annotation("picture", "This is my dog", 0, 0, 0, 0));
	jpeg.setAbsolutePosition(100f, 550f);
	document.add(jpeg);
	Image wmf = Image.getInstance(PdfTestBase.RESOURCES_DIR + "iText.wmf");
	wmf.setAnnotation(new Annotation(0, 0, 0, 0, "http://www.lowagie.com/iText"));
	wmf.setAbsolutePosition(100f, 200f);
	document.add(wmf);

	// step 5: we close the document
	document.close();
}
 
Example #2
Source File: SimplePdfTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testSimplePdf() throws FileNotFoundException, DocumentException {
	// create document
	Document document = PdfTestBase.createPdf("testSimplePdf.pdf");
	try {
		// new page with a rectangle
		document.open();
		document.newPage();
		Annotation ann = new Annotation("Title", "Text");
		Rectangle rect = new Rectangle(100, 100);
		document.add(ann);
		document.add(rect);
	} finally {
		// close document
		if (document != null)
			document.close();
	}

}
 
Example #3
Source File: PdfDocument.java    From MesquiteCore with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Adds an image to the Graphics object.
 * 
 * @param graphics the PdfContentByte holding the graphics layer of this PdfDocument
 * @param image the image
 * @param a an element of the transformation matrix
 * @param b an element of the transformation matrix
 * @param c an element of the transformation matrix
 * @param d an element of the transformation matrix
 * @param e an element of the transformation matrix
 * @param f an element of the transformation matrix
 * @throws DocumentException
 */

private void addImage(PdfContentByte graphics, Image image, float a, float b, float c, float d, float e, float f) throws DocumentException {
    Annotation annotation = image.annotation();
    if (image.hasAbsolutePosition()) {
        graphics.addImage(image);
        if (annotation != null) {
            annotation.setDimensions(image.absoluteX(), image.absoluteY(), image.absoluteX() + image.scaledWidth(), image.absoluteY() + image.scaledHeight());
            add(annotation);
        }
    }
    else {
        graphics.addImage(image, a, b, c, d, e, f);
        if (annotation != null) {
            annotation.setDimensions(e, f, e + image.scaledWidth(), f + image.scaledHeight());
            add(annotation);
        }
    }
}
 
Example #4
Source File: SAXiTextHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
protected void addImage(Image img) throws EmptyStackException {
	// if there is an element on the stack...
	Object current = stack.pop();
	// ...and it's a Chapter or a Section, the Image can be
	// added directly
	if (current instanceof Chapter || current instanceof Section || current instanceof Cell) {
		((TextElementArray) current).add(img);
		stack.push(current);
		return;
	}
	// ...if not, we need to to a lot of stuff
	else {
		Stack newStack = new Stack();
		while (!(current instanceof Chapter || current instanceof Section || current instanceof Cell)) {
			newStack.push(current);
			if (current instanceof Anchor) {
				img.setAnnotation(new Annotation(0, 0, 0, 0, ((Anchor) current).getReference()));
			}
			current = stack.pop();
		}
		((TextElementArray) current).add(img);
		stack.push(current);
		while (!newStack.empty()) {
			stack.push(newStack.pop());
		}
		return;
	}
}
 
Example #5
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 #6
Source File: SAXiTextHandler.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void addImage(Image img) throws EmptyStackException {
    // if there is an element on the stack...
    Object current = stack.pop();
    // ...and it's a Chapter or a Section, the Image can be
    // added directly
    if (current instanceof Chapter
            || current instanceof Section
            || current instanceof Cell) {
        ((TextElementArray) current).add(img);
        stack.push(current);
        return;
    }
    // ...if not, we need to to a lot of stuff
    else {
        Stack newStack = new Stack();
        while (!(current instanceof Chapter
                || current instanceof Section || current instanceof Cell)) {
            newStack.push(current);
            if (current instanceof Anchor) {
                img.setAnnotation(new Annotation(0, 0, 0,
                        0, ((Anchor) current).getReference()));
            }
            current = stack.pop();
        }
        ((TextElementArray) current).add(img);
        stack.push(current);
        while (!newStack.empty()) {
            stack.push(newStack.pop());
        }
        return;
    }
}
 
Example #7
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 #8
Source File: ElementFactory.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates an Annotation object based on a list of properties.
 * 
 * @param attributes
 * @return an Annotation
 */
public static Annotation getAnnotation(Properties attributes) {
	float llx = 0, lly = 0, urx = 0, ury = 0;
	String value;

	value = attributes.getProperty(ElementTags.LLX);
	if (value != null) {
		llx = Float.parseFloat(value + "f");
	}
	value = attributes.getProperty(ElementTags.LLY);
	if (value != null) {
		lly = Float.parseFloat(value + "f");
	}
	value = attributes.getProperty(ElementTags.URX);
	if (value != null) {
		urx = Float.parseFloat(value + "f");
	}
	value = attributes.getProperty(ElementTags.URY);
	if (value != null) {
		ury = Float.parseFloat(value + "f");
	}

	String title = attributes.getProperty(ElementTags.TITLE);
	String text = attributes.getProperty(ElementTags.CONTENT);
	if (title != null || text != null) {
		return new Annotation(title, text, llx, lly, urx, ury);
	}
	value = attributes.getProperty(ElementTags.URL);
	if (value != null) {
		return new Annotation(llx, lly, urx, ury, value);
	}
	value = attributes.getProperty(ElementTags.NAMED);
	if (value != null) {
		return new Annotation(llx, lly, urx, ury, Integer.parseInt(value));
	}
	String file = attributes.getProperty(ElementTags.FILE);
	String destination = attributes.getProperty(ElementTags.DESTINATION);
	String page = (String) attributes.remove(ElementTags.PAGE);
	if (file != null) {
		if (destination != null) {
			return new Annotation(llx, lly, urx, ury, file, destination);
		}
		if (page != null) {
			return new Annotation(llx, lly, urx, ury, file, Integer.parseInt(page));
		}
	}
	return new Annotation("", "", llx, lly, urx, ury);
}
 
Example #9
Source File: ElementFactory.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates an Annotation object based on a list of properties.
 * @param attributes
 * @return an Annotation
 */
public static Annotation getAnnotation(Properties attributes) {
	float llx = 0, lly = 0, urx = 0, ury = 0;
	String value;

	value = attributes.getProperty(ElementTags.LLX);
	if (value != null) {
		llx = Float.parseFloat(value + "f");
	}
	value = attributes.getProperty(ElementTags.LLY);
	if (value != null) {
		lly = Float.parseFloat(value + "f");
	}
	value = attributes.getProperty(ElementTags.URX);
	if (value != null) {
		urx = Float.parseFloat(value + "f");
	}
	value = attributes.getProperty(ElementTags.URY);
	if (value != null) {
		ury = Float.parseFloat(value + "f");
	}

	String title = attributes.getProperty(ElementTags.TITLE);
	String text = attributes.getProperty(ElementTags.CONTENT);
	if (title != null || text != null) {
		return new Annotation(title, text, llx, lly, urx, ury);
	}
	value = attributes.getProperty(ElementTags.URL);
	if (value != null) {
		return new Annotation(llx, lly, urx, ury, value);
	}
	value = attributes.getProperty(ElementTags.NAMED);
	if (value != null) {
		return new Annotation(llx, lly, urx, ury, Integer.parseInt(value));
	}
	String file = attributes.getProperty(ElementTags.FILE);
	String destination = attributes.getProperty(ElementTags.DESTINATION);
	String page = (String) attributes.remove(ElementTags.PAGE);
	if (file != null) {
		if (destination != null) {
			return new Annotation(llx, lly, urx, ury, file, destination);
		}
		if (page != null) {
			return new Annotation(llx, lly, urx, ury, file, Integer
					.parseInt(page));
		}
	}
	return new Annotation("", "", llx, lly, urx, ury);
}
 
Example #10
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 #11
Source File: PatchRtfDocument.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public RtfBasicElement[] mapElement( Element element ) throws DocumentException {
  ArrayList<RtfBasicElement> rtfElements = new ArrayList<RtfBasicElement>();
  if ( element instanceof RtfBasicElement ) {
    RtfBasicElement rtfElement = (RtfBasicElement) element;
    rtfElement.setRtfDocument( rtfDoc );
    return new RtfBasicElement[] { rtfElement };
  }
  switch ( element.type() ) {
    case Element.CHUNK:
      Chunk chunk = (Chunk) element;
      if ( chunk.hasAttributes() ) {
        if ( chunk.getAttributes().containsKey( Chunk.IMAGE ) ) {
          rtfElements.add( new RtfImage( rtfDoc, chunk.getImage() ) );
        } else if ( chunk.getAttributes().containsKey( Chunk.NEWPAGE ) ) {
          rtfElements.add( new RtfNewPage( rtfDoc ) );
        } else if ( chunk.getAttributes().containsKey( Chunk.TAB ) ) {
          Float tabPos = (Float) ( (Object[]) chunk.getAttributes().get( Chunk.TAB ) )[1];
          RtfTab tab = new RtfTab( tabPos.floatValue(), RtfTab.TAB_LEFT_ALIGN );
          tab.setRtfDocument( rtfDoc );
          rtfElements.add( tab );
          rtfElements.add( new RtfChunk( rtfDoc, new Chunk( "\t" ) ) );
        } else {
          rtfElements.add( new RtfChunk( rtfDoc, (Chunk) element ) );
        }
      } else {
        rtfElements.add( new RtfChunk( rtfDoc, (Chunk) element ) );
      }
      break;
    case Element.PHRASE:
      rtfElements.add( new RtfPhrase( rtfDoc, (Phrase) element ) );
      break;
    case Element.PARAGRAPH:
      rtfElements.add( new RtfParagraph( rtfDoc, (Paragraph) element ) );
      break;
    case Element.ANCHOR:
      rtfElements.add( new RtfAnchor( rtfDoc, (Anchor) element ) );
      break;
    case Element.ANNOTATION:
      rtfElements.add( new RtfAnnotation( rtfDoc, (Annotation) element ) );
      break;
    case Element.IMGRAW:
    case Element.IMGTEMPLATE:
    case Element.JPEG:
      rtfElements.add( new RtfImage( rtfDoc, (Image) element ) );
      break;
    case Element.AUTHOR:
    case Element.SUBJECT:
    case Element.KEYWORDS:
    case Element.TITLE:
    case Element.PRODUCER:
    case Element.CREATIONDATE:
      rtfElements.add( new RtfInfoElement( rtfDoc, (Meta) element ) );
      break;
    case Element.LIST:
      rtfElements.add( new RtfList( rtfDoc, (List) element ) ); // TODO: Testing
      break;
    case Element.LISTITEM:
      rtfElements.add( new RtfListItem( rtfDoc, (ListItem) element ) ); // TODO: Testing
      break;
    case Element.SECTION:
      rtfElements.add( new RtfSection( rtfDoc, (Section) element ) );
      break;
    case Element.CHAPTER:
      rtfElements.add( new RtfChapter( rtfDoc, (Chapter) element ) );
      break;
    case Element.TABLE:
      if ( element instanceof Table ) {
        rtfElements.add( new PatchRtfTable( rtfDoc, (Table) element ) );
      } else {
        rtfElements.add( new PatchRtfTable( rtfDoc, ( (SimpleTable) element ).createTable() ) );
      }
      break;
    case Element.PTABLE:
      if ( element instanceof PdfPTable ) {
        rtfElements.add( new PatchRtfTable( rtfDoc, (PdfPTable) element ) );
      } else {
        rtfElements.add( new PatchRtfTable( rtfDoc, ( (SimpleTable) element ).createTable() ) );
      }
      break;
  }

  return rtfElements.toArray( new RtfBasicElement[rtfElements.size()] );
}
 
Example #12
Source File: RtfAnnotation.java    From itext2 with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Constructs a RtfAnnotation based on an Annotation.
 * 
 * @param doc The RtfDocument this RtfAnnotation belongs to
 * @param annotation The Annotation this RtfAnnotation is based off
 */
public RtfAnnotation(RtfDocument doc, Annotation annotation) {
    super(doc);
    title = annotation.title();
    content = annotation.content();
}