DocumentInfoTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using ComPDFKit.PDFDocument;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace DocumentInfoTest
  9. {
  10. internal class DocumentInfoTest
  11. {
  12. static void Main(string[] args)
  13. {
  14. Console.WriteLine("Running DocumentInfo test sample…\r\n");
  15. SDKLicenseHelper.LicenseVerify();
  16. #region Sample 1: Print information
  17. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  18. PrintDocumentInfo(document);
  19. Console.WriteLine("--------------------");
  20. #endregion
  21. Console.WriteLine("Done.");
  22. Console.WriteLine("--------------------");
  23. Console.ReadLine();
  24. }
  25. /// <summary>
  26. /// Returns the file size based on the specified file path, with the smallest unit being bytes (B).
  27. /// </summary>
  28. /// <param name="filePath">The path to the file.</param>
  29. /// <returns>
  30. /// The calculated file size, with units in bytes (B), kilobytes (KB), megabytes (MB), or gigabytes (GB).
  31. /// </returns>
  32. public static string GetFileSize(string filePath)
  33. {
  34. FileInfo fileInfo = null;
  35. try
  36. {
  37. fileInfo = new FileInfo(filePath);
  38. }
  39. catch
  40. {
  41. return "0B";
  42. }
  43. if (fileInfo != null && fileInfo.Exists)
  44. {
  45. double fileSize = fileInfo.Length;
  46. if (fileSize > 1024)
  47. {
  48. fileSize = Math.Round(fileSize / 1024, 2);
  49. if (fileSize > 1024)
  50. {
  51. fileSize = Math.Round(fileSize / 1024, 2);
  52. if (fileSize > 1024)
  53. {
  54. fileSize = Math.Round(fileSize / 1024, 2);
  55. return fileSize + " GB";
  56. }
  57. else
  58. {
  59. return fileSize + " MB";
  60. }
  61. }
  62. else
  63. {
  64. return fileSize + " KB";
  65. }
  66. }
  67. else
  68. {
  69. return fileSize + " B";
  70. }
  71. }
  72. return "0B";
  73. }
  74. /// <summary>
  75. /// Print all of the infomations
  76. /// </summary>
  77. static private void PrintDocumentInfo(CPDFDocument document)
  78. {
  79. Console.WriteLine("File Name: {0}", document.FileName);
  80. Console.WriteLine("File Size: {0}", GetFileSize(document.FilePath));
  81. Console.WriteLine("Title: {0}", document.GetInfo().Title);
  82. Console.WriteLine("Author: {0}", document.GetInfo().Author);
  83. Console.WriteLine("Subject: {0}", document.GetInfo().Subject);
  84. Console.WriteLine("Keywords: {0}", document.GetInfo().Keywords);
  85. Console.WriteLine("Version: {0}", document.GetInfo().Version);
  86. Console.WriteLine("Page Count: {0}", document.PageCount);
  87. Console.WriteLine("Creator: {0}", document.GetInfo().Creator);
  88. Console.WriteLine("Creation Data: {0}",document.GetInfo().CreationDate);
  89. Console.WriteLine("Allows Printing: {0}", document.GetPermissionsInfo().AllowsPrinting);
  90. Console.WriteLine("Allows Copying: {0}", document.GetPermissionsInfo().AllowsCopying);
  91. Console.WriteLine("Allows Document Changes: {0}", document.GetPermissionsInfo().AllowsDocumentChanges);
  92. Console.WriteLine("Allows Document Assembly: {0}", document.GetPermissionsInfo().AllowsDocumentAssembly);
  93. Console.WriteLine("Allows Commenting: {0}", document.GetPermissionsInfo().AllowsCommenting);
  94. Console.WriteLine("Allows FormField Entry: {0}", document.GetPermissionsInfo().AllowsFormFieldEntry);
  95. }
  96. }
  97. }