NoPassGetMetadata.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 try to read its metadata.
  15. // Keep in mind that in some cases metadata in a PDF may also be encrypted, in which case
  16. // reading 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 NoPassGetMetadata
  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. // The PDF to hold the results:
  29. var doc = new GcPdfDocument();
  30. var page = doc.NewPage();
  31. // Set up a TextLayout to format the results:
  32. var tl = page.Graphics.CreateTextLayout();
  33. tl.DefaultFormat.Font = StandardFonts.Courier;
  34. tl.DefaultFormat.FontSize = 14;
  35. tl.MaxWidth = doc.PageSize.Width;
  36. tl.MaxHeight = doc.PageSize.Height;
  37. tl.MarginAll = tl.Resolution;
  38. var captionFmt = new TextFormat(tl.DefaultFormat) { Font = StandardFonts.CourierBold, FontSize = tl.DefaultFormat.FontSize + 2 };
  39.  
  40. // All security handlers before StandardSecurityHandlerRev4 always encrypted metadata,
  41. // the StandardSecurityHandlerRev4 has EncryptMetadata property which we need to check:
  42. bool metadataIsEncrypted = true;
  43. if (docSrc.Security.EncryptHandler is StandardSecurityHandlerRev4 ssh)
  44. metadataIsEncrypted = ssh.EncryptMetadata;
  45.  
  46. if (!metadataIsEncrypted)
  47. {
  48. Metadata m = docSrc.Metadata;
  49. tl.AppendLine("The PDF has unencrypted metadata:", captionFmt);
  50. tl.AppendLine($" Copyright: {m.Copyright}");
  51. tl.AppendLine($" CreatorTool: {m.CreatorTool}");
  52. if (m.CreateDate.HasValue)
  53. tl.AppendLine($" CreateDate: {m.CreateDate.Value.ToUniversalTime():R}");
  54. tl.AppendLine($" Description: {m.Description}");
  55. if (m.ModifyDate.HasValue)
  56. tl.AppendLine($" ModifyDate: {m.ModifyDate.Value.ToUniversalTime():R}");
  57. tl.AppendLine($" Producer: {m.Producer}");
  58. tl.AppendLine($" Title: {m.Title}");
  59. }
  60. else
  61. {
  62. tl.AppendLine("Cannot read metadata because the PDF metadata is encrypted.", captionFmt);
  63. }
  64.  
  65. // Render the results:
  66. tl.PerformLayout(true);
  67. while (true)
  68. {
  69. var splitResult = tl.Split(null, out TextLayout rest);
  70. page.Graphics.DrawTextLayout(tl, PointF.Empty);
  71. if (splitResult != SplitResult.Split)
  72. break;
  73. tl = rest;
  74. tl.MarginTop = tl.Resolution;
  75. page = doc.Pages.Add();
  76. }
  77.  
  78. // Done:
  79. doc.Save(stream);
  80. return doc.Pages.Count;
  81. }
  82. }
  83. }
  84.