java.lang.Object
org.jfree.pdf.PDFDocument
Represents a PDF document. The focus of this implementation is to
allow the use of the
PDFGraphics2D
class 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
FieldsModifier and TypeFieldDescriptionprivate String
The author of the document (can be null).private DictionaryObject
The document catalog.private boolean
A flag that is used to indicate that we are in DEBUG mode.private DictionaryObject
Document info.private static final Logger
private int
The next PDF object number in the document.A list of other objects added to the document.private DictionaryObject
The outlines (placeholder, outline support is not implemented).private Pages
The pages of the document.private static final String
Producer string.private String
The document title (can be null). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) void
Adds an object to the document.createPage
(Rectangle2D bounds) Creates a newPage
, adds it to the document, and returns a reference to thePage
.Returns the author for the document.int
Returns a new PDF object number and increments the internal counter for the next PDF object number.byte[]
Returns a byte array containing the encoding of this PDF document.getTitle()
Returns the title for the document.boolean
Returns the debug mode flag that controls whether or not the output stream is filtered.void
Sets the author for the document.void
setDebugMode
(boolean debug) Sets the debug MODE flag (this needs to be set before any call tocreatePage(java.awt.geom.Rectangle2D)
).void
Sets the title for the document.private byte[]
A utility method to convert a string to US-ASCII byte format.void
writeToFile
(File f) Writes the PDF document to a file.
-
Field Details
-
LOGGER
-
PRODUCER
Producer string.- See Also:
-
catalog
The document catalog. -
outlines
The outlines (placeholder, outline support is not implemented). -
info
Document info. -
title
The document title (can be null). -
author
The author of the document (can be null). -
pages
The pages of the document. -
otherObjects
A list of other objects added to the document. -
nextNumber
private int nextNumberThe next PDF object number in the document. -
debug
private boolean debugA 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.
-
-
Constructor Details
-
PDFDocument
public PDFDocument()Creates a newPDFDocument
, initially with no content.
-
-
Method Details
-
getTitle
Returns the title for the document. The default value isnull
.- Returns:
- The title for the document (possibly
null
).
-
setTitle
Sets the title for the document.- Parameters:
title
- the title (null
permitted).
-
getAuthor
Returns the author for the document. The default value isnull
.- Returns:
- The author for the document (possibly
null
).
-
setAuthor
Sets the author for the document.- Parameters:
author
- the author (null
permitted).
-
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
Creates a newPage
, adds it to the document, and returns a reference to thePage
.- Parameters:
bounds
- the page bounds (null
not permitted).- Returns:
- The new page.
-
addObject
Adds an object to the document.- Parameters:
object
- the object (null
not 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
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
A utility method to convert a string to US-ASCII byte format.- Parameters:
s
- the string.- Returns:
- The corresponding byte array.
-