Docotic.Pdf Library Help > Samples > Images > Extract painted images

This sample shows how to extract images painted on page in a PDF document using two different methods.

You can use PdfPage.GetPaintedImages() method to get all images drawn on the page. You can save these images "as is" (as they are stored in a PDF document) and "as painted" (as they are painted on a page).

To save an image "as is" you will need to access PdfImage object using PdfPaintedImage.Image property, then save the accessed image using PdfImage.Save method.

To save an image "as painted" you will need to use PdfPaintedImage.SaveAsPainted method. This method saves the image as painted on a page (i.e. taking into account whether image is rotated, flipped or scaled). Using SaveAsPainted method you can specify desired image format (PNG, JPEG or TIFF) for the saved image. Default output format is PNG.

The PdfPaintedImage class also provides properties TransformationMatrix and IsTransformed that can help you to determine whether the image is painted on a page scaled, flipped and / or rotated.

C#
using System.Diagnostics;

using BitMiracle.Docotic.Pdf;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class ExtractPaintedImages
    {
        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.

            using (PdfDocument pdf = new PdfDocument(@"Sample Data\ImageScaleAndRotate.pdf"))
            {
                PdfCollection<PdfPaintedImage> paintedImages = pdf.Pages[0].GetPaintedImages();

                PdfPaintedImage image = paintedImages[0];

                // save image as is
                string imageAsIs = "PdfImage.Save";
                string fullPath = image.Image.Save(imageAsIs);
                Process.Start(fullPath);

                // save image as painted
                // NOTE: PdfPaintedImage.SaveAsPainted() method is not supported in version for .NET Standard
                string imageAsPainted = "PdfPaintedImage.SaveAsPainted.tiff";
                image.SaveAsPainted(imageAsPainted, PdfExtractedImageFormat.Tiff);
                Process.Start(imageAsPainted);
            }
        }
    }
}
VB
Imports System.Diagnostics

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class ExtractPaintedImages
        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.

            Using pdf As New PdfDocument("Sample Data\ImageScaleAndRotate.pdf")
                Dim paintedImages As PdfCollection(Of PdfPaintedImage) = pdf.Pages(0).GetPaintedImages()

                Dim image As PdfPaintedImage = paintedImages(0)

                ' save image as is
                Dim imageAsIs As String = "PdfImage.Save"
                Dim fullPath As String = image.Image.Save(imageAsIs)
                Process.Start(fullPath)

                ' save image as painted
                ' NOTE: PdfPaintedImage.SaveAsPainted() method is not supported in version for .NET Standard
                Dim imageAsPainted As String = "PdfPaintedImage.SaveAsPainted.tiff"
                image.SaveAsPainted(imageAsPainted, PdfExtractedImageFormat.Tiff)
                Process.Start(imageAsPainted)
            End Using
        End Sub
    End Class
End Namespace