'''' This code is part of Document Solutions for PDF .NET demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.IOImportsSystem.DrawingImportsGrapeCity.Documents.PdfImportsGrapeCity.Documents.TextImports GCTEXT = GrapeCity.Documents.Text '' 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.PublicClassStartEndDocFunctionCreatePDF(ByVal stream AsStream) AsInteger'' Number of pages to generate:Dim N = Util.LargeDocumentIterationsDim doc = NewGcPdfDocument()'' Start creating the document by this call: doc.StartDoc(stream)'' Prep a TextLayout to hold/format the text:Dim tl = NewTextLayout(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 = 0Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSerif-Regular.ttf"))Dim tf0 = NewTextFormat() 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 = NewTextFormat(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 = 1To 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.CountEndFunctionEndClass