/** * 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(); }
@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(); } }
/** * 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); } } }
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; } }
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)); } }
/** * Adds some annotated images to a PDF file. * * @param args * no arguments needed */ public static void main(String[] args) { System.out.println("images and annotations"); // step 1: creation of a document-object Document document = new Document(PageSize.A4, 50, 50, 50, 50); try { // step 2: // we create a writer that listens to the document PdfWriter.getInstance(document, new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "droidtext" + java.io.File.separator + "annotated_images.pdf")); // step 3: we open the document document.open(); // step 4: we add some content //Can't use filename => use byte[] instead // Image jpeg = Image.getInstance("otsoe.jpg"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); Bitmap bitmap = BitmapFactory.decodeResource(PdfTestRunner.getActivity().getResources(), R.drawable.otsoe); bitmap.compress(Bitmap.CompressFormat.JPEG /* FileType */, 100 /* Ratio */, stream); Image jpeg = Image.getInstance(stream.toByteArray()); jpeg.setAnnotation(new Annotation("picture", "This is my dog", 0, 0, 0, 0)); jpeg.setAbsolutePosition(100f, 550f); document.add(jpeg); //Can't use filename => use byte[] instead // Image wmf = Image.getInstance("iText.wmf"); InputStream inputStream = PdfTestRunner.getActivity().getResources().openRawResource(R.raw.itext_wmf); byte[] buffer = new byte[inputStream.available()]; inputStream.read(buffer); Image wmf = Image.getInstance(buffer); wmf.setAnnotation(new Annotation(0, 0, 0, 0, "http://www.lowagie.com/iText")); wmf.setAbsolutePosition(100f, 200f); document.add(wmf); } catch (Exception de) { de.printStackTrace(); } // step 5: we close the document document.close(); }
/** * 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); }
/** * 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(); }
/** * 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(); }