StartEndDoc.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 GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' Shows how to create a large document using less memory.
''
'' DsPdf provides two approaches to creating a PDF file:
'' - The usually more convenient approach: you build the document completely first,
''   adding text, graphics and other elements. Then you call Save() on the document
''   passing the name of the file, or the stream to save to. This approach allows
''   to modify the already created content - e.g. you can insert pages anywhere
''   in the document, or modify the already added pages.
'' - The StartDoc/EndDoc method: with this approach, you provide the stream
''   to save to at the very beginning, before adding any content to the document,
''   by calling the StartDoc() method on the document. All content is then written
''   directly to that stream, and you cannot go back and update the already created pages.
''   To complete the document you call the EndDoc() method. If you try to perform an
''   action that is not allowed, an exception will be thrown. While this approach is
''   somewhat limiting (e.g. Linearized cannot be set to true in this mode), it uses
''   less memory and may be preferable especially when creating very large documents.
''
'' This sample demonstrates the StartDoc/EndDoc approach.
''
'' Essentially the same code, but without use of StartDoc/EndDoc, is demonstrated by the
'' LargeDocument2 sample. See also LinearizedPdf.
Public Class StartEndDoc
    Function CreatePDF(ByVal stream As Stream) As Integer
        '' Number of pages to generate:
        Dim N = Util.LargeDocumentIterations
        Dim doc = New GcPdfDocument()
        '' Start creating the document by this call:
        doc.StartDoc(stream)
        '' Prep a TextLayout to hold/format the text:
        Dim tl = New TextLayout(72) With {
            .MaxWidth = doc.PageSize.Width,
            .MaxHeight = doc.PageSize.Height,
            .MarginAll = 72
        }
        tl.DefaultFormat.Font = StandardFonts.Times
        tl.DefaultFormat.FontSize = 12
        '' Start with a title page:
        tl.FirstLineIndent = 0
        Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "yumin.ttf"))
        Dim tf0 = New TextFormat() With {.FontSize = 24, .FontBold = True, .Font = fnt}
        tl.Append(String.Format("Large Document" + vbLf + "{0} Pages of Lorem Ipsum" + vbLf + vbLf, N), tf0)
        Dim tf1 = New TextFormat(tf0) With {.FontSize = 14, .FontItalic = True}
        tl.Append(String.Format("Generated on {0}.", Util.TimeNow().ToString("R")), tf1)
        tl.TextAlignment = TextAlignment.Center
        tl.PerformLayout(True)
        doc.Pages.Add().Graphics.DrawTextLayout(tl, PointF.Empty)
        tl.Clear()
        tl.FirstLineIndent = 36
        tl.TextAlignment = TextAlignment.Leading
        '' Generate the document:
        For pageIdx = 1 To N
            tl.Append(Util.LoremIpsum(1))
            tl.PerformLayout(True)
            doc.NewPage().Graphics.DrawTextLayout(tl, PointF.Empty)
            tl.Clear()
        Next
        '' NOTE: Certain operations (e.g. the one below) will throw an error when using StartDoc/EndDoc:
        ''   doc.Pages.Insert(0);
        ''
        '' Done - call EndDoc instead of Save():
        doc.EndDoc()
        Return doc.Pages.Count
    End Function
End Class