Drawing extra content on an image

This is a code example of iText PDF, discover more.

31st May 2016
iText PDF

Switch code for this example

AddPointer.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/26752663/adding-maps-at-itext-java
  11.  */
  12. package com.itextpdf.samples.sandbox.objects;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  15. import com.itextpdf.kernel.color.Color;
  16. import com.itextpdf.kernel.geom.PageSize;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Image;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28. import java.io.IOException;
  29.  
  30. @Category(SampleTest.class)
  31. public class AddPointer extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/objects/add_pointer.pdf";
  33.     public static final String IMG = "./src/test/resources/img/map_cic.png";
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new AddPointer().manipulatePdf(DEST);
  39.     }
  40.  
  41.     public void manipulatePdf(String dest) throws IOException {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  43.         Image img = new Image(ImageDataFactory.create(IMG));
  44.         Document doc = new Document(pdfDoc, new PageSize(img.getImageWidth(), img.getImageHeight()));
  45.  
  46.         img.setFixedPosition(0, 0);
  47.         doc.add(img);
  48.  
  49.         PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
  50.         canvas.setStrokeColor(Color.RED)
  51.                 .setLineWidth(3)
  52.                 .moveTo(220, 330)
  53.                 .lineTo(240, 370)
  54.                 .arc(200, 350, 240, 390, 0, (float) 180)
  55.                 .lineTo(220, 330)
  56.                 .closePathStroke()
  57.                 .setFillColor(Color.RED)
  58.                 .circle(220, 370, 10)
  59.                 .fill();
  60.  
  61.         doc.close();
  62.     }
  63. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now