PDFPageTest.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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\\CS";
  14. static void Main(string[] args)
  15. {
  16. #region Perparation work
  17. Console.WriteLine("Running PDFPage test sample…\r\n");
  18. SDKLicenseHelper.LicenseVerify();
  19. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  20. if (!Directory.Exists(outputPath))
  21. {
  22. Directory.CreateDirectory(outputPath);
  23. }
  24. #endregion
  25. #region Sample 1: Insert blank page
  26. if (InsertBlankPage(document))
  27. {
  28. Console.WriteLine("Insert blank page done.");
  29. }
  30. else
  31. {
  32. Console.WriteLine("Insert blank page failed.");
  33. }
  34. document.Release();
  35. Console.WriteLine("--------------------");
  36. #endregion
  37. #region Sample 2: Insert PDF page
  38. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  39. if (InsertPDFPPage(document))
  40. {
  41. Console.WriteLine("Insert PDF page done.");
  42. }
  43. else
  44. {
  45. Console.WriteLine("Insert PDF page failed.");
  46. }
  47. document.Release();
  48. Console.WriteLine("--------------------");
  49. #endregion
  50. #region Sample 3: Split pages
  51. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  52. if (SplitPages(document))
  53. {
  54. Console.WriteLine("Split page done.");
  55. }
  56. else
  57. {
  58. Console.WriteLine("Split failed.");
  59. }
  60. document.Release();
  61. Console.WriteLine("--------------------");
  62. #endregion
  63. #region Sample 4: Remove pages
  64. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  65. if (RemovePages(document))
  66. {
  67. Console.WriteLine("Delete even page done.");
  68. }
  69. else
  70. {
  71. Console.WriteLine("Delete even page failed.");
  72. }
  73. document.Release();
  74. Console.WriteLine("--------------------");
  75. #endregion
  76. #region Sample 5: Rotate page
  77. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  78. if (RotatePage(document))
  79. {
  80. Console.WriteLine("Rotate page done.");
  81. }
  82. else
  83. {
  84. Console.WriteLine("Rotate page failed.");
  85. }
  86. document.Release();
  87. Console.WriteLine("--------------------");
  88. #endregion
  89. #region Sample 6: Repalce pages
  90. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  91. if (RepalcePages(document))
  92. {
  93. Console.WriteLine("Repalce page done.");
  94. }
  95. else
  96. {
  97. Console.WriteLine("Repalce page failed.");
  98. }
  99. document.Release();
  100. Console.WriteLine("--------------------");
  101. #endregion
  102. #region Sample 7: Extract pages
  103. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  104. if (ExtractPages(document))
  105. {
  106. Console.WriteLine("Extract page done.");
  107. }
  108. else
  109. {
  110. Console.WriteLine("Extract page failed.");
  111. }
  112. document.Release();
  113. Console.WriteLine("--------------------");
  114. #endregion
  115. Console.WriteLine("Done");
  116. Console.WriteLine("--------------------");
  117. Console.ReadLine();
  118. }
  119. /// <summary>
  120. /// Insert a new page of A4 size at the second page
  121. /// </summary>
  122. /// <param name="document">Regular five page document</param>
  123. static private bool InsertBlankPage(CPDFDocument document)
  124. {
  125. int pageIndex = 1;
  126. int pageWidth = 595;
  127. int pageHeight = 842;
  128. document.InsertPage(pageIndex, pageWidth, pageHeight, "");
  129. Console.WriteLine("Insert PageIndex: {0}", pageIndex);
  130. Console.WriteLine("Size: {0}*{1}", pageWidth, pageHeight);
  131. string path = outputPath + "\\InsertBlankPageTest.pdf";
  132. if (!document.WriteToFilePath(path))
  133. {
  134. return false;
  135. }
  136. Console.WriteLine("Browse the changed file in " + path);
  137. return true;
  138. }
  139. /// <summary>
  140. /// Select pages from other PDF files and insert them into the current document
  141. /// </summary>
  142. /// <param name="document">Regular five page document</param>
  143. static private bool InsertPDFPPage(CPDFDocument document)
  144. {
  145. CPDFDocument documentForInsert = CPDFDocument.InitWithFilePath("Text.pdf");
  146. document.ImportPagesAtIndex(documentForInsert, "1", 1);
  147. string path = outputPath + "\\InsertPDFPPageTest.pdf";
  148. if (!document.WriteToFilePath(path))
  149. {
  150. return false;
  151. }
  152. Console.WriteLine("Browse the changed file in " + path);
  153. return true;
  154. }
  155. /// <summary>
  156. /// Split the current document into two documents according to the first 2 pages and the last 3 pages
  157. /// </summary>
  158. /// <param name="document">Regular five page document</param>
  159. static private bool SplitPages(CPDFDocument document)
  160. {
  161. //Split 1-2 pages
  162. CPDFDocument documentPart1 = CPDFDocument.CreateDocument();
  163. documentPart1.ImportPagesAtIndex(document, "1-2", 0);
  164. string pathPart1 = outputPath + "\\SplitPart1Test.pdf";
  165. if (!documentPart1.WriteToFilePath(pathPart1))
  166. {
  167. return false;
  168. }
  169. Console.WriteLine("Browse the changed file in " + pathPart1);
  170. //Split 3-5 pages
  171. CPDFDocument documentPart2 = CPDFDocument.CreateDocument();
  172. documentPart2.ImportPagesAtIndex(document, "3-5", 0);
  173. string pathPart2 = outputPath + "\\SplitPart2Test.pdf";
  174. if (!documentPart2.WriteToFilePath(pathPart2))
  175. {
  176. return false;
  177. }
  178. Console.WriteLine("Browse the changed file in " + pathPart2);
  179. return true;
  180. }
  181. /// <summary>
  182. /// Remove even-numbered pages from a document
  183. /// </summary>
  184. /// <param name="document">Regular five page document</param>
  185. static private bool RemovePages(CPDFDocument document)
  186. {
  187. List<int> pageNumbersToRemove = new List<int>();
  188. for (int i = 1; i < document.PageCount; i += 2)
  189. {
  190. pageNumbersToRemove.Add(i);
  191. }
  192. document.RemovePages(pageNumbersToRemove.ToArray());
  193. string path = outputPath + "\\RemoveEvenPagesTest.pdf";
  194. if (!document.WriteToFilePath(path))
  195. {
  196. return false;
  197. }
  198. Console.WriteLine("Browse the changed file in " + path);
  199. return true;
  200. }
  201. /// <summary>
  202. /// Rotate the first page 90 degrees clockwise
  203. /// </summary>
  204. /// <param name="document">Regular five page document</param>
  205. static private bool RotatePage(CPDFDocument document)
  206. {
  207. document.RotatePage(0, 1);//Rotation: Rotate 90 degrees per unit
  208. string path = outputPath + "\\RotatePageTest.pdf";
  209. if (!document.WriteToFilePath(path))
  210. {
  211. return false;
  212. }
  213. Console.WriteLine("Browse the changed file in " + path);
  214. return true;
  215. }
  216. /// <summary>
  217. /// Replace the first page of the current document with a page from another document
  218. /// Delete the pages that need to be replaced first
  219. /// Insert the required pages into the document
  220. /// </summary>
  221. /// <param name="document">Regular five page document</param>
  222. static private bool RepalcePages(CPDFDocument document)
  223. {
  224. List<int> pageList = new List<int>() { 0 };
  225. document.RemovePages(pageList.ToArray());
  226. CPDFDocument documentForInsert = CPDFDocument.InitWithFilePath("Text.pdf");
  227. document.ImportPagesAtIndex(documentForInsert, "1", 0);
  228. string path = outputPath + "\\RepalcePagesTest.pdf";
  229. if (!document.WriteToFilePath(path))
  230. {
  231. return false;
  232. }
  233. Console.WriteLine("Browse the changed file in " + path);
  234. return true;
  235. }
  236. /// <summary>
  237. /// Extract pages from a document
  238. /// Create a new document
  239. /// Insert the required pages into a new document
  240. /// </summary>
  241. /// <param name="document">Regular five page document</param>
  242. static private bool ExtractPages(CPDFDocument document)
  243. {
  244. CPDFDocument extractDocument = CPDFDocument.CreateDocument();
  245. extractDocument.ImportPagesAtIndex(document, "1", 0);
  246. string path = outputPath + "\\ExtractPagesTest.pdf";
  247. if (!extractDocument.WriteToFilePath(path))
  248. {
  249. return false;
  250. }
  251. Console.WriteLine("Browse the changed file in " + path);
  252. return true;
  253. }
  254. }
  255. }