HeaderFooterTest.cs 6.9 KB

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