LinearizedPdf.cs
//
// This code is part of Document Solutions for PDF demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;

namespace DsPdfWeb.Demos.Basics
{
    // Shows how to create a linearized PDF file.
    // Note that while the code below was used to generate the PDF shown in the sample browser,
    // the browser sends a static copy of this file, so that the web server can send it
    // in smaller chunks (all other sample PDFs are generated on the fly).
    public class LinearizedPdf
    {
        public int CreatePDF(Stream stream)
        {
            // Number of pages to generate:
            const int N = 5000;
            var doc = new GcPdfDocument();
            // Prep a TextLayout to hold/format the text:
            var page = doc.NewPage();
            var tl = page.Graphics.CreateTextLayout();
            tl.DefaultFormat.Font = StandardFonts.Times;
            tl.DefaultFormat.FontSize = 12;
            // Use TextLayout to layout the whole page including margins:
            tl.MaxHeight = page.Size.Height;
            tl.MaxWidth = page.Size.Width;
            tl.MarginAll = 72;
            tl.FirstLineIndent = 72 / 2;
            // Generate the document:
            for (int pageIdx = 0; pageIdx < N; ++pageIdx)
            {
                // Note: for the sake of this sample, we do not care if a sample text does not fit on a page.
                tl.Append(Common.Util.LoremIpsum(2));
                tl.PerformLayout(true);
                doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty);
                if (pageIdx < N - 1)
                {
                    doc.Pages.Add();
                    tl.Clear();
                }
            }
            // To create a linearized PDF we need to specify SaveMode.Linearized when saving the PDF:
            doc.Save(stream, SaveMode.Linearized);
            return doc.Pages.Count;
        }
    }
}