GetImageProperties.vb
''
'' This code is part of Document Solutions for PDF .NET demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Graphics.Images
Imports GrapeCity.Documents.Pdf.Spec
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing

Public Class GetImageProperties
    Public Function CreatePDF(ByVal stream As Stream) As Integer
        Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf"))
            Dim docSrc = New GcPdfDocument()
            docSrc.Load(fs)

            ' Get the images in the loaded PDF and print their info into the resulting PDF:
            Dim imageInfos = docSrc.GetImages()

            ' The PDF to hold the results:
            Dim doc = New GcPdfDocument()
            Dim page = doc.NewPage()
            ' Set up a TextLayout to format the results:
            Dim tl = page.Graphics.CreateTextLayout()
            tl.DefaultFormat.Font = StandardFonts.Courier
            tl.DefaultFormat.FontSize = 14
            tl.MaxWidth = doc.PageSize.Width
            tl.MaxHeight = doc.PageSize.Height
            tl.MarginAll = tl.Resolution
            Dim captionFmt = New TextFormat(tl.DefaultFormat) With {.Font = StandardFonts.CourierBold, .FontSize = tl.DefaultFormat.FontSize + 2}

            Dim i As Integer = 0
            For Each imageInfo In imageInfos
                i += 1
                tl.AppendLine($"Image {i}", captionFmt)

                ' PdfImageBase class represents an image in the loaded PDF file:
                Dim img As PdfImageBase = imageInfo.Image

                tl.AppendLine($"PdfImage object ID: {img.ObjID}")
                ' PdfImageBase is derived from PdfDictWrapper type, it has methods
                ' that allow you to access properties/data of the underlying PDF stream object:
                Using psi As PdfStreamInfo = img.GetPdfStreamInfo()
                    tl.AppendLine($"    Image stream length: {psi.Stream.Length}")
                    tl.AppendLine($"        ImageFilterName: {psi.ImageFilterName}")
                    tl.AppendLine($"ImageFilterDecodeParams: {psi.ImageFilterDecodeParams}")
                    ' Dump the content of the ImageFilterDecodeParams if it exists:
                    If psi.ImageFilterDecodeParams IsNot Nothing Then
                        For Each kvp In psi.ImageFilterDecodeParams.Dict
                            tl.AppendLine($"{kvp.Key}: {kvp.Value}")
                        Next
                        ' An example of how to get the value of BlackIs1:
                        Dim blackIs1 = psi.ImageFilterDecodeParams.GetBool(PdfName.Std.BlackIs1, Nothing)
                        tl.AppendLine($"BlackIs1: {blackIs1}")
                    End If
                End Using
                ' Dump the properties of PdfImage dictionary:
                tl.AppendLine()
                tl.AppendLine("Properties of PdfImage dictionary:")
                For Each kvp As KeyValuePair(Of PdfName, IPdfObject) In img.PdfDict.Dict
                    tl.AppendLine($"{kvp.Key}: {kvp.Value}")
                Next
                '
                Dim cs = img.Get(Of IPdfObject)(PdfName.Std.ColorSpace)
                tl.AppendLine($"ColorSpace: {cs.GetType().Name} {cs}")
                Dim bpc = img.Get(Of IPdfObject)(PdfName.Std.BitsPerComponent)
                tl.AppendLine($"BitsPerComponent: {bpc?.GetType().Name} {bpc}")
                tl.AppendLine()
            Next

            ' Render the results:
            tl.PerformLayout(True)
            Do
                Dim rest As TextLayout = Nothing
                Dim splitResult = tl.Split(Nothing, rest)
                page.Graphics.DrawTextLayout(tl, PointF.Empty)
                If splitResult <> SplitResult.Split Then
                    Exit Do
                End If
                tl = rest
                tl.MarginTop = tl.Resolution
                page = doc.Pages.Add()
            Loop

            ' Append the source PDF to the result for reference:
            doc.MergeWithDocument(docSrc)

            '' Done:
            doc.Save(stream)
            Return doc.Pages.Count
        End Using
    End Function
End Class