OutlineTest.cs 6.3 KB

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