DocumentInfoTest.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  17. PrintDocumentInfo(document);
  18. Console.WriteLine("--------------------");
  19. Console.WriteLine("Done.");
  20. Console.WriteLine("--------------------");
  21. Console.ReadLine();
  22. }
  23. /// <summary>
  24. /// Returns the file size based on the specified file path, with the smallest unit being bytes (B).
  25. /// </summary>
  26. /// <param name="filePath">The path to the file.</param>
  27. /// <returns>
  28. /// The calculated file size, with units in bytes (B), kilobytes (KB), megabytes (MB), or gigabytes (GB).
  29. /// </returns>
  30. public static string GetFileSize(string filePath)
  31. {
  32. FileInfo fileInfo = null;
  33. try
  34. {
  35. fileInfo = new FileInfo(filePath);
  36. }
  37. catch
  38. {
  39. return "0B";
  40. }
  41. if (fileInfo != null && fileInfo.Exists)
  42. {
  43. double fileSize = fileInfo.Length;
  44. if (fileSize > 1024)
  45. {
  46. fileSize = Math.Round(fileSize / 1024, 2);
  47. if (fileSize > 1024)
  48. {
  49. fileSize = Math.Round(fileSize / 1024, 2);
  50. if (fileSize > 1024)
  51. {
  52. fileSize = Math.Round(fileSize / 1024, 2);
  53. return fileSize + " GB";
  54. }
  55. else
  56. {
  57. return fileSize + " MB";
  58. }
  59. }
  60. else
  61. {
  62. return fileSize + " KB";
  63. }
  64. }
  65. else
  66. {
  67. return fileSize + " B";
  68. }
  69. }
  70. return "0B";
  71. }
  72. static private void PrintDocumentInfo(CPDFDocument document)
  73. {
  74. Console.WriteLine("File Name: {0}", document.FileName);
  75. Console.WriteLine("File Size: {0}", GetFileSize(document.FilePath));
  76. Console.WriteLine("Title: {0}", document.GetInfo().Title);
  77. Console.WriteLine("Author: {0}", document.GetInfo().Author);
  78. Console.WriteLine("Subject: {0}", document.GetInfo().Subject);
  79. Console.WriteLine("Keywords: {0}", document.GetInfo().Keywords);
  80. Console.WriteLine("Version: {0}", document.GetInfo().Version);
  81. Console.WriteLine("Page Count: {0}", document.PageCount);
  82. Console.WriteLine("Creator: {0}", document.GetInfo().Creator);
  83. Console.WriteLine("Creation Data: {0}",document.GetInfo().CreationDate);
  84. Console.WriteLine("Allows Printing: {0}", document.GetPermissionsInfo().AllowsPrinting);
  85. Console.WriteLine("Allows Copying: {0}", document.GetPermissionsInfo().AllowsCopying);
  86. Console.WriteLine("Allows Document Changes: {0}", document.GetPermissionsInfo().AllowsDocumentChanges);
  87. Console.WriteLine("Allows Document Assembly: {0}", document.GetPermissionsInfo().AllowsDocumentAssembly);
  88. Console.WriteLine("Allows Commenting: {0}", document.GetPermissionsInfo().AllowsCommenting);
  89. Console.WriteLine("Allows FormField Entry: {0}", document.GetPermissionsInfo().AllowsFormFieldEntry);
  90. }
  91. }
  92. }