LargeDocument3.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. // Generates a large PDF using a single TextLayout.
  16. // Unlike other 'large document' samples (StartEndDoc, LargeDocument2),
  17. // this sample actually paginates the whole document as a single sequence of paragraphs,
  18. // optimally and correctly filling each page with text. Split options such as
  19. // keeping at least 2 lines of paragraph on each page are also supported.
  20. // Due to the extra work, this sample takes significantly more time to complete.
  21. public class LargeDocument3
  22. {
  23. public int CreatePDF(Stream stream)
  24. {
  25. // Number of pages to generate:
  26. const int N = Common.Util.LargeDocumentIterations;
  27. var start = Common.Util.TimeNow();
  28. var doc = new GcPdfDocument();
  29. // Prep a TextLayout to hold/format the text:
  30. var tl = new TextLayout(72)
  31. {
  32. MaxWidth = doc.PageSize.Width,
  33. MaxHeight = doc.PageSize.Height,
  34. MarginAll = 72,
  35. FirstLineIndent = 36,
  36. };
  37. tl.DefaultFormat.Font = StandardFonts.Times;
  38. tl.DefaultFormat.FontSize = 12;
  39. // Generate the document:
  40. for (int paraIdx = 0; paraIdx < N; ++paraIdx)
  41. {
  42. tl.AppendLine(Common.Util.LoremIpsum(1));
  43. }
  44. // Split and render TextLayout as shown in the PaginatedText sample:
  45. var tso = new TextSplitOptions(tl)
  46. {
  47. MinLinesInFirstParagraph = 2,
  48. MinLinesInLastParagraph = 2,
  49. };
  50. tl.PerformLayout(true);
  51. // The loop splitting and rendering the layout:
  52. var tls = new TextLayoutSplitter(tl);
  53. for (var tlPage = tls.Split(tso); tlPage != null; tlPage = tls.Split(tso))
  54. doc.NewPage().Graphics.DrawTextLayout(tlPage, PointF.Empty);
  55.  
  56. tl.Clear();
  57. // Insert a title page (cannot be done if using StartDoc/EndDoc):
  58. tl.FirstLineIndent = 0;
  59. var fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "yumin.ttf"));
  60. var tf0 = new TextFormat() { FontSize = 24, FontBold = true, Font = fnt };
  61. tl.Append(string.Format("Large Document\n{0} Paragraphs of Lorem Ipsum\n\n", N), tf0);
  62. var tf1 = new TextFormat(tf0) { FontSize = 14, FontItalic = true };
  63. tl.Append(string.Format("Generated on {0} in {1:m\\m\\ s\\s\\ fff\\m\\s}.", Common.Util.TimeNow().ToString("R"), Common.Util.TimeNow() - start), tf1);
  64. tl.TextAlignment = TextAlignment.Center;
  65. tl.PerformLayout(true);
  66. doc.Pages.Insert(0).Graphics.DrawTextLayout(tl, PointF.Empty);
  67. // Done:
  68. doc.Save(stream);
  69. return doc.Pages.Count;
  70. }
  71. }
  72. }
  73.