HeaderFooterTest.cs 6.6 KB

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