//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem.IO;usingSystem.Drawing;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Text;usingGCTEXT = GrapeCity.Documents.Text;usingGCDRAW = GrapeCity.Documents.Drawing; namespaceDsPdfWeb.Demos.Basics{// 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 using StartDoc/EndDoc, is demonstrated by the // LargeDocument2 sample. See also LinearizedPdf.publicclassStartEndDoc {publicintCreatePDF(Stream stream) {// Number of pages to generate:constintN = Common.Util.LargeDocumentIterations;var doc = newGcPdfDocument();// Start creating the document by this call: doc.StartDoc(stream);// Prep a TextLayout to hold/format the text:var tl = newTextLayout(72) {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;var fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSerif-Regular.ttf"));var tf0 = newTextFormat() { FontSize = 24, FontBold = true, Font = fnt }; tl.Append(string.Format("Large Document\n{0} Pages of Lorem Ipsum\n\n", N), tf0);var tf1 = newTextFormat(tf0) { FontSize = 14, FontItalic = true }; tl.Append(string.Format("Generated on {0}.", Common.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 (int pageIdx = 0; pageIdx < N; ++pageIdx) { tl.Append(Common.Util.LoremIpsum(1)); tl.PerformLayout(true); doc.NewPage().Graphics.DrawTextLayout(tl, PointF.Empty); tl.Clear(); }// 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; } }}