HeaderFooterTest.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. document.Release();
  43. Console.WriteLine("--------------------");
  44. #endregion
  45. #region Add page header and footer
  46. if (AddPageHeaderFooter(document))
  47. {
  48. Console.WriteLine("Add page header and footer done.");
  49. }
  50. else
  51. {
  52. Console.WriteLine("Add page header and footer failed.");
  53. }
  54. #endregion
  55. Console.WriteLine("--------------------");
  56. #region Edit header and footer
  57. if (EditHeaderFooter(document))
  58. {
  59. Console.WriteLine("Edit header and footer done.");
  60. }
  61. else
  62. {
  63. Console.WriteLine("Edit header and footer failed.");
  64. }
  65. #endregion
  66. Console.WriteLine("--------------------");
  67. #region Delete header and footer
  68. if (ClearHeaderFooter(document))
  69. {
  70. Console.WriteLine("Delete header and footer done.\n");
  71. }
  72. else
  73. {
  74. Console.WriteLine("delete header and footer failed\n");
  75. }
  76. #endregion
  77. Console.WriteLine("--------------------");
  78. Console.WriteLine("Done");
  79. Console.WriteLine("--------------------");
  80. Console.ReadLine();
  81. }
  82. /// <summary>
  83. /// Follow these steps to add a header and footer in a blank pages file.
  84. /// </summary>
  85. /// <param name="document">Regular document</param>
  86. static private bool AddCommonHeaderFooter(CPDFDocument document)
  87. {
  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. headerFooter.SetText(i, "ComPDFKit");
  94. headerFooter.SetTextColor(i, color);
  95. headerFooter.SetFontSize(i, 14);
  96. Console.WriteLine("Text: {0}", headerFooter.GetText(i));
  97. Console.WriteLine("Location: {0}\n", IntToLocationDic[i]);
  98. }
  99. headerFooter.Update();
  100. string addHeaderFooterPath = outputPath + "\\AddCommonHeaderFooterTest.pdf";
  101. if (!document.WriteToFilePath(addHeaderFooterPath))
  102. {
  103. return false;
  104. }
  105. Console.WriteLine("Browse the changed file in " + addHeaderFooterPath);
  106. return true;
  107. }
  108. /// <summary>
  109. /// Add headers and footers that automatically display page numbers
  110. /// </summary>
  111. /// <param name="document">Regular document</param>
  112. static private bool AddPageHeaderFooter(CPDFDocument document)
  113. {
  114. CPDFHeaderFooter headerFooter = document.GetHeaderFooter();
  115. byte[] color = { 255, 0, 0 };
  116. headerFooter.SetPages("0-" + (document.PageCount - 1));
  117. for (int i = 3; i <= 5; i++)
  118. {
  119. headerFooter.SetText(i, "<<1,2>>");
  120. headerFooter.SetTextColor(i, color);
  121. headerFooter.SetFontSize(i, 14);
  122. Console.WriteLine("Text: {0}", headerFooter.GetText(i));
  123. Console.WriteLine("Location: {0}\n", IntToLocationDic[i]);
  124. }
  125. headerFooter.Update();
  126. string addHeaderFooterPath = outputPath + "\\AddPageHeaderFooterTest.pdf";
  127. if (document.WriteToFilePath(addHeaderFooterPath))
  128. {
  129. Console.WriteLine("Browse the changed file in " + addHeaderFooterPath);
  130. return true;
  131. }
  132. else
  133. {
  134. return false;
  135. }
  136. }
  137. /// <summary>
  138. /// Follow these steps to delete a header and footer in a blank pages file.
  139. /// </summary>
  140. /// <param name="document">Documents that require manipulation</param>
  141. static private bool EditHeaderFooter(CPDFDocument document)
  142. {
  143. CPDFHeaderFooter headerFooter = document.GetHeaderFooter();
  144. if (headerFooter.GetText(0) != string.Empty)
  145. {
  146. Console.WriteLine("Get old head and footer 0 succeeded, text is {0}", headerFooter.GetText(0));
  147. }
  148. else
  149. {
  150. Console.WriteLine("Get head and footer 0 failed, or it does not exist");
  151. return false;
  152. }
  153. headerFooter.SetText(0, "ComPDFKit Samples");
  154. headerFooter.Update();
  155. Console.WriteLine("Change head and footer 0 succeeded, new text is {0}", headerFooter.GetText(0));
  156. string editHeaderFooterPath = outputPath + "\\EditHeaderFooterTest.pdf";
  157. if (document.WriteToFilePath(editHeaderFooterPath))
  158. {
  159. Console.WriteLine("Browse the changed file in " + editHeaderFooterPath);
  160. return true;
  161. }
  162. else
  163. {
  164. return false;
  165. }
  166. }
  167. static private bool ClearHeaderFooter(CPDFDocument document)
  168. {
  169. CPDFHeaderFooter headerFooter = document.GetHeaderFooter();
  170. headerFooter.Clear();
  171. string clearHeaderFooterPath = outputPath + "\\ClearHeaderFooterTest.pdf";
  172. if (document.WriteToFilePath(clearHeaderFooterPath))
  173. {
  174. Console.WriteLine("Browse the changed file in " + clearHeaderFooterPath);
  175. return true;
  176. }
  177. else
  178. {
  179. return false;
  180. }
  181. }
  182. }
  183. }