PageManagementSample.cs :  » PDF » PDF-Clown » it » stefanochizzolini » clown » samples » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » PDF Clown 
PDF Clown » it » stefanochizzolini » clown » samples » PageManagementSample.cs
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
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.