ListFonts.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.Collections.Generic
Imports System.Linq
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text

'' This sample lists all fonts found in a loaded PDF,
'' prints some info for each font, And indicates whether a Font object
'' can be created from the font in the PDF.
Public Class ListFonts
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim sourcePDF = "CompleteJavaScriptBook.pdf"

        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()

        Dim rc = Util.AddNote(
            "This sample loads an arbitrary PDF into a temporary GcPdfDocument, " +
            "and lists all fonts found in that document, with some of their properties. " +
            "It also tries to create a Font object from each of those PDF fonts, " +
            "and reports whether this operation succeeded.",
            page)

        '' Text layout to render the text:
        Dim tab = 24
        Dim tl = page.Graphics.CreateTextLayout()
        tl.DefaultFormat.Font = StandardFonts.Times
        tl.DefaultFormat.FontSize = 12
        tl.MaxWidth = doc.PageSize.Width
        tl.MaxHeight = doc.PageSize.Height
        tl.MarginAll = rc.Left
        tl.MarginTop = rc.Bottom + 36
        tl.TabStops = New List(Of TabStop)() From {New TabStop(tab)}
        tl.FirstLineIndent = -tab
        tl.MarginRight = 144

        '' Text split options for widow/orphan control:
        Dim tso = New TextSplitOptions(tl) With
        {
            .KeepParagraphLinesTogether = True,
            .MinLinesInFirstParagraph = 2,
            .MinLinesInLastParagraph = 2,
            .RestMarginTop = rc.Left
        }

        '' Open an arbitrary PDF, load it into a temp document and get all fonts:
        Using fs = New FileStream(Path.Combine("Resources", "PDFs", sourcePDF), FileMode.Open, FileAccess.Read)
            Dim doc1 = New GcPdfDocument()
            doc1.Load(fs)
            Dim fonts = doc1.GetFonts()
            tl.AppendLine($"Total of {fonts.Count} fonts found in {sourcePDF}:")
            tl.AppendLine()
            Dim i As Integer = 0
            For Each fnt In fonts
                Dim nativeFont = fnt.CreateNativeFont()
                tl.Append($"{i}:{vbTab}BaseFont: {fnt.BaseFont} IsEmbedded: {fnt.IsEmbedded}.")
                tl.AppendParagraphBreak()
                If nativeFont IsNot Nothing Then
                    tl.AppendLine($"{vbTab}CreateNativeFont succeeded, family: {nativeFont.FontFamilyName} bold: {nativeFont.FontBold} italic: {nativeFont.FontItalic}.")
                Else
                    tl.AppendLine($"{vbTab}CreateNativeFont failed")
                End If
                tl.AppendLine()
                i += 1
            Next
            tl.PerformLayout(True)
            While (True)
                '' 'rest' will accept the text that did not fit:
                Dim rest As TextLayout = Nothing
                Dim splitResult = tl.Split(tso, rest)
                doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty)
                If splitResult <> SplitResult.Split Then
                    Exit While
                End If
                tl = rest
                doc.NewPage()
            End While
        End Using
        '' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class