using ComPDFKit.PDFDocument; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PDFPageTest { internal class PDFPageTest { static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\PDFPageTest"; static void Main(string[] args) { Console.WriteLine("Running PDFPage test sample…\r\n"); SDKLicenseHelper.LicenseVerify(); CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf"); if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } if (InsertBlankPage(document)) { Console.WriteLine("Insert blank page done."); } else { Console.WriteLine("Insert blank page failed."); } document.Release(); document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf"); Console.WriteLine("--------------------"); if (InsertPDFPPage(document)) { Console.WriteLine("Insert PDF page done."); } else { Console.WriteLine("Insert PDF page failed."); } document.Release(); document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf"); Console.WriteLine("--------------------"); if (SplitPages(document)) { Console.WriteLine("Split page done."); } else { Console.WriteLine("Split failed."); } document.Release(); document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf"); Console.WriteLine("--------------------"); if (RemovePaes(document)) { Console.WriteLine("Delete even page done."); } else { Console.WriteLine("Delete even page failed."); } document.Release(); document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf"); Console.WriteLine("--------------------"); if (RotatePage(document)) { Console.WriteLine("Rotate page done."); } else { Console.WriteLine("Rotate page failed."); } document.Release(); document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf"); Console.WriteLine("--------------------"); if (RepalcePages(document)) { Console.WriteLine("Repalce page done."); } else { Console.WriteLine("Repalce page failed."); } document.Release(); document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf"); Console.WriteLine("--------------------"); if (ExtractPages(document)) { Console.WriteLine("Extract page done."); } else { Console.WriteLine("Extract page failed."); } Console.WriteLine("--------------------"); Console.WriteLine("Done"); Console.WriteLine("--------------------"); Console.ReadLine(); } static private bool InsertBlankPage(CPDFDocument document) { int pageIndex = 1; int pageWidth = 595; int pageHeight = 842; document.InsertPage(pageIndex, pageWidth, pageHeight, null); Console.WriteLine("Insert PageIndex: {0}", pageIndex); Console.WriteLine("Size; {0}*{1}", pageWidth, pageHeight); if (document.PageCount != 6) { return false; } string path = outputPath + "\\InsertBlankPageTest.pdf"; if (!document.WriteToFilePath(path)) { return false; } Console.WriteLine("Browse the changed file in " + path); return true; } static private bool InsertPDFPPage(CPDFDocument document) { CPDFDocument documentForInsert = CPDFDocument.InitWithFilePath("Text.pdf"); document.ImportPagesAtIndex(documentForInsert, "1", 1); if (document.PageCount != 6) { return false; } string path = outputPath + "\\InsertPDFPPageTest.pdf"; if (!document.WriteToFilePath(path)) { return false; } Console.WriteLine("Browse the changed file in " + path); return true; } static private bool SplitPages(CPDFDocument document) { CPDFDocument documentPart1 = CPDFDocument.CreateDocument(); CPDFDocument documentPart2 = CPDFDocument.CreateDocument(); documentPart1.ImportPagesAtIndex(document, "1-2", 0); documentPart2.ImportPagesAtIndex(document, "3-5", 0); if (documentPart1.PageCount != 2) { return false; } string pathPart1 = outputPath + "\\SplitPart1Test.pdf"; if (!documentPart1.WriteToFilePath(pathPart1)) { return false; } if (documentPart2.PageCount != 3) { return false; } string pathPart2 = outputPath + "\\SplitPart2Test.pdf"; if (!documentPart2.WriteToFilePath(pathPart2)) { return false; } Console.WriteLine("Browse the changed file in " + pathPart1); Console.WriteLine("Browse the changed file in " + pathPart2); return true; } static private bool RemovePaes(CPDFDocument document) { ArrayList arr = new ArrayList(); for (int i = 2; i < document.PageCount; i += 2){ arr.Add(i); } document.RemovePages((int[])arr.ToArray(typeof(int))); if(document.PageCount != 3) { return false; } string path = outputPath + "\\RemoveEvenPagesTest.pdf"; if (!document.WriteToFilePath(path)) { return false; } Console.WriteLine("Browse the changed file in " + path); return true; } static private bool RotatePage(CPDFDocument document) { document.RotatePage(0, 1); string path = outputPath + "\\RotatePageTest.pdf"; if (!document.WriteToFilePath(path)) { return false; } Console.WriteLine("Browse the changed file in " + path); return true; } static private bool RepalcePages(CPDFDocument document) { int[] pageArr = new int[1]; pageArr[0] = 0; document.RemovePages(pageArr); CPDFDocument documentForInsert = CPDFDocument.InitWithFilePath("Text.pdf"); document.ImportPagesAtIndex(documentForInsert, "1", 0); string path = outputPath + "\\RepalcePagesTest.pdf"; if (!document.WriteToFilePath(path)) { return false; } Console.WriteLine("Browse the changed file in " + path); return true; } static private bool ExtractPages(CPDFDocument document) { CPDFDocument extractDocument = CPDFDocument.CreateDocument(); extractDocument.ImportPagesAtIndex(document, "1", 0); if (!(extractDocument.PageCount == 1)) { return false; } string path = outputPath + "\\ExtractPagesTest.pdf"; if (!extractDocument.WriteToFilePath(path)) { return false; } Console.WriteLine("Browse the changed file in " + path); return true; } } }