using it.stefanochizzolini.clown.documents;
using it.stefanochizzolini.clown.files;
using it.stefanochizzolini.clown.tools;
using System;
using System.Collections.Generic;
namespace it.stefanochizzolini.clown.samples{
/**
<summary>This sample demonstrates how to manipulate the pages collection within
a document, to perform movements, additions, removals and extractions of groups
of pages.</summary>
*/
public class PageManagementSample
: ISample
{
#region static
#region interface
#region private
private static int GetPageChoice(
string inputDescription,
int pageCount
)
{
int pageIndex = 0;
// Getting the user's choice about which page to remove...
Console.Write("\n" + inputDescription + " [1-" + pageCount + "]: ");
try
{pageIndex = Int32.Parse(Console.ReadLine()) - 1; /* Custom choice. */}
catch
{/* Default choice. */}
if(pageIndex < 0)
pageIndex = 0;
else if(pageIndex >= pageCount)
pageIndex = pageCount - 1;
return pageIndex;
}
#endregion
#endregion
#endregion
#region dynamic
#region interface
#region public
public void Run(
SampleLoader loader
)
{
while(true)
{
// (boilerplate user choice -- ignore it)
string filePath = loader.GetPdfFileChoice("Please select a PDF file");
// Open the PDF file!
File file = new File(filePath);
// Get the PDF document!
Document document = file.Document;
// Get the page collection!
Pages pages = document.Pages;
// Get the page count!
int pageCount = pages.Count;
// Are there more than one page?
if(pageCount > 1) // Multiple pages.
{
int action = 0;
// Getting the user's choice about which operation to perform...
Console.WriteLine("\nAvailable operations:");
Console.WriteLine("[0] Page removal");
Console.WriteLine("[1] Page addition");
Console.WriteLine("[2] Page movement");
Console.WriteLine("[3] Page extraction");
Console.Write("Select the operation to perform: ");
try{action = Int32.Parse(Console.ReadLine()); /* Custom choice. */}
catch{/* Default choice. */}
switch(action)
{
case 0: // Page removal.
{
// Get the user's choice about the first page to remove!
int fromPageIndex = GetPageChoice("Select the start page to remove",pageCount);
// Get the user's choice about the last page to remove!
int toPageIndex = GetPageChoice("Select the end page to remove",pageCount) + 1;
new PageManager(document).Remove(
fromPageIndex,
toPageIndex
);
}
break;
case 1: // Page addition.
{
String sourceFilePath = loader.GetPdfFileChoice("Select the source PDF file");
// Open the PDF file!
File sourceFile = new File(sourceFilePath);
// Get the page collection!
Pages sourcePages = sourceFile.Document.Pages;
// Get the page count!
int sourcePageCount = sourcePages.Count;
// Get the user's choice about the first page to add!
int fromSourcePageIndex = GetPageChoice("Select the start source page to add",sourcePageCount);
// Get the user's choice about the last page to add!
int toSourcePageIndex = GetPageChoice("Select the end source page to add",sourcePageCount) + 1;
// Get the user's choice about where to insert the source pages!
int targetPageIndex = GetPageChoice("Select the position where to insert the source pages",pageCount+1);
new PageManager(document).Add(
targetPageIndex,
sourcePages.GetSlice(
fromSourcePageIndex,
toSourcePageIndex
)
);
}
break;
case 2: // Page movement.
{
// Get the user's choice about the first page to move!
int fromSourcePageIndex = GetPageChoice("Select the start page to move",pageCount);
// Get the user's choice about the last page to add!
int toSourcePageIndex = GetPageChoice("Select the end page to move",pageCount) + 1;
// Get the user's choice about where to move the pages!
int targetPageIndex = GetPageChoice("Select the position where to insert the pages",pageCount+1);
new PageManager(document).Move(
fromSourcePageIndex,
toSourcePageIndex,
targetPageIndex
);
}
break;
case 3: // Page extraction.
{
// Get the user's choice about the first page to extract!
int fromPageIndex = GetPageChoice("Select the start page",pageCount);
// Get the user's choice about the last page to extract!
int toPageIndex = GetPageChoice("Select the end page",pageCount) + 1;
document = new PageManager(document).Extract(
fromPageIndex,
toPageIndex
);
file = document.File;
}
break;
}
// Serialization.
loader.Serialize(file,this.GetType().Name);
break;
}
else // Single page.
{
Console.WriteLine("\nSorry, the document you selected (" + filePath + ") has just a single page: try another document, please!");
}
}
}
#endregion
#endregion
#endregion
}
}
|