//// 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; namespaceDsPdfWeb.Demos.Basics{// This sample shows how to render a long text spanning multiple pages.publicclassPaginatedText {publicintCreatePDF(Stream stream) {var doc = newGcPdfDocument();// Use TextLayout to render text:var tl = newTextLayout(72);// If not specifying formats for individual runs, we MUST provide// font and font size on TextLayout.DefaultFormat: tl.DefaultFormat.Font = StandardFonts.Times; tl.DefaultFormat.FontSize = 12;// First line offset 1/2": tl.FirstLineIndent = 72 / 2;//// All other formatting properties are left at their default values.// In particular, TextLayout's default resolution is 72 dpi - // the same as DsPdf's, and WordWrap is true.//// Set TextLayout's area to the whole page: tl.MaxWidth = doc.PageSize.Width; tl.MaxHeight = doc.PageSize.Height;// ...and have it manage the page margins (1" all around): tl.MarginAll = tl.Resolution;//// Append the text (20 paragraphs so they would not fit on a single page)// (note that TextLayout interprets "\r\n" as paragraph delimiter): tl.Append(Common.Util.LoremIpsum(20));//// When all text has been added, we must calculate the glyphs needed to render the text,// and perform the layout. This can be done by a single call to PerformLayout, passing true to// recalculate glyphs first (even though the text won't all fit in the specified max size,// we only need to call PerformLayout once): tl.PerformLayout(true);// Use split options to provide widow/orphan control:var to = newTextSplitOptions(tl); to.MinLinesInFirstParagraph = 2; to.MinLinesInLastParagraph = 2;// In a loop, split and render the text:while (true) {// 'rest' will accept the text that did not fit:var splitResult = tl.Split(to, outTextLayout rest); doc.Pages.Add().Graphics.DrawTextLayout(tl, PointF.Empty);if (splitResult != SplitResult.Split)break; tl = rest; }// Done: doc.Save(stream);return doc.Pages.Count; } }}