NoPassSetMetadata.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.Pdf.Security;
  10. using GrapeCity.Documents.Text;
  11.  
  12. namespace DsPdfWeb.Demos
  13. {
  14. // This example shows how to load a password protected PDF and change its metadata.
  15. // Keep in mind that in some cases metadata in a PDF may also be encrypted, in which case
  16. // accessing it without the password is impossible.
  17. // For reference, the loaded PDF is protected with the owner password 'owner' and user password 'user'.
  18. public class NoPassSetMetadata
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands-password-user.pdf"));
  23. // Set up DecryptionOptions to allow loading password protected PDFs without password:
  24. var dopt = new DecryptionOptions() { ThrowExceptionIfInvalidPassword = false };
  25. var docSrc = new GcPdfDocument();
  26. docSrc.Load(fs, dopt);
  27.  
  28. // All security handlers before StandardSecurityHandlerRev4 always encrypted metadata,
  29. // the StandardSecurityHandlerRev4 has EncryptMetadata property which we need to check:
  30. bool metadataIsEncrypted = true;
  31. if (docSrc.Security.EncryptHandler is StandardSecurityHandlerRev4 ssh)
  32. metadataIsEncrypted = ssh.EncryptMetadata;
  33. // In the PDf used by this demo the metadata is not encrypted:
  34. if (metadataIsEncrypted)
  35. throw new Exception("Set PDF Metadata demo: Unexpected error.");
  36.  
  37. Metadata m = docSrc.Metadata;
  38. // Save original metadata so we can compare the results:
  39. var origTitle = m.Title;
  40. var origCreatorTool = m.CreatorTool;
  41. var origModifyDate = m.ModifyDate;
  42. // Change some metadata:
  43. m.Title = "Set PDF Metadata Demo";
  44. m.CreatorTool = "DsPdf Demo Browser";
  45. m.ModifyDate = Common.Util.TimeNow();
  46.  
  47. // We save the modified password protected document to a temp file,
  48. // load it again (also without password), read the (modified) metadata
  49. // and display it in the resulting PDF:
  50. var fn = Path.GetTempFileName();
  51. {
  52. docSrc.Save(fn);
  53.  
  54. using var fsTemp = File.OpenRead(fn);
  55. var docTemp = new GcPdfDocument();
  56. docTemp.Load(fsTemp, dopt);
  57.  
  58. // The PDF to hold the results:
  59. var doc = new GcPdfDocument();
  60. var page = doc.NewPage();
  61. // Set up a TextLayout to format the results:
  62. var tl = page.Graphics.CreateTextLayout();
  63. tl.DefaultFormat.Font = StandardFonts.Courier;
  64. tl.DefaultFormat.FontSize = 14;
  65. tl.MaxWidth = doc.PageSize.Width;
  66. tl.MaxHeight = doc.PageSize.Height;
  67. tl.MarginAll = tl.Resolution;
  68. var captionFmt = new TextFormat(tl.DefaultFormat) { Font = StandardFonts.CourierBold, FontSize = tl.DefaultFormat.FontSize + 2 };
  69.  
  70. if (docTemp.Security.EncryptHandler is StandardSecurityHandlerRev4 sshTemp)
  71. metadataIsEncrypted = sshTemp.EncryptMetadata;
  72. if (metadataIsEncrypted)
  73. throw new Exception("Set PDF Metadata demo: Unexpected error.");
  74.  
  75. // Render the results:
  76. Metadata mTemp = docTemp.Metadata;
  77. tl.AppendLine("Modified metadata in a password protected PDF:", captionFmt);
  78. tl.AppendLine($"\nTitle", captionFmt);
  79. tl.AppendLine($"Was '{origTitle}', now '{mTemp.Title}'");
  80. tl.AppendLine($"\nCreatorTool", captionFmt);
  81. tl.AppendLine($"Was '{origCreatorTool}', now '{mTemp.CreatorTool}'");
  82. tl.AppendLine($"\nModifyDate", captionFmt);
  83. var origModifyDateStr = origModifyDate.HasValue ? origModifyDate.Value.ToString("R") : string.Empty;
  84. var newModifyDateStr = mTemp.ModifyDate.HasValue ? mTemp.ModifyDate.Value.ToString("R") : string.Empty;
  85. tl.AppendLine($"Was '{origModifyDateStr}', now '{newModifyDateStr}'");
  86. tl.PerformLayout(true);
  87. page.Graphics.DrawTextLayout(tl, PointF.Empty);
  88. doc.Save(stream);
  89. }
  90. File.Delete(fn);
  91.  
  92. // Done:
  93. return 1;
  94. }
  95. }
  96. }
  97.