HeaderFooterTest.cs 7.3 KB

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