BatesTest.cs 5.4 KB

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