Package com.orsonpdf
Class PDFDocument
- java.lang.Object
-
- com.orsonpdf.PDFDocument
-
public class PDFDocument extends java.lang.ObjectRepresents a PDF document. The focus of this implementation is to allow the use of thePDFGraphics2Dclass to generate PDF content, typically in the following manner:PDFDocument pdfDoc = new PDFDocument();Page page = pdfDoc.createPage(new Rectangle(612, 468));PDFGraphics2D g2 = page.getGraphics2D();g2.setPaint(Color.RED);g2.draw(new Rectangle(10, 10, 40, 50));pdfDoc.writeToFile(new File("demo.pdf"));The implementation is light-weight and works very well alongside packages such as JFreeChart and Orson Charts.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringauthorThe author of the document (can be null).private DictionaryObjectcatalogThe document catalog.private booleandebugA flag that is used to indicate that we are in DEBUG mode.private DictionaryObjectinfoDocument info.private static java.util.logging.LoggerLOGGERprivate intnextNumberThe next PDF object number in the document.private java.util.List<PDFObject>otherObjectsA list of other objects added to the document.private DictionaryObjectoutlinesThe outlines (placeholder, outline support is not implemented).private PagespagesThe pages of the document.private static java.lang.StringPRODUCERProducer string.private java.lang.StringtitleThe document title (can be null).
-
Constructor Summary
Constructors Constructor Description PDFDocument()Creates a newPDFDocument, initially with no content.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddObject(PDFObject object)Adds an object to the document.PagecreatePage(java.awt.geom.Rectangle2D bounds)Creates a newPage, adds it to the document, and returns a reference to thePage.java.lang.StringgetAuthor()Returns the author for the document.intgetNextNumber()Returns a new PDF object number and increments the internal counter for the next PDF object number.byte[]getPDFBytes()Returns a byte array containing the encoding of this PDF document.java.lang.StringgetTitle()Returns the title for the document.booleanisDebugMode()Returns the debug mode flag that controls whether or not the output stream is filtered.voidsetAuthor(java.lang.String author)Sets the author for the document.voidsetDebugMode(boolean debug)Sets the debug MODE flag (this needs to be set before any call tocreatePage(java.awt.geom.Rectangle2D)).voidsetTitle(java.lang.String title)Sets the title for the document.private byte[]toBytes(java.lang.String s)A utility method to convert a string to US-ASCII byte format.voidwriteToFile(java.io.File f)Writes the PDF document to a file.
-
-
-
Field Detail
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
PRODUCER
private static final java.lang.String PRODUCER
Producer string.- See Also:
- Constant Field Values
-
catalog
private DictionaryObject catalog
The document catalog.
-
outlines
private DictionaryObject outlines
The outlines (placeholder, outline support is not implemented).
-
info
private DictionaryObject info
Document info.
-
title
private java.lang.String title
The document title (can be null).
-
author
private java.lang.String author
The author of the document (can be null).
-
pages
private Pages pages
The pages of the document.
-
otherObjects
private java.util.List<PDFObject> otherObjects
A list of other objects added to the document.
-
nextNumber
private int nextNumber
The next PDF object number in the document.
-
debug
private boolean debug
A flag that is used to indicate that we are in DEBUG mode. In this mode, the graphics stream for a page does not have a filter applied, so the output can be read in a text editor.
-
-
Method Detail
-
getTitle
public java.lang.String getTitle()
Returns the title for the document. The default value isnull.- Returns:
- The title for the document (possibly
null).
-
setTitle
public void setTitle(java.lang.String title)
Sets the title for the document.- Parameters:
title- the title (nullpermitted).
-
getAuthor
public java.lang.String getAuthor()
Returns the author for the document. The default value isnull.- Returns:
- The author for the document (possibly
null).
-
setAuthor
public void setAuthor(java.lang.String author)
Sets the author for the document.- Parameters:
author- the author (nullpermitted).
-
isDebugMode
public boolean isDebugMode()
Returns the debug mode flag that controls whether or not the output stream is filtered.- Returns:
- The debug flag.
- Since:
- 1.4
-
setDebugMode
public void setDebugMode(boolean debug)
Sets the debug MODE flag (this needs to be set before any call tocreatePage(java.awt.geom.Rectangle2D)).- Parameters:
debug- the new flag value.- Since:
- 1.4
-
createPage
public Page createPage(java.awt.geom.Rectangle2D bounds)
Creates a newPage, adds it to the document, and returns a reference to thePage.- Parameters:
bounds- the page bounds (nullnot permitted).- Returns:
- The new page.
-
addObject
public void addObject(PDFObject object)
Adds an object to the document.- Parameters:
object- the object (nullnot permitted).
-
getNextNumber
public int getNextNumber()
Returns a new PDF object number and increments the internal counter for the next PDF object number. This method is used to ensure that all objects in the document are assigned a unique number.- Returns:
- A new PDF object number.
-
getPDFBytes
public byte[] getPDFBytes()
Returns a byte array containing the encoding of this PDF document.- Returns:
- A byte array containing the encoding of this PDF document.
-
writeToFile
public void writeToFile(java.io.File f)
Writes the PDF document to a file. This is not a robust method, it exists mainly for the demo output.- Parameters:
f- the file.
-
toBytes
private byte[] toBytes(java.lang.String s)
A utility method to convert a string to US-ASCII byte format.- Parameters:
s- the string.- Returns:
- The corresponding byte array.
-
-