public class PDFDocument extends Object
Modifier and Type | Field and Description |
---|---|
static String |
ENCODING
the encoding to use when converting strings to PDF commands
|
protected int |
gStateObjectCount |
protected List<Long> |
indirectObjectOffsets
the character position of each object
|
protected int |
objectcount
the counter for object numbering
|
protected List<PDFObject> |
objects
the objects themselves
|
protected boolean |
outputStarted |
protected List<PDFPage> |
pageObjs |
protected long |
position
the current character position
|
protected List<PDFStructElem> |
structureTreeElements |
protected List<PDFObject> |
trailerObjects
List of objects to write in the trailer
|
Constructor and Description |
---|
PDFDocument(String prod)
Creates an empty PDF document.
|
PDFDocument(String prod,
VersionController versionController)
Creates an empty PDF document.
|
Modifier and Type | Method and Description |
---|---|
void |
addDestination(PDFDestination destination)
Adds a destination to the document.
|
PDFFormXObject |
addFormXObject(PDFResourceContext res,
PDFStream cont,
PDFReference formres,
String key)
Add a form XObject to the PDF document.
|
PDFImageXObject |
addImage(PDFResourceContext res,
PDFImage img)
Add an image to the PDF document.
|
void |
addObject(PDFObject obj)
Adds a
PDFObject to this document. |
void |
addTrailerObject(PDFObject obj)
Add trailer object.
|
void |
applyEncryption(AbstractPDFStream stream)
Apply the encryption filter to a PDFStream if encryption is enabled.
|
void |
assignObjectNumber(PDFObject obj)
|
void |
enableAccessibility(boolean enableAccessibility) |
static byte[] |
encode(String text)
Converts text to a byte array for writing to a PDF file.
|
protected PDFDestination |
findDestination(PDFDestination compare)
Finds a named destination.
|
protected PDFFileSpec |
findFileSpec(PDFFileSpec compare)
Finds a file spec.
|
protected PDFFont |
findFont(String fontname)
Finds a font.
|
protected PDFFunction |
findFunction(PDFFunction compare)
Looks through the registered functions to see if one that is equal to
a reference object exists
|
protected PDFGoTo |
findGoTo(PDFGoTo compare)
Finds a goto.
|
protected PDFGoToRemote |
findGoToRemote(PDFGoToRemote compare)
Finds a goto remote.
|
protected PDFGState |
findGState(PDFGState wanted,
PDFGState current)
Looks for an existing GState to use
|
protected PDFLaunch |
findLaunch(PDFLaunch compare)
Finds a launch.
|
protected PDFLink |
findLink(PDFLink compare)
Finds a link.
|
protected PDFPattern |
findPattern(PDFPattern compare)
Find a previous pattern.
|
protected PDFShading |
findShading(PDFShading compare)
Looks through the registered shadings to see if one that is equal to
a reference object exists
|
static void |
flushTextBuffer(StringBuilder textBuffer,
OutputStream out)
Flushes the given text buffer to an output stream with the right encoding and resets
the text buffer.
|
int |
getColorSpace()
Returns the color space.
|
List<PDFDestination> |
getDestinationList()
Gets the list of named destinations.
|
PDFEncryption |
getEncryption()
Returns the active Encryption object.
|
PDFFactory |
getFactory()
Returns the factory for PDF objects.
|
Map<String,List<String>> |
getFilterMap()
Returns the
PDFFilter s map used for filters in this document. |
Map<String,PDFFont> |
getFontMap()
Returns the font map for this document.
|
PDFImageXObject |
getImage(String key)
Deprecated.
Use getXObject instead (so forms are treated in the same way)
|
PDFInfo |
getInfo()
Get the
PDFInfo object for this document. |
int |
getObjectCount() |
PDFOutline |
getOutlineRoot()
Get the root Outlines object.
|
PDFPages |
getPages()
Returns the
PDFPages object associated with the root object. |
PDFDeviceColorSpace |
getPDFColorSpace()
Returns the PDF color space object.
|
Version |
getPDFVersion()
Returns the current PDF version.
|
String |
getPDFVersionString() |
PDFProfile |
getProfile() |
PDFResources |
getResources()
Get the /Resources object for the document
|
PDFRoot |
getRoot()
Get the
PDFRoot object for this document. |
List<PDFStructElem> |
getStructureTreeElements()
Get the Structural Tree Collection for this document
|
PDFXObject |
getXObject(String key)
Get an XObject from the image map.
|
boolean |
hasDestinations()
Gets whether the document has named destinations.
|
boolean |
isEncryptionActive()
Indicates whether encryption is active for this PDF or not.
|
boolean |
isFormXObjectEnabled() |
boolean |
isLinearizationEnabled() |
boolean |
isMergeFontsEnabled() |
boolean |
isMergeFormFieldsEnabled() |
boolean |
isStaticRegionsPerPageForAccessibility() |
PDFStructTreeRoot |
makeStructTreeRoot(PDFParentTree parentTree)
Creates and returns a StructTreeRoot object.
|
void |
output(OutputStream stream)
Writes out the entire document
|
void |
outputHeader(OutputStream stream)
Write the PDF header.
|
static int |
outputIndirectObject(PDFObject object,
OutputStream stream)
Outputs the given object, wrapped by obj/endobj, to the given stream.
|
void |
outputTrailer(OutputStream stream)
Write the trailer
|
PDFObject |
registerObject(PDFObject obj)
Registers a
PDFObject in this PDF document. |
void |
registerStructureElement(PDFStructElem structElem)
Adds the given element to the structure tree.
|
void |
registerStructureElement(PDFStructElem structElem,
StandardStructureAttributes.Table.Scope scope)
Assigns the given scope to the given element and adds it to the structure tree.
|
<T extends PDFObject> |
registerTrailerObject(T obj)
Registers a
PDFObject in this PDF document at end. |
PDFReference |
resolveExtensionReference(String id) |
void |
setColorSpace(int theColorspace)
Set the color space.
|
void |
setCreationDate(Date date)
Sets the creation date of the document.
|
void |
setCreator(String creator)
Sets the creator of the document.
|
void |
setEncryption(PDFEncryptionParams params)
Enables PDF encryption.
|
void |
setFilterMap(Map<String,List<String>> map)
Sets the filter map to use for filters in this document.
|
void |
setFormXObjectEnabled(boolean b) |
void |
setLinearizationEnabled(boolean b) |
void |
setMergeFontsEnabled(boolean mergeFontsEnabled) |
void |
setMergeFormFieldsEnabled(boolean mergeFormFieldsEnabled) |
void |
setObjectStreamsEnabled(boolean b) |
void |
setPDFVersion(Version version)
Sets the PDF version of this document.
|
void |
setProducer(String producer)
Sets the producer of the document.
|
void |
setStaticRegionsPerPageForAccessibility(boolean staticRegionsPerPageForAccessibility) |
protected int |
streamIndirectObject(PDFObject o,
OutputStream stream) |
protected void |
writeTrailer(OutputStream stream,
int first,
int last,
int size,
long mainOffset,
long startxref) |
public static final String ENCODING
protected int objectcount
protected long position
protected List<Long> indirectObjectOffsets
protected List<PDFStructElem> structureTreeElements
protected int gStateObjectCount
protected boolean outputStarted
public PDFDocument(String prod)
prod
- the name of the producer of this pdf documentpublic PDFDocument(String prod, VersionController versionController)
prod
- the name of the producer of this pdf documentversionController
- the version controller of this PDF documentpublic Version getPDFVersion()
public void setPDFVersion(Version version)
version
- the PDF versionIllegalStateException
- if the version of this PDF is not allowed to change.public String getPDFVersionString()
public PDFProfile getProfile()
public PDFFactory getFactory()
PDFFactory
objectpublic static byte[] encode(String text)
text
- text to convert/encodebyte
arraypublic static void flushTextBuffer(StringBuilder textBuffer, OutputStream out) throws IOException
textBuffer
- the text bufferout
- the output stream to flush the text content toIOException
- if an I/O error occurs while writing to the output streampublic void setProducer(String producer)
producer
- string indicating application producing the PDFpublic void setCreationDate(Date date)
date
- Date to be stored as creation date in the PDF.public void setCreator(String creator)
creator
- string indicating application creating the documentpublic void setFilterMap(Map<String,List<String>> map)
map
- the map of filter lists for each stream typepublic Map<String,List<String>> getFilterMap()
PDFFilter
s map used for filters in this document.public PDFPages getPages()
PDFPages
object associated with the root object.PDFPages
objectpublic PDFRoot getRoot()
PDFRoot
object for this document.PDFRoot
objectpublic List<PDFStructElem> getStructureTreeElements()
public PDFStructTreeRoot makeStructTreeRoot(PDFParentTree parentTree)
parentTree
- the value of the ParenTree entrypublic void registerStructureElement(PDFStructElem structElem)
public void registerStructureElement(PDFStructElem structElem, StandardStructureAttributes.Table.Scope scope)
public PDFInfo getInfo()
PDFInfo
object for this document.PDFInfo
objectpublic PDFObject registerObject(PDFObject obj)
PDFObject
in this PDF document.
The object is assigned a new object number.public <T extends PDFObject> T registerTrailerObject(T obj)
PDFObject
in this PDF document at end.
The object is assigned a new object number.public void assignObjectNumber(PDFObject obj)
obj
- PDFObject
to assign a number topublic void addObject(PDFObject obj)
PDFObject
to this document.
The object MUST have an object number assigned.obj
- PDFObject
to addpublic void addTrailerObject(PDFObject obj)
obj
- the PDF object to addpublic void applyEncryption(AbstractPDFStream stream)
stream
- PDFStream to encryptpublic void setEncryption(PDFEncryptionParams params)
params
- The encryption parameters for the pdf filepublic boolean isEncryptionActive()
public PDFEncryption getEncryption()
protected PDFFunction findFunction(PDFFunction compare)
compare
- reference objectprotected PDFShading findShading(PDFShading compare)
compare
- reference objectprotected PDFPattern findPattern(PDFPattern compare)
compare
- reference objectprotected PDFFont findFont(String fontname)
fontname
- name of the fontprotected PDFDestination findDestination(PDFDestination compare)
compare
- reference object to use as search templateprotected PDFLink findLink(PDFLink compare)
compare
- reference object to use as search templateprotected PDFFileSpec findFileSpec(PDFFileSpec compare)
compare
- reference object to use as search templateprotected PDFGoToRemote findGoToRemote(PDFGoToRemote compare)
compare
- reference object to use as search templateprotected PDFGoTo findGoTo(PDFGoTo compare)
compare
- reference object to use as search templateprotected PDFLaunch findLaunch(PDFLaunch compare)
compare
- reference object to use as search templateprotected PDFGState findGState(PDFGState wanted, PDFGState current)
wanted
- requested featurescurrent
- currently active featurespublic PDFDeviceColorSpace getPDFColorSpace()
public int getColorSpace()
public void setColorSpace(int theColorspace)
theColorspace
- the new color spacepublic Map<String,PDFFont> getFontMap()
@Deprecated public PDFImageXObject getImage(String key)
key
- the image key to look forpublic PDFXObject getXObject(String key)
key
- the XObject key to look forpublic void addDestination(PDFDestination destination)
destination
- the destination objectpublic List<PDFDestination> getDestinationList()
public boolean hasDestinations()
public PDFImageXObject addImage(PDFResourceContext res, PDFImage img)
PDFXObject
.res
- the PDF resource context to add to, may be nullimg
- the PDF image to addpublic PDFFormXObject addFormXObject(PDFResourceContext res, PDFStream cont, PDFReference formres, String key)
PDFFormXObject
.res
- the PDF resource context to add to, may be nullcont
- the PDF Stream contents of the Form XObjectformres
- a reference to the PDF Resources for the Form XObject datakey
- the key for the objectpublic PDFOutline getOutlineRoot()
public PDFResources getResources()
public void enableAccessibility(boolean enableAccessibility)
public void setStaticRegionsPerPageForAccessibility(boolean staticRegionsPerPageForAccessibility)
public boolean isStaticRegionsPerPageForAccessibility()
public PDFReference resolveExtensionReference(String id)
public void output(OutputStream stream) throws IOException
stream
- the OutputStream to output the document toIOException
- if there is an exception writing to the output streamprotected void writeTrailer(OutputStream stream, int first, int last, int size, long mainOffset, long startxref) throws IOException
IOException
protected int streamIndirectObject(PDFObject o, OutputStream stream) throws IOException
IOException
public static int outputIndirectObject(PDFObject object, OutputStream stream) throws IOException
object
- an indirect object, as described in Section 3.2.9 of the PDF 1.5
Reference.stream
- the stream to which the object must be outputIllegalArgumentException
- if the object is not an indirect objectIOException
public void outputHeader(OutputStream stream) throws IOException
stream
- the OutputStream to write the header toIOException
- if there is an exception writing to the output streampublic void outputTrailer(OutputStream stream) throws IOException
stream
- the OutputStream to write the trailer toIOException
- if there is an exception writing to the output streampublic boolean isMergeFontsEnabled()
public void setMergeFontsEnabled(boolean mergeFontsEnabled)
public boolean isMergeFormFieldsEnabled()
public void setMergeFormFieldsEnabled(boolean mergeFormFieldsEnabled)
public boolean isLinearizationEnabled()
public void setLinearizationEnabled(boolean b)
public boolean isFormXObjectEnabled()
public void setFormXObjectEnabled(boolean b)
public void setObjectStreamsEnabled(boolean b)
public int getObjectCount()
Copyright © 2025 Apache Software Foundation. All rights reserved.