BatesTest.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. static private string parentPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())));
  10. static private string outputPath = Path.Combine(parentPath, "Output", "CS");
  11. private static Dictionary<int, string> IntToLocationDic = new Dictionary<int, string>()
  12. {
  13. {0, "Top Left" },
  14. {1, "Top Middle" },
  15. {2, "Top Right" },
  16. {3, "Bottom Left" },
  17. {4, "Bottom Middle" },
  18. {5, "Bottom Right" }
  19. };
  20. static void Main(string[] args)
  21. {
  22. #region Preparation work
  23. Console.WriteLine("Running bates test sample…\r\n");
  24. SDKLicenseHelper.LicenseVerify();
  25. if (!Directory.Exists(outputPath))
  26. {
  27. Directory.CreateDirectory(outputPath);
  28. }
  29. #endregion
  30. #region Sample 1: Add bates
  31. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  32. if (AddBates(document))
  33. {
  34. Console.WriteLine("Add bates done.");
  35. }
  36. document.Release();
  37. Console.WriteLine("--------------------");
  38. #endregion
  39. #region Samles 2: Edit bates
  40. document = CPDFDocument.InitWithFilePath("Bates.pdf");
  41. if (EditBates(document))
  42. {
  43. Console.WriteLine("Edit bates done.");
  44. }
  45. document.Release();
  46. Console.WriteLine("--------------------");
  47. #endregion
  48. #region Sample 3: Clear bates
  49. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  50. if (ClearBates(document))
  51. {
  52. Console.WriteLine("Clear bates done.");
  53. }
  54. document.Release();
  55. Console.WriteLine("--------------------");
  56. #endregion
  57. Console.WriteLine("Done!");
  58. Console.WriteLine("--------------------");
  59. Console.ReadLine();
  60. }
  61. /// <summary>
  62. /// Add a new bates
  63. /// </summary>
  64. /// <param name="document">Regular document</param>
  65. private static bool AddBates(CPDFDocument document)
  66. {
  67. string addBatesPath = outputPath + "\\AddBatesTest.pdf";
  68. CPDFBates bates = document.GetBates();
  69. byte[] color = { 255, 0, 0 };
  70. bates.SetPages("0-" + (document.PageCount - 1));//Page numbering from 0
  71. for (int i = 0; i <= 5; i++)
  72. {
  73. bates.SetText(i, @"<<#3#5#Prefix-#-Suffix>>"); //3 digits, starting from 5
  74. bates.SetTextColor(i, color);
  75. bates.SetFontSize(i, 14);
  76. Console.WriteLine("Text: {0}", bates.GetText(i));
  77. Console.WriteLine("Location: {0}\n", IntToLocationDic[i]);
  78. }
  79. bates.Update();
  80. if (!document.WriteToFilePath(addBatesPath))
  81. {
  82. return false;
  83. }
  84. Console.WriteLine("Browse the changed file in " + addBatesPath);
  85. return true;
  86. }
  87. /// <summary>
  88. /// Edit bates, <<#3#5#Prefix-#-Suffix>> -> <<#3#1#ComPDFKit-#-ComPDFKit>>
  89. /// get current bates,
  90. /// then edit it
  91. /// </summary>
  92. /// <param name="document">documet with bates</param>
  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. /// <summary>
  120. /// Clear bates.
  121. /// </summary>
  122. /// <param name="document">documet with bates</param>
  123. private static bool ClearBates(CPDFDocument document)
  124. {
  125. CPDFBates bates = document.GetBates();
  126. bates.Clear();
  127. string clearBatesPath = outputPath + "\\ClearBatesTest.pdf";
  128. if (document.WriteToFilePath(clearBatesPath))
  129. {
  130. Console.WriteLine("Browse the changed file in " + clearBatesPath);
  131. return true;
  132. }
  133. else
  134. {
  135. return false;
  136. }
  137. }
  138. }
  139. }