Skip to main content
Skip table of contents

Drawing extra content on an image

This example was written in answer to the question Click How to add a map with a pointer to a PDF?


addpointer

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.objects;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class AddPointer {
    public static final String DEST = "./target/sandbox/objects/add_pointer.pdf";
    public static final String IMG = "./src/main/resources/img/map_cic.png";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new AddPointer().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Image img = new Image(ImageDataFactory.create(IMG));
        Document doc = new Document(pdfDoc, new PageSize(img.getImageWidth(), img.getImageHeight()));

        img.setFixedPosition(0, 0);
        doc.add(img);

        // Added a custom shape on top of a image
        PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
        canvas.setStrokeColor(ColorConstants.RED)
                .setLineWidth(3)
                .moveTo(220, 330)
                .lineTo(240, 370)
                .arc(200, 350, 240, 390, 0, 180)
                .lineTo(220, 330)
                .closePathStroke()
                .setFillColor(ColorConstants.RED)
                .circle(220, 370, 10)
                .fill();

        doc.close();
    }
}

C#

C#
using System.IO;
using iText.IO.Image;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Samples.Sandbox.Objects
{
    public class AddPointer
    {
        public static readonly string DEST = "results/sandbox/objects/add_pointer.pdf";
        public static readonly string IMG = "../../../resources/img/map_cic.png";

        public static void Main(string[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new AddPointer().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Image img = new Image(ImageDataFactory.Create(IMG));
            Document doc = new Document(pdfDoc, new PageSize(img.GetImageWidth(), img.GetImageHeight()));

            img.SetFixedPosition(0, 0);
            doc.Add(img);

            // Added a custom shape on top of a image
            PdfCanvas canvas = new PdfCanvas(pdfDoc.GetFirstPage());
            canvas.SetStrokeColor(ColorConstants.RED)
                .SetLineWidth(3)
                .MoveTo(220, 330)
                .LineTo(240, 370)
                .Arc(200, 350, 240, 390, 0, 180)
                .LineTo(220, 330)
                .ClosePathStroke()
                .SetFillColor(ColorConstants.RED)
                .Circle(220, 370, 10)
                .Fill();

            doc.Close();
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.