HeaderFooterTest.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. using System.Xml.Linq;
  9. using static System.Net.Mime.MediaTypeNames;
  10. namespace HeaderFooterTest
  11. {
  12. internal class HeaderFooterTest
  13. {
  14. private static string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\HeaderFooter";
  15. private static Dictionary<int, string> IntToLocationDic = new Dictionary<int, string>()
  16. {
  17. {0, "Top Left" },
  18. {1, "Top Middle" },
  19. {2, "Top Right" },
  20. {3, "Bottom Left" },
  21. {4, "Bottom Middle" },
  22. {5, "Bottom Right" }
  23. };
  24. static void Main(string[] args)
  25. {
  26. Console.WriteLine("Running header and footer test sample…\r\n");
  27. SDKLicenseHelper.LicenseVerify();
  28. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  29. if (!Directory.Exists(outputPath))
  30. {
  31. Directory.CreateDirectory(outputPath);
  32. }
  33. #region Add common header and footer
  34. if (AddCommonHeaderFooter(document))
  35. {
  36. Console.WriteLine("Add common header and footer done.");
  37. }
  38. else
  39. {
  40. Console.WriteLine("Add common header and footer failed.");
  41. }
  42. #endregion
  43. Console.WriteLine("--------------------");
  44. #region Add page header and footer
  45. if (AddPageHeaderFooter(document))
  46. {
  47. Console.WriteLine("Add page header and footer done.");
  48. }
  49. else
  50. {
  51. Console.WriteLine("Add page header and footer failed.");
  52. }
  53. #endregion
  54. Console.WriteLine("--------------------");
  55. #region Edit header and footer
  56. if (EditHeaderFooter(document))
  57. {
  58. Console.WriteLine("Edit header and footer done.");
  59. }
  60. else
  61. {
  62. Console.WriteLine("Edit header and footer failed.");
  63. }
  64. #endregion
  65. Console.WriteLine("--------------------");
  66. #region Delete header and footer
  67. if (ClearHeaderFooter(document))
  68. {
  69. Console.WriteLine("Delete header and footer done.\n");
  70. }
  71. else
  72. {
  73. Console.WriteLine("delete header and footer failed\n");
  74. }
  75. #endregion
  76. Console.WriteLine("--------------------");
  77. Console.WriteLine("Done");
  78. Console.WriteLine("--------------------");
  79. Console.ReadLine();
  80. }
  81. /// <summary>
  82. /// Follow these steps to add a header and footer in a blank pages file.
  83. /// </summary>
  84. /// <param name="document">Documents that require manipulation</param>
  85. static private bool AddCommonHeaderFooter(CPDFDocument document)
  86. {
  87. // Init HeaderFooter
  88. CPDFHeaderFooter headerFooter = document.GetHeaderFooter();
  89. byte[] color = { 255, 0, 0 };
  90. headerFooter.SetPages("0-" + (document.PageCount - 1));
  91. for (int i = 0; i <= 2; i++)
  92. {
  93. // Set Text: ComPDFKit
  94. headerFooter.SetText(i, "ComPDFKit");
  95. // Set page index: all the page
  96. // Set color : red
  97. headerFooter.SetTextColor(i, color);
  98. // Set font size : 14
  99. headerFooter.SetFontSize(i, 14);
  100. Console.WriteLine("Text: {0}", headerFooter.GetText(i));
  101. Console.WriteLine("Location: {0}\n", IntToLocationDic[i]);
  102. }
  103. // Update header and footer after changed
  104. headerFooter.Update();
  105. // Save to pointed path so you can observe the effect.
  106. string addHeaderFooterPath = outputPath + "\\AddCommonHeaderFooterTest.pdf";
  107. if (document.WriteToFilePath(addHeaderFooterPath))
  108. {
  109. Console.WriteLine("Browse the changed file in " + addHeaderFooterPath);
  110. return true;
  111. }
  112. else
  113. {
  114. return false;
  115. }
  116. }
  117. static private bool AddPageHeaderFooter(CPDFDocument document)
  118. {
  119. // Init HeaderFooter
  120. CPDFHeaderFooter headerFooter = document.GetHeaderFooter();
  121. byte[] color = { 255, 0, 0 };
  122. // Set page index: all the page
  123. headerFooter.SetPages("0-" + (document.PageCount - 1));
  124. for (int i = 3; i <= 5; i++)
  125. {
  126. // Set Text: 1
  127. headerFooter.SetText(i, "<<1,2>>");
  128. // Set color : red
  129. headerFooter.SetTextColor(i, color);
  130. // Set font size : 14
  131. headerFooter.SetFontSize(i, 14);
  132. Console.WriteLine("Text: {0}", headerFooter.GetText(i));
  133. Console.WriteLine("Location: {0}\n", IntToLocationDic[i]);
  134. }
  135. // Update header and footer after changed
  136. headerFooter.Update();
  137. string addHeaderFooterPath = outputPath + "\\AddPageHeaderFooterTest.pdf";
  138. if (document.WriteToFilePath(addHeaderFooterPath))
  139. {
  140. Console.WriteLine("Browse the changed file in " + addHeaderFooterPath);
  141. return true;
  142. }
  143. else
  144. {
  145. return false;
  146. }
  147. }
  148. /// <summary>
  149. /// Follow these steps to delete a header and footer in a blank pages file.
  150. /// </summary>
  151. /// <param name="document">Documents that require manipulation</param>
  152. static private bool EditHeaderFooter(CPDFDocument document)
  153. {
  154. CPDFHeaderFooter headerFooter = document.GetHeaderFooter();
  155. if (headerFooter.GetText(0) != string.Empty)
  156. {
  157. Console.WriteLine("Get old head and footer 0 succeeded, text is {0}", headerFooter.GetText(0));
  158. }
  159. else
  160. {
  161. Console.WriteLine("Get head and footer 0 failed, or it does not exist");
  162. return false;
  163. }
  164. headerFooter.SetText(0, "ComPDFKit Samples");
  165. headerFooter.Update();
  166. Console.WriteLine("Change head and footer 0 succeeded, new text is {0}", headerFooter.GetText(0));
  167. string editHeaderFooterPath = outputPath + "\\EditHeaderFooterTest.pdf";
  168. if (document.WriteToFilePath(editHeaderFooterPath))
  169. {
  170. Console.WriteLine("Browse the changed file in " + editHeaderFooterPath);
  171. return true;
  172. }
  173. else
  174. {
  175. return false;
  176. }
  177. }
  178. static private bool ClearHeaderFooter(CPDFDocument document)
  179. {
  180. CPDFHeaderFooter headerFooter = document.GetHeaderFooter();
  181. headerFooter.Clear();
  182. string clearHeaderFooterPath = outputPath + "\\ClearHeaderFooterTest.pdf";
  183. if (document.WriteToFilePath(clearHeaderFooterPath))
  184. {
  185. Console.WriteLine("Browse the changed file in " + clearHeaderFooterPath);
  186. return true;
  187. }
  188. else
  189. {
  190. return false;
  191. }
  192. }
  193. }
  194. }