Drawing extra content on an image

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

6th November 2015
admin-marketing

Switch code for this example

AddPointer.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/26752663/adding-maps-at-itext-java
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Image;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class AddPointer {
  21.        
  22.     public static final String IMG = "resources/images/map_cic.png";
  23.     public static final String DEST = "results/objects/map_with_pointer.pdf";
  24.    
  25.     public static void main(String[] args) throws IOException, DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new AddPointer().createPdf(DEST);
  29.     }
  30.     public void createPdf(String dest) throws IOException, DocumentException {
  31.         Image img = Image.getInstance(IMG);
  32.         Document document = new Document(img);
  33.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  34.         document.open();
  35.         img.setAbsolutePosition(0, 0);
  36.         document.add(img);
  37.         PdfContentByte canvas = writer.getDirectContent();
  38.         canvas.setColorStroke(BaseColor.RED);
  39.         canvas.setLineWidth(3);
  40.         canvas.moveTo(220, 330);
  41.         canvas.lineTo(240, 370);
  42.         canvas.arc(200, 350, 240, 390, 0, (float) 180);
  43.         canvas.lineTo(220, 330);
  44.         canvas.closePathStroke();
  45.         canvas.setColorFill(BaseColor.RED);
  46.         canvas.circle(220, 370, 10);
  47.         canvas.fill();
  48.         document.close();
  49.     }
  50. }
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