)
{
String filePath = promptPdfFileChoice("Please select a PDF file");
// 1. Open the PDF file!
File file;
try
{file = new File(filePath);}
catch(FileFormatException e)
{throw new RuntimeException(filePath + " file has a bad file format.",e);}
catch(Exception e)
{throw new RuntimeException(filePath + " file access error.",e);}
Document document = file.getDocument();
// 2. Document parsing.
// 2.1. Showing basic metadata...
System.out.println("\nDocument information:");
Information info = document.getInformation();
if(info == null)
{System.out.println("No information available (Info dictionary doesn't exist).");}
else
{
System.out.println("Author: " + info.getAuthor());
System.out.println("Title: " + info.getTitle());
System.out.println("Subject: " + info.getSubject());
System.out.println("CreationDate: " + info.getCreationDate());
}
System.out.println("\nIterating through the indirect-object collection (please wait)...");
// 2.2. Counting the indirect objects, grouping them by type...
HashMap<String,Integer> objCounters = new HashMap<String,Integer>();
objCounters.put("xref free entry",0);
for(PdfIndirectObject object : file.getIndirectObjects())
{
if(object.isInUse()) // In-use entry.
{
String typeName = object.getDataObject().getClass().getSimpleName();
if(objCounters.containsKey(typeName))
{objCounters.put(typeName, objCounters.get(typeName) + 1);}
else
{objCounters.put(typeName, 1);}
}
else // Free entry.
{objCounters.put("xref free entry", objCounters.get("xref free entry") + 1);}
}
System.out.println("\nIndirect objects partial counts (grouped by PDF object type):");
for(Map.Entry<String,Integer> entry : objCounters.entrySet())
{System.out.println(" " + entry.getKey() + ": " + entry.getValue());}
System.out.println("Indirect objects total count: " + file.getIndirectObjects().size());
// 2.3. Showing some page information...
Pages pages = document.getPages();
int pageCount = pages.size();
System.out.println("\nPage count: " + pageCount);