DocProperties.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 GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Drawing;
  12. using GCDRAW = GrapeCity.Documents.Drawing;
  13.  
  14. namespace DsPdfWeb.Demos
  15. {
  16. // Shows how to set various document properties such as PDF version, document info and metadata.
  17. public class DocProperties
  18. {
  19. public int CreatePDF(Stream stream)
  20. {
  21. // Create a new PDF document:
  22. var doc = new GcPdfDocument();
  23. // While normally the PDF Version written into the document is determined automatically
  24. // (as the minimal version needed for the features actually used in this document),
  25. // it can be explicitly set:
  26. doc.PdfVersion = "1.7";
  27. // By default, GcPdfDocument uses CompressionLevel.Fastest to reduce the file size.
  28. // Setting the level to CompressionLevel.NoCompression will result in uncompressed PDF:
  29. doc.CompressionLevel = CompressionLevel.NoCompression;
  30. // By default, font subsets containing just the glyphs used in the document,
  31. // are embedded. This can be changed to embed whole fonts (which may result in
  32. // huge file size) or to not embed fonts as we do here (embedding can also
  33. // be controlled for individual fonts using the GcPdfDocument.Fonts collection):
  34. doc.FontEmbedMode = FontEmbedMode.NotEmbed;
  35. // Document properties such as title, author etc. can be set on GcPdfDocument.DocumentInfo,
  36. // but it should be noted that similar properties can be set in the GcPdfDocument.Metadata
  37. // and in most PDF readers properties set in metadata (see below) take precedence.
  38. // Here we set some of the properties available in DocumentInfo:
  39. doc.DocumentInfo.Title = "DsPdf Document Info Sample";
  40. doc.DocumentInfo.Author = "Jaime Smith";
  41. doc.DocumentInfo.Subject = "GcPdfDocument.DocumentInfo";
  42. doc.DocumentInfo.Producer = "DsPdfWeb Producer";
  43. doc.DocumentInfo.Creator = "DsPdfWeb Creator";
  44. // For sample sake, set CreationDate to a date 10 years in the future:
  45. doc.DocumentInfo.CreationDate = Common.Util.TimeNow().AddYears(10);
  46. // Document metadata is available via the GcPdfDocument.Metadata property.
  47. // It provides a number of predefined accessors, such as:
  48. doc.Metadata.Contributors.Add("contributor 1");
  49. doc.Metadata.Contributors.Add("contributor 2");
  50. doc.Metadata.Contributors.Add("contributor 3");
  51. doc.Metadata.Copyright = "MESCIUS inc.";
  52. doc.Metadata.Creators.Add("Creator 1");
  53. doc.Metadata.Creators.Add("Creator 2");
  54. doc.Metadata.Creators.Add("Creator 3");
  55. doc.Metadata.Description = "Sample document description";
  56. doc.Metadata.Keywords.Add("Keyword1");
  57. doc.Metadata.Keywords.Add("MESCIUS");
  58. doc.Metadata.Keywords.Add("Keyword3");
  59. doc.Metadata.Source = "Sourced by DsPdfWeb";
  60. // Arbitrary metadata can also be added using the AddProperty method:
  61. doc.Metadata.AddProperty("http://purl.org/dc/elements/1.1/", "language", "English");
  62. // Finally, add a page and some text to the document and save it:
  63. var rc = Common.Util.AddNote("DsPdf Document Properties sample.", doc.NewPage());
  64. //
  65. var pg = doc.Pages.Last;
  66. var g = pg.Graphics;
  67. var tl = g.CreateTextLayout();
  68. tl.AppendLine("See the source code of this demo (C# or VB.NET) for details on how to access the document properties and metadata.");
  69. tl.AppendLine();
  70. tl.AppendLine("When viewing the PDF, click the 'Document properties' button located in the top right part of the DsPdfViewer toolbar " +
  71. "to see the properties associated with this PDF document.");
  72. tl.MaxWidth = pg.Bounds.Width - rc.X * 2;
  73. tl.MarginLeft = rc.X;
  74. tl.MarginTop = rc.Bottom + 36;
  75. g.DrawTextLayout(tl, PointF.Empty);
  76. // Done:
  77. doc.Save(stream);
  78. return doc.Pages.Count;
  79. }
  80. }
  81. }
  82.