voyent
PageTree.getResources() returning null  XML
Forum Index -> General Help
Author Message
dwhytock

Joined: 01/Mar/2017 10:53:49
Messages: 3
Offline


Hi...I'm having font-loading issues, so I'm trying to find out what fonts are included in PDFs my Java program is sending to the printer. My understanding is that this would be Document.getPageTree().getResources.getFonts(), but the getResources() call is returning null.

Code:

Document pdf = new Document();

Exception exception = null;
try {
// load the file specified by the command line
pdf.setFile(filePath);

System.out.println("Number of pages: " + pdf.getNumberOfPages());
PageTree pagetree = pdf.getPageTree();
System.out.println("PageTree: number of pages: " + pagetree.getNumberOfPages());
Page page = pagetree.getPage(1);
if (page == null)
{
System.out.println("Page 1 came back null.");
return false;
}

Resources resources = pagetree.getResources();
if (resources == null)
{
System.out.println("Resources came back null.");
return false;
}
HashMap fonts = resources.getFonts();

Output:

Number of pages: 3
PageTree: number of pages: 3
Resources came back null.

I also tried pagetree.getPage(1).getResources, but that also came back null.

File attached.

Thanks...

Don
 Filename test3.pdf [Disk] Download
 Description PDF created by MS Word.
 Filesize 84 Kbytes
 Downloaded:  1193 time(s)

 Filename test3.doc [Disk] Download
 Description File the PDF was created from.
 Filesize 22 Kbytes
 Downloaded:  1126 time(s)

patrick.corless

Joined: 26/Oct/2004 00:00:00
Messages: 1982
Offline


Your close, this particular case the pageTree doesn't haven any resources. Generally the Page object will contain all the resources for the page. In theory common resources could be attached to the pageTree but encoders never seem to use this optimization.

Take a look at the class org.icepdf.ri.common.fonts.FindFontsTask in the ./viewer source. http://sventon.icesoft.org/svn/repos/repo/show/icepdf/trunk/icepdf/viewer/viewer-awt/src/main/java/org/icepdf/ri/common/fonts/FindFontsTask.java. This class is issed by the viewer RI to build a tree of the document fonts. You can also access this dialog through the viewer RI's file menu -> document fonts.
[Email]
dwhytock

Joined: 01/Mar/2017 10:53:49
Messages: 3
Offline


Okay, thanks, that did help with seeing the fonts in that particular file. (It comes up as Calibri.) Trying it with three other files (which unfortunately I'm not allowed to show you) which were produced with iText comes up with two null font lists and one page 1 not found. The last one was a PDF containing nothing but an image, but the other two have fonts in them that I can see in Adobe Reader using right-click->DocumentProperties->Fonts.

BTW, the link I wound up using was

http://sventon.icesoft.org/svn/repos/repo/show/icepdf/trunk/icepdf/viewer/viewer-awt/src/main/java/org/icepdf/ri/common/fonts/FindFontsTask.java?revision=HEAD

Code:

pdf.setFile(filePath);

Library library = pdf.getCatalog().getLibrary();
System.out.println("Number of pages: " + pdf.getNumberOfPages());
PageTree pagetree = pdf.getPageTree();
System.out.println("PageTree: number of pages: " + pagetree.getNumberOfPages());
Page page = pagetree.getPage(1);
if (page == null)
{
System.out.println("Page 1 came back null.");
return false;
}

page.initPageResources();
Resources resources = page.getResources();
if (resources == null)
{
System.out.println("Resources came back null.");
return false;
}
HashMap fonts = resources.getFonts();
if (fonts != null && fonts.size() > 0)
{
System.out.println("number of fonts: " + fonts.size());
Set fontKeys = fonts.keySet();
for (Object key: fontKeys)
{
System.out.println("key to string:" + key.toString());
Object fontObject = fonts.get(key);
System.out.println("font object to string: " + fontObject.toString());
if (fontObject instanceof Reference)
{
Reference fontReference = (Reference) fontObject;
fontObject = library.getObject(fontReference);
if (fontObject instanceof org.icepdf.core.pobjects.fonts.Font)
{
Font font = (Font) fontObject;
font.init();
System.out.println("font name: " + font.getName());
System.out.println("base font name: " + font.getBaseFont());
}
}
}
return false;
}
else
System.out.println("Fonts are null.");

Output:

Number of pages: 7
PageTree: number of pages: 7
Fonts are null.
dwhytock

Joined: 01/Mar/2017 10:53:49
Messages: 3
Offline


I produced a test file. Using the code in my previous post, the results were:

Number of pages: 1
PageTree: number of pages: 1
Page 1 came back null.
 Filename atlastest.pdf [Disk] Download
 Description
 Filesize 84 Kbytes
 Downloaded:  1161 time(s)

 
Forum Index -> General Help
Go to:   
Powered by JForum 2.1.7ice © JForum Team