StartEndDoc.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. using GCTEXT = GrapeCity.Documents.Text;
  11. using GCDRAW = GrapeCity.Documents.Drawing;
  12.  
  13. namespace DsPdfWeb.Demos.Basics
  14. {
  15. // Shows how to create a large document using less memory.
  16. //
  17. // DsPdf provides two approaches to creating a PDF file:
  18. // - The usually more convenient approach: you build the document completely first,
  19. // adding text, graphics and other elements. Then you call Save() on the document
  20. // passing the name of the file, or the stream to save to. This approach allows
  21. // to modify the already created content - e.g. you can insert pages anywhere
  22. // in the document, or modify the already added pages.
  23. // - The StartDoc/EndDoc method: with this approach, you provide the stream
  24. // to save to at the very beginning, before adding any content to the document,
  25. // by calling the StartDoc() method on the document. All content is then written
  26. // directly to that stream, and you cannot go back and update the already created pages.
  27. // To complete the document you call the EndDoc() method. If you try to perform an
  28. // action that is not allowed, an exception will be thrown. While this approach is
  29. // somewhat limiting (e.g. Linearized cannot be set to true in this mode), it uses
  30. // less memory and may be preferable especially when creating very large documents.
  31. //
  32. // This sample demonstrates the StartDoc/EndDoc approach.
  33. //
  34. // Essentially the same code, but without using StartDoc/EndDoc, is demonstrated by the
  35. // LargeDocument2 sample. See also LinearizedPdf.
  36. public class StartEndDoc
  37. {
  38. public int CreatePDF(Stream stream)
  39. {
  40. // Number of pages to generate:
  41. const int N = Common.Util.LargeDocumentIterations;
  42. var doc = new GcPdfDocument();
  43. // Start creating the document by this call:
  44. doc.StartDoc(stream);
  45. // Prep a TextLayout to hold/format the text:
  46. var tl = new TextLayout(72)
  47. {
  48. MaxWidth = doc.PageSize.Width,
  49. MaxHeight = doc.PageSize.Height,
  50. MarginAll = 72,
  51. };
  52. tl.DefaultFormat.Font = StandardFonts.Times;
  53. tl.DefaultFormat.FontSize = 12;
  54. // Start with a title page:
  55. tl.FirstLineIndent = 0;
  56. var fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "yumin.ttf"));
  57. var tf0 = new TextFormat() { FontSize = 24, FontBold = true, Font = fnt };
  58. tl.Append(string.Format("Large Document\n{0} Pages of Lorem Ipsum\n\n", N), tf0);
  59. var tf1 = new TextFormat(tf0) { FontSize = 14, FontItalic = true };
  60. tl.Append(string.Format("Generated on {0}.", Common.Util.TimeNow().ToString("R")), tf1);
  61. tl.TextAlignment = TextAlignment.Center;
  62. tl.PerformLayout(true);
  63. doc.Pages.Add().Graphics.DrawTextLayout(tl, PointF.Empty);
  64. tl.Clear();
  65. tl.FirstLineIndent = 36;
  66. tl.TextAlignment = TextAlignment.Leading;
  67. // Generate the document:
  68. for (int pageIdx = 0; pageIdx < N; ++pageIdx)
  69. {
  70. tl.Append(Common.Util.LoremIpsum(1));
  71. tl.PerformLayout(true);
  72. doc.NewPage().Graphics.DrawTextLayout(tl, PointF.Empty);
  73. tl.Clear();
  74. }
  75. // NOTE: Certain operations (e.g. the one below) will throw an error when using StartDoc/EndDoc:
  76. // doc.Pages.Insert(0);
  77. //
  78. // Done - call EndDoc instead of Save():
  79. doc.EndDoc();
  80. return doc.Pages.Count;
  81. }
  82. }
  83. }
  84.