package org.apache.fop.pdf;
+import java.awt.geom.Rectangle2D;
+
/**
* Class representing a /Page object.
* <p>
*/
public class PDFPage extends PDFResourceContext {
- /**
- * Holds a reference on the parent PDFPages object.
- */
- private String parentRef;
-
- /**
- * the contents stream
- */
- protected PDFStream contents;
-
- /**
- * the width of the page in points
- */
- protected int pagewidth;
-
- /**
- * the height of the page in points
- */
- protected int pageheight;
-
/** the page index (zero-based) */
protected int pageIndex;
- /**
- * Duration to display page
- */
- protected int duration = -1;
-
- /**
- * Transition dictionary
- */
- protected TransitionDictionary trDictionary = null;
-
/**
* Create a /Page object
*
/* generic creation of object */
super(resources);
+ put("Type", new PDFName("Page"));
/* set fields using parameters */
- this.contents = contents;
- this.pagewidth = pageWidth;
- this.pageheight = pageHeight;
+ setContents(contents);
+ setSimplePageSize(pageWidth, pageHeight);
this.pageIndex = pageIndex;
}
this(resources, null, pageWidth, pageHeight, pageIndex);
}
+ private void setSimplePageSize(int width, int height) {
+ Rectangle2D box = new Rectangle2D.Double(0, 0, width, height);
+ setMediaBox(box);
+ setBleedBox(box); //Recommended by PDF/X
+ setTrimBox(box); //Needed for PDF/X
+ }
+
+ private PDFArray toPDFArray(Rectangle2D box) {
+ return new PDFArray(this, new double[] {
+ box.getX(), box.getY(), box.getMaxX(), box.getMaxY()});
+ }
+
+ /**
+ * Sets the "MediaBox" entry
+ * @param box the media rectangle
+ */
+ public void setMediaBox(Rectangle2D box) {
+ put("MediaBox", toPDFArray(box));
+ }
+
+ /**
+ * Sets the "TrimBox" entry
+ * @param box the trim rectangle
+ */
+ public void setTrimBox(Rectangle2D box) {
+ put("TrimBox", toPDFArray(box));
+ }
+
+ /**
+ * Sets the "BleedBox" entry
+ * @param box the bleed rectangle
+ */
+ public void setBleedBox(Rectangle2D box) {
+ put("BleedBox", toPDFArray(box));
+ }
+
/**
* set this page contents
*
* @param contents the contents of the page
*/
public void setContents(PDFStream contents) {
- this.contents = contents;
+ if (contents != null) {
+ put("Contents", new PDFReference(contents));
+ }
}
/**
* @param parent the /Pages object that is this page's parent
*/
public void setParent(PDFPages parent) {
- this.parentRef = parent.referencePDF();
+ put("Parent", new PDFReference(parent));
}
/**
* @param tr the transition dictionary
*/
public void setTransition(int dur, TransitionDictionary tr) {
- duration = dur;
- trDictionary = tr;
- }
-
- /**
- * Returns the page width.
- * @return the page width
- */
- public int getWidth() {
- return this.pagewidth;
- }
-
- /**
- * Returns the page height.
- * @return the page height
- */
- public int getHeight() {
- return this.pageheight;
+ put("Dur", new Integer(dur));
+ put("Trans", tr);
}
/**
public int getPageIndex() {
return this.pageIndex;
}
-
- /**
- * {@inheritDoc}
- */
- public String toPDFString() {
- StringBuffer sb = new StringBuffer();
-
- String box = "[ 0 0 " + getWidth() + " " + getHeight() + " ]";
- sb = sb.append(getObjectID()
- + "<< /Type /Page\n"
- + "/Parent " + this.parentRef + "\n"
- + "/MediaBox " + box + "\n"
- + "/TrimBox " + box + "\n" //Needed for PDF/X
- + "/BleedBox " + box + "\n" //Recommended by PDF/X
- + "/Resources " + this.resources.referencePDF() + "\n"
- + "/Contents " + this.contents.referencePDF() + "\n");
- if (this.annotList != null) {
- sb = sb.append("/Annots " + this.annotList.referencePDF() + "\n");
- }
- if (this.duration != -1) {
- sb = sb.append("/Dur " + this.duration + "\n");
- }
- if (this.trDictionary != null) {
- sb = sb.append("/Trans << " + this.trDictionary.getDictionary() + " >>\n");
- }
-
- sb = sb.append(">>\nendobj\n");
- return sb.toString();
- }
}
* to the memory profile this was causing OOM issues. So, we store
* only the object ID of the parent, rather than the parent itself.
*/
-public class PDFResourceContext extends PDFObject {
-
- /**
- * the page's /Resource object
- */
- protected PDFResources resources;
-
- /**
- * the list of annotation objects for this page
- */
- protected PDFAnnotList annotList;
+public class PDFResourceContext extends PDFDictionary {
/**
* Creates a new ResourceContext.
super();
/* set fields using parameters */
- //this.document = doc;
- this.resources = resources;
- this.annotList = null;
+ put("Resources", resources);
}
/**
* @return the resources in this resource context
*/
public PDFResources getPDFResources() {
- return this.resources;
+ return (PDFResources)get("Resources");
}
/**
* @param annot a PDFAnnotList list of annotations
*/
public void addAnnotation(PDFObject annot) {
- if (this.annotList == null) {
- this.annotList = getDocument().getFactory().makeAnnotList();
+ PDFAnnotList annotList = getAnnotations();
+ if (annotList == null) {
+ annotList = getDocument().getFactory().makeAnnotList();
+ put("Annots", annotList);
}
- this.annotList.addAnnot(annot);
+ annotList.addAnnot(annot);
}
/**
* @return the current annotation list
*/
public PDFAnnotList getAnnotations() {
- return this.annotList;
+ return (PDFAnnotList)get("Annots");
}
/**
package org.apache.fop.pdf;
-import java.util.Map;
-import java.util.Iterator;
-
/**
* Transition Dictionary
* This class is used to build a transition dictionary to
* specify the transition between pages.
*/
-public class TransitionDictionary extends PDFObject {
-
- private Map dictionaryValues;
+public class TransitionDictionary extends PDFDictionary {
/**
* Create a Transition Dictionary
*
* @param values the dictionary values to output
*/
- public TransitionDictionary(Map values) {
- dictionaryValues = values;
+ public TransitionDictionary() {
+ put("Type", new PDFName("Trans"));
}
- /**
- * Get the dictionary.
- * This returns the string containing the dictionary values.
- *
- * @return the string with the dictionary values
- */
- public String getDictionary() {
- StringBuffer sb = new StringBuffer();
- sb.append("/Type /Trans\n");
- for (Iterator iter = dictionaryValues.keySet().iterator(); iter.hasNext();) {
- Object key = iter.next();
- sb.append(key + " " + dictionaryValues.get(key) + "\n");
- }
- return sb.toString();
- }
-
- /**
- * there is nothing to return for the toPDF method, as it should not be called
- *
- * @return an empty string
- */
- public byte[] toPDF() {
- return new byte[0];
- }
}