Determine if a PDF only contains images

The following sample code determines whether a PDF document only contains images (and not other shapes such as text fragments).

static void Main(string[] args)
{
    string path = @"..\..\input.pdf";
    System.Console.WriteLine("File " + ((OnlyImages(path) ? " contains " : " does not contain ")) + "only images");
}

public static bool OnlyImages(string path)
{
    using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
    {
        Document document = new Document(file);
        foreach (Page page in document.Pages)
        {
            ShapeCollection shapes = page.CreateShapes();
            if (!OnlyImages(shapes)) return false;
        }
    }
    return true;
}

public static bool OnlyImages(ShapeCollection shapes)
{
    foreach (Shape shape in shapes)
    {
        if (shape is ImageShape) continue;
        if (shape is ShapeCollection) // recurse
        {
            if (OnlyImages(shape as ShapeCollection)) continue;
        }
        return false;
    }
    return true;
}
Sub Main()
    Dim path As String = "..\..\input.pdf"
    System.Console.WriteLine("File " + ((If(OnlyImages(path), " contains ", " does not contain "))) + "only images")
End Sub

Public Function OnlyImages(path As String) As Boolean
    Using file As New FileStream(path, FileMode.Open, FileAccess.Read)
        Dim document As New Document(file)
        For Each page As Page In document.Pages
            Dim shapes As ShapeCollection = page.CreateShapes()
            If Not OnlyImages(shapes) Then
                Return False
            End If
        Next
    End Using
    Return True
End Function

Public Function OnlyImages(shapes As ShapeCollection) As Boolean
    For Each shape As Shape In shapes
        If TypeOf shape Is ImageShape Then
            Continue For
        End If
        If TypeOf shape Is ShapeCollection Then
            ' recurse
            If OnlyImages(TryCast(shape, ShapeCollection)) Then
                Continue For
            End If
        End If
        Return False
    Next
    Return True
End Function
Download PDFKit.NET 5.0
We will send you a download link
Why do we ask your email address?
We send tips that speed up your evaluation
We let you know about bug fixes
You can always unsubscribe with one click
We never share your address with a 3rd party
Thank you for your download

We have sent an email with a download link. Alternatively, you may want to use the NuGet package manager to install our library.

Nuget ID

Use the NugetID and start right away, or download the package and install it handmatically