OutlineTest.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFDocument.Action;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. namespace OutlineTest
  7. {
  8. internal class OutlineTest
  9. {
  10. static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\CS";
  11. static private int outlineCounter = 0;
  12. static private int outlineNumber = 0;
  13. static void Main(string[] args)
  14. {
  15. #region Perparation work
  16. Console.WriteLine("Running Outline test sample…\r\n");
  17. SDKLicenseHelper.LicenseVerify();
  18. if (!Directory.Exists(outputPath))
  19. {
  20. Directory.CreateDirectory(outputPath);
  21. }
  22. #endregion
  23. #region Sample 1: Print outline
  24. CPDFDocument document = CPDFDocument.InitWithFilePath("FourOutline.pdf");
  25. if (PrintOutline(document))
  26. {
  27. Console.WriteLine("Print outline done.");
  28. }
  29. else
  30. {
  31. Console.WriteLine("Print outline failed.");
  32. }
  33. document.Release();
  34. Console.WriteLine("--------------------");
  35. #endregion
  36. #region Sample 2: Create outline
  37. document = CPDFDocument.InitWithFilePath("FourOutline.pdf");
  38. if (CreateOutline(document))
  39. {
  40. Console.WriteLine("Create outline done.");
  41. }
  42. else
  43. {
  44. Console.WriteLine("Create outline failed.");
  45. }
  46. document.Release();
  47. Console.WriteLine("--------------------");
  48. #endregion
  49. #region Sample 3: Move outline
  50. document = CPDFDocument.InitWithFilePath("FourOutline.pdf");
  51. if (MoveOutline(document))
  52. {
  53. Console.WriteLine("Move outline done.");
  54. }
  55. else
  56. {
  57. Console.WriteLine("Move outline failed.");
  58. }
  59. document.Release();
  60. Console.WriteLine("--------------------");
  61. #endregion
  62. #region Sample 4: Remove outline
  63. document = CPDFDocument.InitWithFilePath("FourOutline.pdf");
  64. if (RemoveOutline(document))
  65. {
  66. Console.WriteLine("Remove outline done.");
  67. }
  68. else
  69. {
  70. Console.WriteLine("Remove outline failed.");
  71. }
  72. document.Release();
  73. Console.WriteLine("--------------------");
  74. #endregion
  75. Console.WriteLine("Done.");
  76. Console.WriteLine("--------------------");
  77. Console.ReadLine();
  78. }
  79. /// <summary>
  80. /// Traverse outline and print it as a list
  81. /// </summary>
  82. static private void TraverseOutline(List<CPDFOutline> outlineList)
  83. {
  84. foreach (var outline in outlineList)
  85. {
  86. for (var i = 0; i < outlineCounter; i++)
  87. {
  88. Console.Write(" ");
  89. }
  90. Console.Write("-> " + outline.Title + "\n");
  91. outlineNumber++;
  92. var childList = outline.ChildList;
  93. if (childList != null && childList.Count != 0)
  94. {
  95. outlineCounter++;
  96. TraverseOutline(childList);
  97. }
  98. else
  99. {
  100. if (outlineList.IndexOf(outline)+1 == outlineList.Count)
  101. {
  102. outlineCounter--;
  103. }
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Print all outlines in the file
  109. /// </summary>
  110. /// <param name="document">Document with some outlines</param>
  111. static private bool PrintOutline(CPDFDocument document)
  112. {
  113. List<CPDFOutline> outlineList = document.GetOutlineList();
  114. outlineNumber = 0;
  115. outlineCounter = 0;
  116. TraverseOutline(outlineList);
  117. Console.WriteLine("Outline number: {0}", outlineNumber);
  118. return true;
  119. }
  120. /// <summary>
  121. /// Create an outline at the top of the first page
  122. /// </summary>
  123. /// <param name="document">Document with some outlines</param>
  124. static private bool CreateOutline(CPDFDocument document)
  125. {
  126. CPDFOutline outline = document.GetOutlineRoot();
  127. CPDFOutline childOutline = null;
  128. outline.InsertChildAtIndex(document, 0, ref childOutline);
  129. CPDFGoToAction gotoAction = new CPDFGoToAction();
  130. CPDFDestination dest = new CPDFDestination();
  131. dest.PageIndex = 1;
  132. gotoAction.SetDestination(document, dest);
  133. childOutline.SetAction(gotoAction);
  134. childOutline.SetTitle("New outline");
  135. PrintOutline(document);
  136. return true;
  137. }
  138. /// <summary>
  139. /// Create an outline at the top of the first page
  140. /// </summary>
  141. /// <param name="document"></param>
  142. /// <returns></returns>
  143. static private bool MoveOutline(CPDFDocument document)
  144. {
  145. CPDFOutline outline = document.GetOutlineRoot();
  146. outline.InsertChildAtIndex(document, outline.ChildList.Count, ref outline);
  147. outline.SetTitle("new outline");
  148. CPDFOutline targetOutline = document.GetOutlineList()[1];
  149. targetOutline.MoveChildAtIndex(document, outline, targetOutline.ChildList.Count);
  150. string path = outputPath + "\\MoveOutlineTest.pdf";
  151. if (!document.WriteToFilePath(path))
  152. {
  153. return false;
  154. }
  155. CPDFDocument newDocument = CPDFDocument.InitWithFilePath(path);
  156. PrintOutline(newDocument);
  157. return true;
  158. }
  159. /// <summary>
  160. /// Remove outline
  161. /// </summary>
  162. static private bool RemoveOutline(CPDFDocument document)
  163. {
  164. document.GetOutlineList()[0].RemoveFromParent(document);
  165. string path = outputPath + "\\RemoveOutlineTest.pdf";
  166. if (!document.WriteToFilePath(path))
  167. {
  168. return false;
  169. }
  170. CPDFDocument newDocument = CPDFDocument.InitWithFilePath(path);
  171. PrintOutline(newDocument);
  172. return true;
  173. }
  174. }
  175. }