BatesTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 BatesTest
  9. {
  10. internal class BatesTest
  11. {
  12. private static string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\Bates";
  13. private static Dictionary<int, string> IntToLocationDic = new Dictionary<int, string>()
  14. {
  15. {0, "Top Left" },
  16. {1, "Top Middle" },
  17. {2, "Top Right" },
  18. {3, "Bottom Left" },
  19. {4, "Bottom Middle" },
  20. {5, "Bottom Right" }
  21. };
  22. static void Main(string[] args)
  23. {
  24. Console.WriteLine("Running bates test sample…\r\n");
  25. SDKLicenseHelper.LicenseVerify();
  26. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  27. if (!Directory.Exists(outputPath))
  28. {
  29. Directory.CreateDirectory(outputPath);
  30. }
  31. #region Add bates
  32. if (AddBates(document))
  33. {
  34. Console.WriteLine("Add bates done.");
  35. }
  36. else
  37. {
  38. Console.WriteLine("Add bates failed.");
  39. }
  40. #endregion
  41. Console.WriteLine("--------------------");
  42. #region Edit bates
  43. if (EditBates(document))
  44. {
  45. Console.WriteLine("Edit bates done.");
  46. }
  47. else
  48. {
  49. Console.WriteLine("Edit bates failed.");
  50. }
  51. #endregion
  52. Console.WriteLine("--------------------");
  53. #region Clear bates
  54. if (ClearBates(document))
  55. {
  56. Console.WriteLine("Clear bates done.");
  57. }
  58. else
  59. {
  60. Console.WriteLine("Clear bates failed.");
  61. }
  62. #endregion
  63. Console.WriteLine("--------------------");
  64. Console.WriteLine("Done!");
  65. Console.WriteLine("--------------------");
  66. Console.ReadLine();
  67. }
  68. private static bool AddBates(CPDFDocument document)
  69. {
  70. string addBatesPath = outputPath + "\\AddBatesTest.pdf";
  71. CPDFBates bates = document.GetBates();
  72. byte[] color = { 255, 0, 0 };
  73. bates.SetPages("0-" + (document.PageCount - 1));
  74. for (int i = 0; i <= 5; i++)
  75. {
  76. bates.SetText(i, @"<<#3#5#Prefix-#-Suffix>>");
  77. bates.SetTextColor(i, color);
  78. bates.SetFontSize(i, 14);
  79. Console.WriteLine("Text: {0}", bates.GetText(i));
  80. Console.WriteLine("Location: {0}\n", IntToLocationDic[i]);
  81. }
  82. bates.Update();
  83. if (document.WriteToFilePath(addBatesPath))
  84. {
  85. Console.WriteLine("Browse the changed file in " + addBatesPath);
  86. return true;
  87. }
  88. else
  89. {
  90. return false;
  91. }
  92. }
  93. private static bool EditBates(CPDFDocument document)
  94. {
  95. CPDFBates bates = document.GetBates();
  96. if(bates.GetText(0) != string.Empty)
  97. {
  98. Console.WriteLine("Get old bates 0 done, text is {0}", bates.GetText(0));
  99. }
  100. else
  101. {
  102. Console.WriteLine("Get bates 0 failed, or it does not exist");
  103. return false;
  104. }
  105. bates.SetText(0, @"<<#3#1#ComPDFKit-#-ComPDFKit>>");
  106. bates.Update();
  107. Console.WriteLine("Change bates 0 done, new text is {0}", bates.GetText(0));
  108. string editBatesPath = outputPath + "\\EditBatesTest.pdf";
  109. if (document.WriteToFilePath(editBatesPath))
  110. {
  111. Console.WriteLine("Browse the changed file in " + editBatesPath);
  112. return true;
  113. }
  114. else
  115. {
  116. return false;
  117. }
  118. }
  119. private static bool ClearBates(CPDFDocument document)
  120. {
  121. CPDFBates bates = document.GetBates();
  122. bates.Clear();
  123. string clearBatesPath = outputPath + "\\ClearBatesTest.pdf";
  124. if (document.WriteToFilePath(clearBatesPath))
  125. {
  126. Console.WriteLine("Browse the changed file in " + clearBatesPath);
  127. return true;
  128. }
  129. else
  130. {
  131. return false;
  132. }
  133. }
  134. }
  135. }