ParsingSample.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 » ParsingSample.cs
using it.stefanochizzolini.clown.documents;
using it.stefanochizzolini.clown.documents.contents;
using it.stefanochizzolini.clown.documents.contents.objects;
using it.stefanochizzolini.clown.documents.interchange.metadata;
using it.stefanochizzolini.clown.files;
using it.stefanochizzolini.clown.objects;

using System;
using System.Collections.Generic;

namespace it.stefanochizzolini.clown.samples{
  /**
    <summary>This sample demonstrates how to inspect the structure of a PDF
    document.</summary>
    <remarks>
      <para>This implementation is just a limited exercise: see the API documentation
      to perform all the possible access functionalities.</para>
    </remarks>
  */
  public class ParsingSample
    : ISample
  {
    public void Run(
      SampleLoader loader
      )
    {
      // (boilerplate user choice -- ignore it)
      string filePath = loader.GetPdfFileChoice("Please select a PDF file");

      // 1. Open the PDF file!
      File file = new File(filePath);

      // 2. Parsing the document...
      // Get the PDF document!
      Document document = file.Document;
      // 2.1. Showing basic metadata...
      Console.WriteLine("\nDocument information:");
      Information info = document.Information;
      if(info == null)
      {Console.WriteLine("No information available (Info dictionary doesn't exist).");}
      else
      {
        Console.WriteLine("Author: " + info.Author);
        Console.WriteLine("Title: " + info.Title);
        Console.WriteLine("Subject: " + info.Subject);
        Console.WriteLine("CreationDate: " + info.CreationDate);
      }

      Console.WriteLine("\nIterating through the indirect-object collection (please wait)...");

      // 2.2. Counting the indirect objects, grouping them by type...
      Dictionary<string,int> objCounters = new Dictionary<string,int>();
      objCounters["xref free entry"] = 0;
      foreach(PdfIndirectObject obj in file.IndirectObjects)
      {
        if(obj.IsInUse()) // In-use entry.
        {
          string typeName = obj.DataObject.GetType().Name;
          if(objCounters.ContainsKey(typeName))
          {objCounters[typeName]++;}
          else
          {objCounters[typeName] = 1;}
        }
        else // Free entry.
        {objCounters["xref free entry"]++;}
      }
      Console.WriteLine("\nIndirect objects partial counts (grouped by PDF object type):");
      foreach(KeyValuePair<string,int> keyValuePair in objCounters)
      {Console.WriteLine(" " + keyValuePair.Key + ": " + keyValuePair.Value);}
      Console.WriteLine("Indirect objects total count: " + file.IndirectObjects.Count);

      // 2.3. Showing some page information...
      Pages pages = document.Pages;
      int pageCount = pages.Count;
      Console.WriteLine("\nPage count: " + pageCount);
      int pageIndex = (int)Math.Round((float)pageCount / 2);
      Console.WriteLine("Mid page:");
      PrintPageInfo(pages[pageIndex],pageIndex);

      pageIndex++;
      if(pageIndex < pageCount)
      {
        Console.WriteLine("Next page:");
        PrintPageInfo(pages[pageIndex],pageIndex);
      }

      file.Dispose();
    }

    private void PrintPageInfo(
      Page page,
      int index
      )
    {
      // 1. Showing basic page information...
      Console.WriteLine(" Index (calculated): " + page.Index + " (should be " + index + ")");
      Console.WriteLine(" ID: " + ((PdfReference)page.BaseObject).ID);
      PdfDictionary pageDictionary = page.BaseDataObject;
      Console.WriteLine(" Dictionary entries:");
      foreach(PdfName key in pageDictionary.Keys)
      {Console.WriteLine("  " + key.Value);}

      // 2. Showing page contents information...
      Contents contents = page.Contents;
      Console.WriteLine(" Content objects count: " + contents.Count);
      Console.WriteLine(" Content head (operations):");
      {
        int i = 0,
          count = contents.Count;
        while(i < 10
          && i < count)
        {i = PrintContentObject(contents[i],i,0);}
      }

      // 3. Showing page resources information...
      {
        Resources resources = page.Resources;
        Console.WriteLine(" Resources:");
        try{Console.WriteLine("  Font count: " + resources.Fonts.Count);}catch{}
        try{Console.WriteLine("  XObjects count: " + resources.XObjects.Count);}catch{}
        try{Console.WriteLine("  ColorSpaces count: " + resources.ColorSpaces.Count);}catch{}
      }
    }

    private int PrintContentObject(
      ContentObject content,
      int index,
      int level
      )
    {
      string indentation = new String(' ',level);

      /*
        NOTE: Contents are expressed through both simple operations and composite objects.
      */
      if(content is Operation)
      {Console.WriteLine("   " + indentation + (++index) + ": " + content.ToString());}
      else if(content is ContainerObject)
      {
        Console.WriteLine(
          "   " + indentation + content.GetType().Name
            + "\n   " + indentation + "{"
          );
        foreach(ContentObject obj in ((ContainerObject)content).Objects)
        {if((index = PrintContentObject(obj,index,level+1)) > 9) break;}
        Console.WriteLine("   " + indentation + "}");
      }
      else if(content is GraphicsObject)
      {
        Console.WriteLine(
          "   " + indentation + content.GetType().Name
            + "\n   " + indentation + "{"
          );
        foreach(Operation obj in ((GraphicsObject)content).Objects)
        {if((index = PrintContentObject(obj,index,level+1)) > 9) break;}
        Console.WriteLine("   " + indentation + "}");
      }
      return index;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.