GetImages.vb
''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Text
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing

'' This sample loads a PDF (we use the document generated by the Wetlands sample)
'' and extracts all images from it. It then prints those images into a new PDF,
'' centered one per page.
Public Class GetImages
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim tf = New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 12}
        Using fs = New FileStream(Path.Combine("Resources", "PDFs", "Wetlands.pdf"), FileMode.Open, FileAccess.Read)
            Dim docSrc = New GcPdfDocument()
            '' Load an existing PDF with some images:
            docSrc.Load(fs)
            '' This call extracts information about images from the loaded PDF,
            '' note that for large files it may take a while to complete:
            Dim imageInfos = docSrc.GetImages()

            Dim doc = New GcPdfDocument()
            Dim textPt = New PointF(72, 72)
            Dim imageRc = New RectangleF(72, 72 * 2, doc.PageSize.Width - 72 * 2, doc.PageSize.Height - 72 * 3)

            For Each imageInfo In imageInfos
                '' The same image may appear on multiple locations,
                '' imageInfo includes page indices and locations on pages
                '' for simplicity sake we only print page numbers here:
                Dim sb = New StringBuilder()
                imageInfo.Locations.ForEach(Function(il_) sb.Append((il_.Page.Index + 1).ToString() + ", "))
                Dim g = doc.NewPage().Graphics
                g.DrawString($"This image appears on page(s) {sb.ToString().TrimEnd(" "c, ","c)} of the original PDF:", tf, New PointF(72, 72))
                g.DrawImage(imageInfo.Image, imageRc, Nothing, ImageAlign.ScaleImage)
            Next
            '' Done:
            doc.Save(stream)
            Return doc.Pages.Count
        End Using
    End Function
End Class