|
@@ -14,6 +14,7 @@ namespace PDFPageTest
|
|
|
static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\PDFPageTest";
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
+ #region Perparation work
|
|
|
Console.WriteLine("Running PDFPage test sample…\r\n");
|
|
|
|
|
|
SDKLicenseHelper.LicenseVerify();
|
|
@@ -22,6 +23,9 @@ namespace PDFPageTest
|
|
|
{
|
|
|
Directory.CreateDirectory(outputPath);
|
|
|
}
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Sample 1: Insert blank page
|
|
|
|
|
|
if (InsertBlankPage(document))
|
|
|
{
|
|
@@ -32,8 +36,13 @@ namespace PDFPageTest
|
|
|
Console.WriteLine("Insert blank page failed.");
|
|
|
}
|
|
|
document.Release();
|
|
|
- document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
|
|
|
Console.WriteLine("--------------------");
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Sample 2: Insert PDF page
|
|
|
+
|
|
|
+ document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
|
|
|
if (InsertPDFPPage(document))
|
|
|
{
|
|
|
Console.WriteLine("Insert PDF page done.");
|
|
@@ -43,8 +52,13 @@ namespace PDFPageTest
|
|
|
Console.WriteLine("Insert PDF page failed.");
|
|
|
}
|
|
|
document.Release();
|
|
|
- document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
|
|
|
Console.WriteLine("--------------------");
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Sample 3: Split pages
|
|
|
+
|
|
|
+ document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
|
|
|
if (SplitPages(document))
|
|
|
{
|
|
|
Console.WriteLine("Split page done.");
|
|
@@ -52,21 +66,31 @@ namespace PDFPageTest
|
|
|
else
|
|
|
{
|
|
|
Console.WriteLine("Split failed.");
|
|
|
- }
|
|
|
+ }
|
|
|
document.Release();
|
|
|
- document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
|
|
|
Console.WriteLine("--------------------");
|
|
|
- if (RemovePaes(document))
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Sample 4: Remove pages
|
|
|
+
|
|
|
+ document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
|
|
|
+ if (RemovePages(document))
|
|
|
{
|
|
|
Console.WriteLine("Delete even page done.");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Console.WriteLine("Delete even page failed.");
|
|
|
- }
|
|
|
+ }
|
|
|
document.Release();
|
|
|
- document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
|
|
|
Console.WriteLine("--------------------");
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Sample 5: Rotate page
|
|
|
+
|
|
|
+ document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
|
|
|
if (RotatePage(document))
|
|
|
{
|
|
|
Console.WriteLine("Rotate page done.");
|
|
@@ -77,8 +101,13 @@ namespace PDFPageTest
|
|
|
}
|
|
|
|
|
|
document.Release();
|
|
|
- document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
|
|
|
Console.WriteLine("--------------------");
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Sample 6: Repalce pages
|
|
|
+
|
|
|
+ document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
|
|
|
if (RepalcePages(document))
|
|
|
{
|
|
|
Console.WriteLine("Repalce page done.");
|
|
@@ -89,8 +118,12 @@ namespace PDFPageTest
|
|
|
}
|
|
|
|
|
|
document.Release();
|
|
|
- document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
|
|
|
Console.WriteLine("--------------------");
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Sample 7: Extract pages
|
|
|
+ document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
|
|
|
if (ExtractPages(document))
|
|
|
{
|
|
|
Console.WriteLine("Extract page done.");
|
|
@@ -99,14 +132,21 @@ namespace PDFPageTest
|
|
|
{
|
|
|
Console.WriteLine("Extract page failed.");
|
|
|
}
|
|
|
-
|
|
|
+ document.Release();
|
|
|
Console.WriteLine("--------------------");
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
Console.WriteLine("Done");
|
|
|
Console.WriteLine("--------------------");
|
|
|
|
|
|
Console.ReadLine();
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Insert a new page of A4 size at the second page
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="document">Regular five page document</param>
|
|
|
static private bool InsertBlankPage(CPDFDocument document)
|
|
|
{
|
|
|
int pageIndex = 1;
|
|
@@ -114,12 +154,7 @@ namespace PDFPageTest
|
|
|
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;
|
|
|
- }
|
|
|
+ Console.WriteLine("Size: {0}*{1}", pageWidth, pageHeight);
|
|
|
|
|
|
string path = outputPath + "\\InsertBlankPageTest.pdf";
|
|
|
if (!document.WriteToFilePath(path))
|
|
@@ -130,14 +165,15 @@ namespace PDFPageTest
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Select pages from other PDF files and insert them into the current document
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="document">Regular five page document</param>
|
|
|
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))
|
|
|
{
|
|
@@ -147,46 +183,49 @@ namespace PDFPageTest
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Split the current document into two documents according to the first 2 pages and the last 3 pages
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="document">Regular five page document</param>
|
|
|
static private bool SplitPages(CPDFDocument document)
|
|
|
{
|
|
|
+ //Split 1-2 pages
|
|
|
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;
|
|
|
- }
|
|
|
+ Console.WriteLine("Browse the changed file in " + pathPart1);
|
|
|
+
|
|
|
+ //Split 3-5 pages
|
|
|
+ CPDFDocument documentPart2 = CPDFDocument.CreateDocument();
|
|
|
+ documentPart2.ImportPagesAtIndex(document, "3-5", 0);
|
|
|
+
|
|
|
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)
|
|
|
+ /// <summary>
|
|
|
+ /// Remove even-numbered pages from a document
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="document">Regular five page document</param>
|
|
|
+ static private bool RemovePages(CPDFDocument document)
|
|
|
{
|
|
|
ArrayList arr = new ArrayList();
|
|
|
- for (int i = 2; i < document.PageCount; i += 2){
|
|
|
+ for (int i = 1; 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))
|
|
|
{
|
|
@@ -196,9 +235,13 @@ namespace PDFPageTest
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Rotate the first page 90 degrees clockwise
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="document">Regular five page document</param>
|
|
|
static private bool RotatePage(CPDFDocument document)
|
|
|
{
|
|
|
- document.RotatePage(0, 1);
|
|
|
+ document.RotatePage(0, 1);//Rotation: Rotate 90 degrees per unit
|
|
|
string path = outputPath + "\\RotatePageTest.pdf";
|
|
|
if (!document.WriteToFilePath(path))
|
|
|
{
|
|
@@ -208,6 +251,12 @@ namespace PDFPageTest
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Replace the first page of the current document with a page from another document
|
|
|
+ /// Delete the pages that need to be replaced first
|
|
|
+ /// Insert the required pages into the document
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="document">Regular five page document</param>
|
|
|
static private bool RepalcePages(CPDFDocument document)
|
|
|
{
|
|
|
int[] pageArr = new int[1];
|
|
@@ -224,14 +273,16 @@ namespace PDFPageTest
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Extract pages from a document
|
|
|
+ /// Create a new document
|
|
|
+ /// Insert the required pages into a new document
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="document">Regular five page document</param>
|
|
|
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))
|
|
|
{
|