LinearizedPdf.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10.  
  11. namespace DsPdfWeb.Demos.Basics
  12. {
  13. // Shows how to create a linearized PDF file.
  14. // Note that while the code below was used to generate the PDF shown in the sample browser,
  15. // the browser sends a static copy of this file, so that the web server can send it
  16. // in smaller chunks (all other sample PDFs are generated on the fly).
  17. public class LinearizedPdf
  18. {
  19. public int CreatePDF(Stream stream)
  20. {
  21. // Number of pages to generate:
  22. const int N = 5000;
  23. var doc = new GcPdfDocument();
  24. // Prep a TextLayout to hold/format the text:
  25. var page = doc.NewPage();
  26. var tl = page.Graphics.CreateTextLayout();
  27. tl.DefaultFormat.Font = StandardFonts.Times;
  28. tl.DefaultFormat.FontSize = 12;
  29. // Use TextLayout to layout the whole page including margins:
  30. tl.MaxHeight = page.Size.Height;
  31. tl.MaxWidth = page.Size.Width;
  32. tl.MarginAll = 72;
  33. tl.FirstLineIndent = 72 / 2;
  34. // Generate the document:
  35. for (int pageIdx = 0; pageIdx < N; ++pageIdx)
  36. {
  37. // Note: for the sake of this sample, we do not care if a sample text does not fit on a page.
  38. tl.Append(Common.Util.LoremIpsum(2));
  39. tl.PerformLayout(true);
  40. doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty);
  41. if (pageIdx < N - 1)
  42. {
  43. doc.Pages.Add();
  44. tl.Clear();
  45. }
  46. }
  47. // To create a linearized PDF we need to specify SaveMode.Linearized when saving the PDF:
  48. doc.Save(stream, SaveMode.Linearized);
  49. return doc.Pages.Count;
  50. }
  51. }
  52. }
  53.