/*
* $Id: PageTreeNode.java 49 2007-05-19 19:24:42Z chammer $
* Copyright (c) 2005-2007 Carsten Hammer, Bruno Lowagie
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* This class was originally published under the MPL by Carsten Hammer.
* It was a part of iText, a Java-PDF library. You can now use it under
* the MIT License; for backward compatibility you can also use it under
* the MPL version 1.1: http://www.mozilla.org/MPL/
* A copy of the MPL license is bundled with the source code FYI.
*/
package com.lowagie.tools.swing.treenodes;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.tree.DefaultMutableTreeNode;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PRStream;
import com.lowagie.text.pdf.PdfArray;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfNumber;
import com.lowagie.text.pdf.PdfObject;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.tools.swing.interfaces.RecursivePdfObjectIterator;
import com.lowagie.tools.swing.interfaces.UpdateNodeView;
public class PageTreeNode extends DictionaryTreeNode {
/** A serial version id. */
private static final long serialVersionUID = -3463874220927052895L;
/** the page number. */
private int pagenumber;
/** the page width. */
private float width;
/** the page height. */
private float height;
/**
* Constructs a page tree node.
* @param page the page dictionary
* @param pagenumber the page number
* @param pageanalyzer the page analysis class
* @param pdfreader the reader instance
*/
public PageTreeNode(com.lowagie.text.pdf.PdfDictionary page,
int pagenumber, RecursivePdfObjectIterator pageanalyzer, PdfReader pdfreader) {
super(page);
this.pagenumber = pagenumber;
DefaultMutableTreeNode info;
PdfArray arr = (PdfArray) page.get(PdfName.MEDIABOX);
float curwidth = 0;
float curheight = 0;
if (arr != null) {
ArrayList arl = arr.getArrayList();
curwidth = Float.parseFloat(arl.get(2).toString());
curheight = Float.parseFloat(arl.get(3).toString());
info = new ObjectTreeNode(PdfName.MEDIABOX + " " + curwidth
+ "*" + curheight);
this.add(info);
}
PdfArray arrcrop = (PdfArray) page.get(PdfName.CROPBOX);
if (arrcrop != null) {
ArrayList arl = arrcrop.getArrayList();
curwidth = Float.parseFloat(arl.get(2).toString());
curheight = Float.parseFloat(arl.get(3).toString());
info = new ObjectTreeNode(PdfName.CROPBOX + " " + curwidth
+ "*" + curheight);
this.add(info);
}
PdfNumber rotation = (PdfNumber) PdfReader.getPdfObject(page
.get(PdfName.ROTATE));
if (rotation == null) {
System.out.println("Rotation missing");
rotation = new PdfNumber(0);
} else {
info = new ObjectTreeNode(PdfName.ROTATE + " " + rotation);
this.add(info);
}
Rectangle rect = new Rectangle(curwidth, curheight);
if ((rotation.floatValue() == 90) || (rotation.floatValue() == 270)) {
rect = rect.rotate();
}
width = rect.width();
height = rect.height();
PdfArray dict = (PdfArray) PdfReader.getPdfObject(page.get(PdfName.ANNOTS));
if (dict != null) {
this.add(new ObjectTreeNode(PdfName.ANNOTS + " " + dict.length()));
ObjectTreeNode annots_node = new ObjectTreeNode(PdfName.ANNOTS + " " + dict.type());
this.add(annots_node);
pageanalyzer.iterateObjects(dict, pdfreader, annots_node);
}
PdfObject reso = PdfReader.getPdfObject(page.get(PdfName.RESOURCES));
if (reso != null) {
ObjectTreeNode resources_node = new ObjectTreeNode(PdfName.RESOURCES
+ " " + reso.type());
this.add(resources_node);
pageanalyzer.iterateObjects(reso, pdfreader, resources_node);
}
PdfObject contents = PdfReader.getPdfObject(page.get(PdfName.CONTENTS));
if (contents != null) {
this.add(new StreamTreeNode(contents, "Content"));
if (contents.isStream()) {
PRStream page_content = (PRStream) contents;
Set s = page_content.getKeys();
Iterator it = s.iterator();
while (it.hasNext()) {
Object obj = it.next();
System.out.println("Field:" + obj);
Object value = PdfReader.getPdfObject(page_content.get((PdfName) obj));
System.out.println("Value:" + value);
}
}
}
}
/**
*
* @see javax.swing.tree.DefaultMutableTreeNode#toString()
* @return String
*/
public String toString() {
return "Page " + pagenumber;
}
/**
* Getter for the page number
* @return the page number
*/
public int getPagenumber() {
return pagenumber;
}
/**
* Getter for the width
* @return the page width
*/
public float getWidth() {
return width;
}
/**
* Getter for the height
* @return the page height
*/
public float getHeight() {
return height;
}
/**
* updateview
*
* @param updateobject
* IUpdatenodeview
*/
public void updateview(UpdateNodeView updateobject) {
StringBuffer sb = new StringBuffer();
sb.append("<html>");
sb.append("<p>");
sb.append("Page ").append(getPagenumber());
sb.append("</p>");
sb.append("<p>");
sb.append("Size: ").append(getWidth()).append('*').append(getHeight());
sb.append("</p>");
Set set = dictionary.getKeys();
Iterator it = set.iterator();
while (it.hasNext()) {
sb.append("<p>");
sb.append("Key ").append(it.next().toString());
sb.append("</p>");
}
sb.append("</html>");
updateobject.showvalues(sb.toString());
}
/**
* Shows the icon for a single page.
*
* @return Icon
*/
public Icon getIcon() {
return new ImageIcon(PageTreeNode.class
.getResource("page.gif"));
}
}