LargeDocument2.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.
  16. // This sample is identical to StartEndDoc, but does not use the StartDoc/EndDoc method.
  17. public class LargeDocument2
  18. {
  19. public int CreatePDF(Stream stream)
  20. {
  21. // Number of pages to generate:
  22. const int N = Common.Util.LargeDocumentIterations;
  23. var start = Common.Util.TimeNow();
  24. var doc = new GcPdfDocument();
  25. // Prep a TextLayout to hold/format the text:
  26. var tl = new TextLayout(72)
  27. {
  28. MaxWidth = doc.PageSize.Width,
  29. MaxHeight = doc.PageSize.Height,
  30. MarginAll = 72,
  31. FirstLineIndent = 36,
  32. };
  33. tl.DefaultFormat.Font = StandardFonts.Times;
  34. tl.DefaultFormat.FontSize = 12;
  35. // Generate the document:
  36. for (int pageIdx = 0; pageIdx < N; ++pageIdx)
  37. {
  38. tl.Append(Common.Util.LoremIpsum(1));
  39. tl.PerformLayout(true);
  40. doc.NewPage().Graphics.DrawTextLayout(tl, PointF.Empty);
  41. tl.Clear();
  42. }
  43. // Insert a title page (cannot be done if using StartDoc/EndDoc):
  44. tl.FirstLineIndent = 0;
  45. var fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "yumin.ttf"));
  46. var tf0 = new TextFormat() { Font = fnt, FontSize = 24, FontBold = true };
  47. tl.Append(string.Format("Large Document\n{0} Pages of Lorem Ipsum\n\n", N), tf0);
  48. var tf1 = new TextFormat(tf0) { FontSize = 14, FontItalic = true };
  49. 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);
  50. tl.TextAlignment = TextAlignment.Center;
  51. tl.PerformLayout(true);
  52. doc.Pages.Insert(0).Graphics.DrawTextLayout(tl, PointF.Empty);
  53. // Done:
  54. doc.Save(stream);
  55. return doc.Pages.Count;
  56. }
  57. }
  58. }
  59.