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;
}
}
}
|