'' This code is part of Document Solutions for PDF samples.
'' 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.",

        '' 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()
            Dim fonts = doc1.GetFonts()
            tl.AppendLine($"Total of {fonts.Count} fonts found in {sourcePDF}:")
            Dim i As Integer = 0
            For Each fnt In fonts
                Dim nativeFont = fnt.CreateNativeFont()
                tl.Append($"{i}:{vbTab}BaseFont: {fnt.BaseFont} IsEmbedded: {fnt.IsEmbedded}.")
                If nativeFont IsNot Nothing Then
                    tl.AppendLine($"{vbTab}CreateNativeFont succeeded, family: {nativeFont.FontFamilyName} bold: {nativeFont.FontBold} italic: {nativeFont.FontItalic}.")
                    tl.AppendLine($"{vbTab}CreateNativeFont failed")
                End If
                i += 1
            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
            End While
        End Using
        '' Done:
        Return doc.Pages.Count
    End Function
End Class