ObjectStreams.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.IO.Compression;
  8. using System.Drawing;
  9. using System.Collections.Generic;
  10. using GrapeCity.Documents.Pdf;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Common;
  13. using GCTEXT = GrapeCity.Documents.Text;
  14. using GCDRAW = GrapeCity.Documents.Drawing;
  15.  
  16. namespace DsPdfWeb.Demos
  17. {
  18. // This sample shows how to use the SavePdfOptions.UseObjectStreams property
  19. // to reduce the size of a PDF.
  20. //
  21. // Note that as with most optimizations, the size reduction will vary depending
  22. // on the actual content of the source PDF.
  23. public class ObjectStreams
  24. {
  25. public int CreatePDF(Stream stream)
  26. {
  27. // Create a 5 page non-optimal PDF:
  28. var tmpInput = MakeInputFile();
  29. var fiInput = new FileInfo(tmpInput);
  30.  
  31. // Create a new PDF, load the source PDF into it, and optimize it:
  32. var tmpOutput = Path.GetTempFileName();
  33. var tmpDoc = new GcPdfDocument();
  34. using (var fs = File.OpenRead(tmpInput))
  35. {
  36. tmpDoc.Load(fs);
  37. // By default GcPdfDocument uses CompressionLevel.Fastest when saving a PDF.
  38. // If PDF size is important, we should use optimal compression:
  39. tmpDoc.CompressionLevel = CompressionLevel.Optimal;
  40. // Minimize stream sizes, use object streams:
  41. tmpDoc.Save(tmpOutput, new SavePdfOptions(SaveMode.Default, PdfStreamHandling.MinimizeSize, UseObjectStreams.Multiple));
  42. }
  43. var fiOutput = new FileInfo(tmpOutput);
  44.  
  45. // Record the input and output file sizes in the resultant PDF:
  46. var doc = new GcPdfDocument();
  47. Common.Util.AddNote(String.Format(
  48. "Using the UseObjectStreams.Multiple option when saving a PDF will in most cases reduce the resulting file size, " +
  49. "sometimes significantly. In this case the size of the PDF generated by the 'Large Document' sample decreased " +
  50. "from {0:N0} to {1:N0} bytes, without any loss in fidelity or PDF opening speed.\n" +
  51. "Using the UseObjectStreams.Single option yields an even slightly smaller PDF size at the cost of slower opening in PDF viewers.\n" +
  52. "To reproduce these results locally, download and run this sample, specifying a valid license key " +
  53. "(otherwise loading is limited to 5 pages, and the size reduction may be too small). " +
  54. "You may also modify the sample code to keep the temporary " +
  55. "input and output files, and compare their sizes using a file manager.", fiInput.Length, fiOutput.Length),
  56. doc.NewPage());
  57. doc.Save(stream);
  58.  
  59. // Clean up:
  60. File.Delete(tmpInput);
  61. File.Delete(tmpOutput);
  62.  
  63. return doc.Pages.Count;
  64. }
  65.  
  66. static string MakeInputFile()
  67. {
  68. // Number of pages to generate:
  69. const int N = Common.Util.LargeDocumentIterations;
  70. var start = Common.Util.TimeNow();
  71. var doc = new GcPdfDocument();
  72. // Prep a TextLayout to hold/format the text:
  73. var tl = new TextLayout(72)
  74. {
  75. MaxWidth = doc.PageSize.Width,
  76. MaxHeight = doc.PageSize.Height,
  77. MarginAll = 72,
  78. FirstLineIndent = 36,
  79. };
  80. tl.DefaultFormat.Font = StandardFonts.Times;
  81. tl.DefaultFormat.FontSize = 12;
  82. // Generate the document:
  83. for (int pageIdx = 0; pageIdx < N; ++pageIdx)
  84. {
  85. tl.Append(Common.Util.LoremIpsum(1));
  86. tl.PerformLayout(true);
  87. doc.NewPage().Graphics.DrawTextLayout(tl, PointF.Empty);
  88. tl.Clear();
  89. }
  90. // Insert a title page (cannot be done if using StartDoc/EndDoc):
  91. tl.FirstLineIndent = 0;
  92. var fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "yumin.ttf"));
  93. var tf0 = new TextFormat() { Font = fnt, FontSize = 24, FontBold = true };
  94. tl.Append(string.Format("Large Document\n{0} Pages of Lorem Ipsum\n\n", N), tf0);
  95. var tf1 = new TextFormat(tf0) { FontSize = 14, FontItalic = true };
  96. 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);
  97. tl.TextAlignment = TextAlignment.Center;
  98. tl.PerformLayout(true);
  99. doc.Pages.Insert(0).Graphics.DrawTextLayout(tl, PointF.Empty);
  100.  
  101. // Save the resultant PDF in a temp file with UseObjectStreams.None (it is the default):
  102. var outFn = Path.GetTempFileName();
  103. doc.Save(outFn, new SavePdfOptions(SaveMode.Default, PdfStreamHandling.Copy, UseObjectStreams.None));
  104. return outFn;
  105. }
  106. }
  107. }
  108.