SampleLoader.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 » SampleLoader.cs
using it.stefanochizzolini.clown;
using bytesit.stefanochizzolini.clown.bytes;
using it.stefanochizzolini.clown.documents;
using it.stefanochizzolini.clown.documents.interaction;
using it.stefanochizzolini.clown.documents.interchange.metadata;
using it.stefanochizzolini.clown.documents.interaction.viewer;
using filesit.stefanochizzolini.clown.files;

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Reflection;

namespace it.stefanochizzolini.clown.samples{
  public class SampleLoader
  {
    #region static
    #region fields
    private static readonly string ClassName = (typeof(SampleLoader)).FullName;

    private static readonly string Properties_InputPath = ClassName + ".inputPath";
    private static readonly string Properties_OutputPath = ClassName + ".outputPath";
    #endregion

    #region interface
    #region public
    public static void Main(
      string[] args
      )
    {
      Console.WriteLine("\nSampleLoader running...");

      {
        Assembly pdfClownAssembly = Assembly.GetAssembly(typeof(Engine));
        Console.WriteLine("\n" + ((AssemblyTitleAttribute)pdfClownAssembly.GetCustomAttributes(typeof(AssemblyTitleAttribute),false)[0]).Title + " version " + pdfClownAssembly.GetName().Version);
      }

      SampleLoader loader = new SampleLoader(
        ConfigurationSettings.AppSettings.Get(Properties_InputPath),
        ConfigurationSettings.AppSettings.Get(Properties_OutputPath)
        );
      loader.Run();

      Console.WriteLine("\nSampleLoader finished.\n");
    }
    #endregion
    #endregion
    #endregion

    #region dynamic
    #region fields
    private string inputPath;
    private string outputPath;
    #endregion

    #region constructors
    public SampleLoader(
      string inputPath,
      string outputPath
      )
    {
      this.inputPath = inputPath;
      this.outputPath = outputPath;
    }
    #endregion

    #region interface
    #region public
    public string GetFileChoice(
      string fileExtension,
      string listDescription,
      string inputDescription
      )
    {
      Console.WriteLine("\n" + listDescription + ":");

      // Get the list of available PDF files!
      string[] filePaths = Directory.GetFiles(inputPath + "pdf" + Path.DirectorySeparatorChar,"*." + fileExtension);

      // Display files!
      for(
        int i = 0;
        i < filePaths.Length;
        i++
        )
      {Console.WriteLine("[" + i + "] {0}", System.IO.Path.GetFileName(filePaths[i]));}

      // Get the user's choice!
      Console.Write(inputDescription + ": ");
      try
      {return filePaths[Int32.Parse(Console.ReadLine())]; /* Custom choice. */}
      catch
      {return filePaths[0]; /* Default choice. */}
    }

    public string GetPdfFileChoice(
      string inputDescription
      )
    {
      return GetFileChoice(
        "pdf",
        "Available PDF files",
        inputDescription
        );
    }

    public string InputPath
    {get{return inputPath;}}

    public string OutputPath
    {get{return outputPath;}}

    public void Run(
      )
    {
      while(true)
      {
        // Get the current assembly!
        Assembly assembly = Assembly.GetExecutingAssembly();
        // Get all the types inside the current assembly!
        Type[] types = assembly.GetTypes();

        Console.WriteLine("\nAvailable samples:");
        // Instantiate the list of available samples!
        List<Type> sampleTypes = new List<Type>();
        // Picking available samples...
        foreach(Type type in types)
        {
          if(type.GetInterface("ISample") != null)
          {
            sampleTypes.Add(type);
            Console.WriteLine("[" + sampleTypes.IndexOf(type) + "] {0}", type.Name);
          }
        }
        Console.WriteLine("[Q] (Quit)");

        // Getting the user's choice...
        Type sampleType = null;
        Console.Write("Please select a sample: ");
        try
        {
          string choice = Console.ReadLine();
          if(choice.ToUpper().Equals("Q")) // Quit.
            break;

          sampleType = sampleTypes[Int32.Parse(choice)]; // Custom choice.
        }
        catch
        {
          sampleType = sampleTypes[0]; // Default choice.
        }

        Console.WriteLine("\nInstantiating " + sampleType.Name + " sample...");

        // Instantiate the sample!
        ISample sample = (ISample)Activator.CreateInstance(sampleType);

        Console.WriteLine("Running " + sampleType.Name + " sample...");

        // Run the sample!
        sample.Run(this);
      }
    }

    public void Serialize(
      files::File file,
      string outputFileName
      )
    {
      Serialize(
        file,
        outputFileName,
        true
        );
    }

    public void Serialize(
      files::File file,
      string outputFileName,
      bool choiceMode
      )
    {
      files::SerializationModeEnum serializationMode = files::SerializationModeEnum.Incremental;
      if(choiceMode)
      {
        Console.WriteLine("[0] Standard serialization");
        Console.WriteLine("[1] Incremental update");
        // Get the user's choice.
        Console.Write("Please select a serialization mode: ");
        try
        {serializationMode = (files::SerializationModeEnum)Int32.Parse(Console.ReadLine());}
        catch
        {/* Default. */}
      }

      string outputFilePath = outputPath + outputFileName + "." + serializationMode + ".pdf";

      // Save the file!
      file.WriteTo(
        outputFilePath,
        serializationMode
        );

// Alternatively, defining an appropriate target stream:
/*
      FileStream outputStream = new System.IO.FileStream(
        outputFilePath,
        System.IO.FileMode.Create,
        System.IO.FileAccess.Write
        );
      file.WriteTo(
        new bytes::Stream(outputStream),
        serializationMode
        );
      outputStream.Flush();
      outputStream.Close();
*/
      Console.WriteLine("Output: "+ outputFilePath);
    }

    public void BuildAccessories(
      Document document,
      Type creator,
      string title,
      string subject
      )
    {
      // Viewer preferences.
      ViewerPreferences view = new ViewerPreferences(document); // Instantiates viewer preferences inside the document context.
      document.ViewerPreferences = view; // Assigns the viewer preferences object to the viewer preferences function.
      view.DisplayDocTitle = true;

      // Document metadata.
      Information info = new Information(document);
      document.Information = info;
      info.Author = "Stefano Chizzolini";
      info.CreationDate = DateTime.Now;
      info.Creator = creator.AssemblyQualifiedName;
      info.Title = "PDF Clown - " + title + " sample";
      info.Subject = "Sample about " + subject + " using PDF Clown";
    }
    #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.