PDFPageTest.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using ComPDFKit.PDFDocument;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace PDFPageTest
  10. {
  11. internal class PDFPageTest
  12. {
  13. static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\PDFPageTest";
  14. static void Main(string[] args)
  15. {
  16. Console.WriteLine("Running PDFPage test sample…\r\n");
  17. SDKLicenseHelper.LicenseVerify();
  18. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  19. if (!Directory.Exists(outputPath))
  20. {
  21. Directory.CreateDirectory(outputPath);
  22. }
  23. if (InsertBlankPage(document))
  24. {
  25. Console.WriteLine("Insert blank page done.");
  26. }
  27. else
  28. {
  29. Console.WriteLine("Insert blank page failed.");
  30. }
  31. document.Release();
  32. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  33. Console.WriteLine("--------------------");
  34. if (InsertPDFPPage(document))
  35. {
  36. Console.WriteLine("Insert PDF page done.");
  37. }
  38. else
  39. {
  40. Console.WriteLine("Insert PDF page failed.");
  41. }
  42. document.Release();
  43. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  44. Console.WriteLine("--------------------");
  45. if (SplitPages(document))
  46. {
  47. Console.WriteLine("Split page done.");
  48. }
  49. else
  50. {
  51. Console.WriteLine("Split failed.");
  52. }
  53. document.Release();
  54. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  55. Console.WriteLine("--------------------");
  56. if (RemovePaes(document))
  57. {
  58. Console.WriteLine("Delete even page done.");
  59. }
  60. else
  61. {
  62. Console.WriteLine("Delete even page failed.");
  63. }
  64. document.Release();
  65. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  66. Console.WriteLine("--------------------");
  67. if (RotatePage(document))
  68. {
  69. Console.WriteLine("Rotate page done.");
  70. }
  71. else
  72. {
  73. Console.WriteLine("Rotate page failed.");
  74. }
  75. document.Release();
  76. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  77. Console.WriteLine("--------------------");
  78. if (RepalcePages(document))
  79. {
  80. Console.WriteLine("Repalce page done.");
  81. }
  82. else
  83. {
  84. Console.WriteLine("Repalce page failed.");
  85. }
  86. document.Release();
  87. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  88. Console.WriteLine("--------------------");
  89. if (ExtractPages(document))
  90. {
  91. Console.WriteLine("Extract page done.");
  92. }
  93. else
  94. {
  95. Console.WriteLine("Extract page failed.");
  96. }
  97. Console.WriteLine("--------------------");
  98. Console.WriteLine("Done");
  99. Console.WriteLine("--------------------");
  100. Console.ReadLine();
  101. }
  102. static private bool InsertBlankPage(CPDFDocument document)
  103. {
  104. int pageIndex = 1;
  105. int pageWidth = 595;
  106. int pageHeight = 842;
  107. document.InsertPage(pageIndex, pageWidth, pageHeight, null);
  108. Console.WriteLine("Insert PageIndex: {0}", pageIndex);
  109. Console.WriteLine("Size; {0}*{1}", pageWidth, pageHeight);
  110. if (document.PageCount != 6)
  111. {
  112. return false;
  113. }
  114. string path = outputPath + "\\InsertBlankPageTest.pdf";
  115. if (!document.WriteToFilePath(path))
  116. {
  117. return false;
  118. }
  119. Console.WriteLine("Browse the changed file in " + path);
  120. return true;
  121. }
  122. static private bool InsertPDFPPage(CPDFDocument document)
  123. {
  124. CPDFDocument documentForInsert = CPDFDocument.InitWithFilePath("Text.pdf");
  125. document.ImportPagesAtIndex(documentForInsert, "1", 1);
  126. if (document.PageCount != 6)
  127. {
  128. return false;
  129. }
  130. string path = outputPath + "\\InsertPDFPPageTest.pdf";
  131. if (!document.WriteToFilePath(path))
  132. {
  133. return false;
  134. }
  135. Console.WriteLine("Browse the changed file in " + path);
  136. return true;
  137. }
  138. static private bool SplitPages(CPDFDocument document)
  139. {
  140. CPDFDocument documentPart1 = CPDFDocument.CreateDocument();
  141. CPDFDocument documentPart2 = CPDFDocument.CreateDocument();
  142. documentPart1.ImportPagesAtIndex(document, "1-2", 0);
  143. documentPart2.ImportPagesAtIndex(document, "3-5", 0);
  144. if (documentPart1.PageCount != 2)
  145. {
  146. return false;
  147. }
  148. string pathPart1 = outputPath + "\\SplitPart1Test.pdf";
  149. if (!documentPart1.WriteToFilePath(pathPart1))
  150. {
  151. return false;
  152. }
  153. if (documentPart2.PageCount != 3)
  154. {
  155. return false;
  156. }
  157. string pathPart2 = outputPath + "\\SplitPart2Test.pdf";
  158. if (!documentPart2.WriteToFilePath(pathPart2))
  159. {
  160. return false;
  161. }
  162. Console.WriteLine("Browse the changed file in " + pathPart1);
  163. Console.WriteLine("Browse the changed file in " + pathPart2);
  164. return true;
  165. }
  166. static private bool RemovePaes(CPDFDocument document)
  167. {
  168. ArrayList arr = new ArrayList();
  169. for (int i = 2; i < document.PageCount; i += 2){
  170. arr.Add(i);
  171. }
  172. document.RemovePages((int[])arr.ToArray(typeof(int)));
  173. if(document.PageCount != 3)
  174. {
  175. return false;
  176. }
  177. string path = outputPath + "\\RemoveEvenPagesTest.pdf";
  178. if (!document.WriteToFilePath(path))
  179. {
  180. return false;
  181. }
  182. Console.WriteLine("Browse the changed file in " + path);
  183. return true;
  184. }
  185. static private bool RotatePage(CPDFDocument document)
  186. {
  187. document.RotatePage(0, 1);
  188. string path = outputPath + "\\RotatePageTest.pdf";
  189. if (!document.WriteToFilePath(path))
  190. {
  191. return false;
  192. }
  193. Console.WriteLine("Browse the changed file in " + path);
  194. return true;
  195. }
  196. static private bool RepalcePages(CPDFDocument document)
  197. {
  198. int[] pageArr = new int[1];
  199. pageArr[0] = 0;
  200. document.RemovePages(pageArr);
  201. CPDFDocument documentForInsert = CPDFDocument.InitWithFilePath("Text.pdf");
  202. document.ImportPagesAtIndex(documentForInsert, "1", 0);
  203. string path = outputPath + "\\RepalcePagesTest.pdf";
  204. if (!document.WriteToFilePath(path))
  205. {
  206. return false;
  207. }
  208. Console.WriteLine("Browse the changed file in " + path);
  209. return true;
  210. }
  211. static private bool ExtractPages(CPDFDocument document)
  212. {
  213. CPDFDocument extractDocument = CPDFDocument.CreateDocument();
  214. extractDocument.ImportPagesAtIndex(document, "1", 0);
  215. if (!(extractDocument.PageCount == 1))
  216. {
  217. return false;
  218. }
  219. string path = outputPath + "\\ExtractPagesTest.pdf";
  220. if (!extractDocument.WriteToFilePath(path))
  221. {
  222. return false;
  223. }
  224. Console.WriteLine("Browse the changed file in " + path);
  225. return true;
  226. }
  227. }
  228. }