Docotic.Pdf Library Help > Samples > Images > Extract image coordinates

This sample shows how to extract images with their location and size from PdfDocument.

Use PdfPage.GetPaintedImages() method to get all images drawn on the page.

PdfPaintedImage class provides properties for accessing a PdfImage object for the painted image and bounds of this image on the page.

C#
using System.Diagnostics;

using BitMiracle.Docotic.Pdf;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class ExtractImageCoordinates
    {
        public static void Main()
        {
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "ExtractImageCoordinates.pdf";

            using (PdfDocument pdf = new PdfDocument(@"Sample Data\gmail-cheat-sheet.pdf"))
            {
                foreach (PdfPage page in pdf.Pages)
                {
                    foreach (PdfPaintedImage image in page.GetPaintedImages())
                    {
                        PdfCanvas canvas = page.Canvas;
                        canvas.Pen.Width = 3;
                        canvas.Pen.Color = new PdfRgbColor(255, 0, 0);

                        canvas.DrawRectangle(image.Bounds);
                    }
                }
                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
    }
}
VB
Imports System.Diagnostics

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class ExtractImageCoordinates
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.

            Dim pathToFile As String = "ExtractImageCoordinates.pdf"

            Using pdf As New PdfDocument("Sample Data\gmail-cheat-sheet.pdf")

                For Each page As PdfPage In pdf.Pages

                    For Each image As PdfPaintedImage In page.GetPaintedImages()
                        Dim canvas As PdfCanvas = page.Canvas
                        canvas.Pen.Width = 3
                        canvas.Pen.Color = New PdfRgbColor(255, 0, 0)

                        canvas.DrawRectangle(image.Bounds)
                    Next

                Next

                pdf.Save(pathToFile)
            End Using

            Process.Start(pathToFile)
        End Sub
    End Class
End Namespace