NoPassGetPageInfo.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 GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text

'' This example shows how to load a password protected PDF and get its page count and page sizes.
'' For reference, the loaded PDF is protected with the owner password 'owner' and user password 'user'.
Public Class NoPassGetPageInfo
    Public Function CreatePDF(ByVal stream As Stream) As Integer
        Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands-password-user.pdf"))
            '' Set up DecryptionOptions to allow loading password protected PDFs without password:
            Dim dopt = New DecryptionOptions() With {.ThrowExceptionIfInvalidPassword = False}
            Dim docSrc = New GcPdfDocument()
            docSrc.Load(fs, dopt)

            '' 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}

            '' Get and print some of the source PDF stats:
            tl.AppendLine($"The source PDF contains {docSrc.Pages.Count} page(s).", captionFmt)
            For Each pg In docSrc.Pages
                tl.AppendLine($"  Page at index {pg.Index}:", captionFmt)
                Dim sz = pg.GetRenderSize()
                tl.AppendLine($"    Page size is {sz.Width}x{sz.Height}.")
                tl.AppendLine($"    Page has {If(pg.Landscape, "landscape", "portrait")} orientation.")
                tl.AppendLine($"    Page paper kind is '{pg.PaperKind}'.")
            Next

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

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