/* * $Id: PDFPage.java,v 1.5 2007/09/22 12:58:40 gil1 Exp $ * * $Date: 2007/09/22 12:58:40 $ * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package gnu.jpdf; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.print.PageFormat; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.io.Serializable; import java.util.Vector; /** *
* This class defines a single page within a document. It is linked to a single * PDFGraphics object
* * @author Peter T Mount * @author Eric Z. Beard, ericzbeard@hotmail.com * @author Gilbert DeLeeuw, gil1@users.sourceforge.net * @version $Revision: 1.5 $, $Date: 2007/09/22 12:58:40 $ * * */ public class PDFPage extends PDFObject implements Serializable { /* * NOTE: The original class is the work of Peter T. Mount, who released it * in the uk.org.retep.pdf package. It was modified by Eric Z. Beard as * follows: * The package name was changed to gnu.pdf. * The formatting was changed a little bit. * It is still licensed under the LGPL. */ /** * Default page format (Letter size with 1 inch margins and Portrait * orientation) */ private static final PageFormat DEF_FORMAT = new PageFormat(); /** * This is this page format, ie the size of the page, margins, and rotation */ protected PageFormat pageFormat; /** * This is the pages object id that this page belongs to. It is set by the * pages object when it is added to it. */ protected PDFObject pdfPageList; /** * This holds the contents of the page. */ protected Vector* Once created, it is added to the document via the PDF.add() method. (For * Advanced use, via the PDFPages.add() method). * *
* This defaults to a4 media.
*/
public PDFPage() {
super("/Page");
pageFormat = DEF_FORMAT;
contents = new Vector
* Normally, this should be done when the page is created, to avoid
* problems.
*
* @param orientation a PageFormat orientation constant:
* PageFormat.PORTRAIT, PageFormat.LANDSACPE or PageFromat.REVERSE_LANDSACPE
*/
public void setOrientation(int orientation) {
pageFormat.setOrientation(orientation);
}
/**
* Returns the pages orientation: PageFormat.PORTRAIT, PageFormat.LANDSACPE
* or PageFromat.REVERSE_LANDSACPE
*
* @see java.awt.print.PageFormat
* @return current orientation of the page
*/
public int getOrientation() {
return pageFormat.getOrientation();
}
/**
* This adds an object that describes some content to this page.
*
*
* Note: Objects that describe contents must be added using this
* method _AFTER_ the PDF.add() method has been called.
*
* @param ob PDFObject describing some contents
*/
public void add(PDFObject ob) {
contents.addElement(ob);
}
/**
* This adds an Annotation to the page.
*
*
* As with other objects, the annotation must be added to the pdf document
* using PDF.add() before adding to the page.
*
* @param ob Annotation to add.
*/
public void addAnnotation(PDFObject ob) {
annotations.addElement(ob);
}
/**
* This method adds a text note to the document.
*
* @param note Text of the note
* @param x Coordinate of note
* @param y Coordinate of note
* @param w Width of the note
* @param h Height of the note
* @return Returns the annotation, so other settings can be changed.
*/
public PDFAnnot addNote(String note, int x, int y, int w, int h) {
int xy1[] = cxy(x, y + h);
int xy2[] = cxy(x + w, y);
PDFAnnot ob = new PDFAnnot(xy1[0], xy1[1],
xy2[0], xy2[1],
note);
pdfDocument.add(ob);
annotations.addElement(ob);
return ob;
}
/**
* Adds a hyperlink to the document.
*
* @param x Coordinate of active area
* @param y Coordinate of active area
* @param w Width of the active area
* @param h Height of the active area
* @param dest Page that will be displayed when the link is activated. When
* displayed, the zoom factor will be changed to fit the display.
* @return Returns the annotation, so other settings can be changed.
*/
public PDFAnnot addLink(int x, int y, int w, int h, PDFObject dest) {
int xy1[] = cxy(x, y + h);
int xy2[] = cxy(x + w, y);
PDFAnnot ob = new PDFAnnot(xy1[0], xy1[1],
xy2[0], xy2[1],
dest
);
pdfDocument.add(ob);
annotations.addElement(ob);
return ob;
}
/**
* Adds a hyperlink to the document.
*
* @param x Coordinate of active area
* @param y Coordinate of active area
* @param w Width of the active area
* @param h Height of the active area
* @param dest Page that will be displayed when the link is activated
* @param vx Coordinate of view area
* @param vy Coordinate of view area
* @param vw Width of the view area
* @param vh Height of the view area
* @return Returns the annotation, so other settings can be changed.
*/
public PDFAnnot addLink(int x, int y, int w, int h,
PDFObject dest,
int vx, int vy, int vw, int vh) {
int xy1[] = cxy(x, y + h);
int xy2[] = cxy(x + w, y);
int xy3[] = cxy(vx, vy + vh);
int xy4[] = cxy(vx + vw, vy);
PDFAnnot ob = new PDFAnnot(xy1[0], xy1[1],
xy2[0], xy2[1],
dest,
xy3[0], xy3[1],
xy4[0], xy4[1]
);
pdfDocument.add(ob);
annotations.addElement(ob);
return ob;
}
/**
* Contains the text strings for the xobjects.
*/
private Vector
* Note: The object must already exist in the PDF, as only the object
* ID is stored.
*
* @param thumbnail PDFObject containing the thumbnail
*/
public void setThumbnail(PDFObject thumbnail) {
this.thumbnail = thumbnail;
}
/**
* This method attaches an outline to the current page being generated. When
* selected, the outline displays the top of the page.
*
* @param title Outline title to attach
* @return PDFOutline object created, for addSubOutline if required.
*/
public PDFOutline addOutline(String title) {
PDFOutline outline = new PDFOutline(title, this);
pdfDocument.add(outline);
pdfDocument.getOutline().add(outline);
return outline;
}
/**
* This method attaches an outline to the current page being generated. When
* selected, the outline displays the top of the page.
*
*
* Note: If the outline is not in the top level (ie below another outline)
* then it must not be passed to this method.
*
* @param title Outline title to attach
* @param x Left coordinate of region
* @param y Bottom coordinate of region
* @param w Width of region
* @param h Height coordinate of region
* @return PDFOutline object created, for addSubOutline if required.
*/
public PDFOutline addOutline(String title, int x, int y, int w, int h) {
int xy1[] = cxy(x, y + h);
int xy2[] = cxy(x + w, y);
PDFOutline outline = new PDFOutline(title, this,
xy1[0], xy1[1],
xy2[0], xy2[1]);
pdfDocument.add(outline);
pdfDocument.getOutline().add(outline);
return outline;
}
/**
* @param os OutputStream to send the object to
* @exception IOException on error
*/
@Override
public void write(OutputStream os) throws IOException {
// Write the object header
writeStart(os);
// now the objects body
// the /Parent pages object
os.write("/Parent ".getBytes("UTF-8"));
os.write(pdfPageList.toString().getBytes("UTF-8"));
os.write("\n".getBytes("UTF-8"));
// the /MediaBox for the page size
os.write("/MediaBox [".getBytes("UTF-8"));
os.write(Integer.toString(0).getBytes("UTF-8"));
os.write(" ".getBytes("UTF-8"));
os.write(Integer.toString(0).getBytes("UTF-8"));
os.write(" ".getBytes("UTF-8"));
os.write(Integer.toString((int) pageFormat.getWidth()).getBytes("UTF-8"));
os.write(" ".getBytes("UTF-8"));
os.write(Integer.toString((int) pageFormat.getHeight()).getBytes("UTF-8"));
os.write("]\n".getBytes("UTF-8"));
// Rotation (if not zero)
// if(rotate!=0) {
// os.write("/Rotate ".getBytes("UTF-8"));
// os.write(Integer.toString(rotate).getBytes("UTF-8"));
// os.write("\n".getBytes("UTF-8"));
// }
writeResources(os);
// The thumbnail
if (thumbnail != null) {
os.write("/Thumb ".getBytes("UTF-8"));
os.write(thumbnail.toString().getBytes("UTF-8"));
os.write("\n".getBytes("UTF-8"));
}
// the /Contents pages object
if (contents.size() > 0) {
if (contents.size() == 1) {
PDFObject ob = (PDFObject) contents.elementAt(0);
os.write("/Contents ".getBytes("UTF-8"));
os.write(ob.toString().getBytes("UTF-8"));
os.write("\n".getBytes("UTF-8"));
} else {
os.write("/Contents [".getBytes("UTF-8"));
os.write(PDFObject.toArray(contents).getBytes("UTF-8"));
os.write("\n".getBytes("UTF-8"));
}
}
// The /Annots object
if (annotations.size() > 0) {
os.write("/Annots ".getBytes("UTF-8"));
os.write(PDFObject.toArray(annotations).getBytes("UTF-8"));
os.write("\n".getBytes("UTF-8"));
}
// finish off with its footer
writeEnd(os);
}
public void writeResources(OutputStream os) throws IOException {
// Now the resources
os.write("/Resources << ".getBytes("UTF-8"));
// fonts
if (fonts.size() > 0) {
//os.write("/Font << ".getBytes("UTF-8"));
os.write("\n/Font << ".getBytes("UTF-8"));
for (PDFFont font : fonts) {
os.write(font.getName().getBytes("UTF-8"));
os.write(" ".getBytes("UTF-8"));
os.write(font.toString().getBytes("UTF-8"));
os.write(" ".getBytes("UTF-8"));
}
os.write(">> ".getBytes("UTF-8"));
}
// Now the XObjects
if (xobjects.size() > 0) {
os.write("\n/XObject << ".getBytes("UTF-8"));
for (String str : xobjects) {
os.write(str.getBytes("UTF-8"));
os.write(" ".getBytes("UTF-8"));
}
os.write(">> ".getBytes("UTF-8"));
}
// Any other resources
for (String str : resources) {
os.write(str.getBytes("UTF-8"));
os.write(" ".getBytes("UTF-8"));
}
// JM
if (imageResources.size() > 0) {
os.write("/XObject << ".getBytes("UTF-8"));
for (String str : imageResources) {
os.write(str.getBytes("UTF-8"));
os.write(" ".getBytes("UTF-8"));
}
os.write(" >> ".getBytes("UTF-8"));
}
if (patternResources.size() > 0) {
os.write("/Pattern << ".getBytes("UTF-8"));
for (String str : patternResources) {
os.write(str.getBytes("UTF-8"));
os.write(" ".getBytes("UTF-8"));
}
os.write(" >> ".getBytes("UTF-8"));
}
if (shadingResources.size() > 0) {
os.write("/Shading << ".getBytes("UTF-8"));
for (String str : shadingResources) {
os.write(str.getBytes("UTF-8"));
os.write(" ".getBytes("UTF-8"));
}
os.write(" >> ".getBytes("UTF-8"));
}
if (extGStateResources.size() > 0) {
os.write("/ExtGState << ".getBytes("UTF-8"));
for (String str : extGStateResources) {
os.write(str.getBytes("UTF-8"));
os.write(" ".getBytes("UTF-8"));
}
os.write(" >> ".getBytes("UTF-8"));
}
os.write(">>\n".getBytes("UTF-8"));
}
/**
* This creates a procset and sets up the page to reference it
*/
private void addProcset() {
if (procset == null) {
pdfDocument.add(procset = new procset());
resources.addElement("/ProcSet " + procset);
}
}
/**
* This defines a procset
*/
public class procset extends PDFObject {
private Vector