Browse Source

Change PDFPage to use PDFDictionary in order to make it possible to better set MediaBox/TrimBox/BleedBox.

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@618626 13f79535-47bb-0310-9956-ffa450edef68
pull/15/head
Jeremias Maerki 16 years ago
parent
commit
94996e0139

+ 14
- 0
src/java/org/apache/fop/pdf/PDFArray.java View File

@@ -66,6 +66,20 @@ public class PDFArray extends PDFObject {
}
}

/**
* Create an array object.
* @param parent the array's parent if any
* @param values the actual array wrapped by this object
*/
public PDFArray(PDFObject parent, double[] values) {
/* generic creation of PDF object */
super(parent);

for (int i = 0, c = values.length; i < c; i++) {
this.values.add(new Double(values[i]));
}
}

/**
* Create an array object.
* @param parent the array's parent if any

+ 47
- 82
src/java/org/apache/fop/pdf/PDFPage.java View File

@@ -19,6 +19,8 @@
package org.apache.fop.pdf;

import java.awt.geom.Rectangle2D;

/**
* Class representing a /Page object.
* <p>
@@ -29,39 +31,9 @@ package org.apache.fop.pdf;
*/
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
*
@@ -77,10 +49,10 @@ public class PDFPage extends PDFResourceContext {
/* 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;
}

@@ -97,13 +69,51 @@ public class PDFPage extends PDFResourceContext {
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));
}
}

/**
@@ -112,7 +122,7 @@ public class PDFPage extends PDFResourceContext {
* @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));
}

/**
@@ -124,24 +134,8 @@ public class PDFPage extends PDFResourceContext {
* @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);
}

/**
@@ -151,34 +145,5 @@ public class PDFPage extends PDFResourceContext {
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();
}

}

+ 9
- 19
src/java/org/apache/fop/pdf/PDFResourceContext.java View File

@@ -33,17 +33,7 @@ package org.apache.fop.pdf;
* 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.
@@ -54,9 +44,7 @@ public class PDFResourceContext extends PDFObject {
super();

/* set fields using parameters */
//this.document = doc;
this.resources = resources;
this.annotList = null;
put("Resources", resources);
}

/**
@@ -65,7 +53,7 @@ public class PDFResourceContext extends PDFObject {
* @return the resources in this resource context
*/
public PDFResources getPDFResources() {
return this.resources;
return (PDFResources)get("Resources");
}

/**
@@ -74,10 +62,12 @@ public class PDFResourceContext extends PDFObject {
* @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);
}

/**
@@ -86,7 +76,7 @@ public class PDFResourceContext extends PDFObject {
* @return the current annotation list
*/
public PDFAnnotList getAnnotations() {
return this.annotList;
return (PDFAnnotList)get("Annots");
}

/**

+ 3
- 32
src/java/org/apache/fop/pdf/TransitionDictionary.java View File

@@ -19,50 +19,21 @@
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];
}
}


Loading…
Cancel
Save