This is the in-memory representation of the PDF document. You need to call
close() on this object when you are done using it!!
Method from org.apache.pdfbox.pdmodel.PDDocument Detail: |
public void addPage(PDPage page) {
PDPageNode rootPages = getDocumentCatalog().getPages();
rootPages.getKids().add( page );
page.setParent( rootPages );
rootPages.updateCount();
}
This will add a page to the document. This is a convenience method, that
will add the page to the root of the hierarchy and set the parent of the
page to the root. |
public void clearWillEncryptWhenSaving() {
//method is deprecated.
} Deprecated! Do - not rely on this method anymore. It is the responsability of
COSWriter to hold this state.
This shoule only be called by the COSWriter after encryption has completed. |
public void close() throws IOException {
document.close();
}
This will close the underlying COSDocument object. |
public void decrypt(String password) throws CryptographyException, IOException, InvalidPasswordException {
try
{
StandardDecryptionMaterial m = new StandardDecryptionMaterial(password);
this.openProtection(m);
document.dereferenceObjectStreams();
}
catch(BadSecurityHandlerException e)
{
throw new CryptographyException(e);
}
}
This will decrypt a document. This method is provided for compatibility reasons only. User should use
the new security layer instead and the openProtection method especially. |
public void encrypt(String ownerPassword,
String userPassword) throws CryptographyException, IOException {
try
{
StandardProtectionPolicy policy =
new StandardProtectionPolicy(ownerPassword, userPassword, new AccessPermission());
this.protect(policy);
}
catch(BadSecurityHandlerException e)
{
throw new CryptographyException(e);
}
}
This will mark a document to be encrypted. The actual encryption
will occur when the document is saved.
This method is provided for compatibility reasons only. User should use
the new security layer instead and the openProtection method especially. |
public AccessPermission getCurrentAccessPermission() {
if(this.securityHandler == null)
{
return AccessPermission.getOwnerAccessPermission();
}
return securityHandler.getCurrentAccessPermission();
}
Returns the access permissions granted when the document was decrypted.
If the document was not decrypted this method returns the access permission
for a document owner (ie can do everything).
The returned object is in read only mode so that permissions cannot be changed.
Methods providing access to content should rely on this object to verify if the current
user is allowed to proceed. |
public COSDocument getDocument() {
return document;
}
This will get the low level document. |
public PDDocumentCatalog getDocumentCatalog() {
if( documentCatalog == null )
{
COSDictionary trailer = document.getTrailer();
COSDictionary infoDic = (COSDictionary)trailer.getDictionaryObject( COSName.ROOT );
if( infoDic == null )
{
documentCatalog = new PDDocumentCatalog( this );
}
else
{
documentCatalog = new PDDocumentCatalog( this, infoDic );
}
}
return documentCatalog;
}
This will get the document CATALOG. This is guaranteed to not return null. |
public PDDocumentInformation getDocumentInformation() {
if( documentInformation == null )
{
COSDictionary trailer = document.getTrailer();
COSDictionary infoDic = (COSDictionary)trailer.getDictionaryObject( COSName.INFO );
if( infoDic == null )
{
infoDic = new COSDictionary();
trailer.setItem( COSName.INFO, infoDic );
}
documentInformation = new PDDocumentInformation( infoDic );
}
return documentInformation;
}
This will get the document info dictionary. This is guaranteed to not return null. |
public PDEncryptionDictionary getEncryptionDictionary() throws IOException {
if( encParameters == null )
{
if( isEncrypted() )
{
encParameters = new PDEncryptionDictionary(document.getEncryptionDictionary());
}
}
return encParameters;
}
This will get the encryption dictionary for this document. This will still
return the parameters if the document was decrypted. If the document was
never encrypted then this will return null. As the encryption architecture
in PDF documents is plugable this returns an abstract class, but the only
supported subclass at this time is a PDStandardEncryption object. |
public int getNumberOfPages() {
PDDocumentCatalog cat = getDocumentCatalog();
return (int)cat.getPages().getCount();
}
|
public String getOwnerPasswordForEncryption() {
return null;
} Deprecated! Do - not rely on this method anymore.
The owner password that was passed into the encrypt method. You should
never use this method. This will not longer be valid once encryption
has occured. |
public int getPageCount() {
return getNumberOfPages();
} Deprecated! Use - the getNumberOfPages method instead!
This will return the total page count of the PDF document. Note: This method
is deprecated in favor of the getNumberOfPages method. The getNumberOfPages is
a required interface method of the Pageable interface. This method will
be removed in a future version of PDFBox!! |
public PageFormat getPageFormat(int pageIndex) {
PDPage page = (PDPage)getDocumentCatalog().getAllPages().get( pageIndex );
Dimension mediaBox = page.findMediaBox().createDimension();
Dimension cropBox = page.findCropBox().createDimension();
double diffWidth = 0;
double diffHeight = 0;
double mediaWidth = mediaBox.getWidth();
double mediaHeight = mediaBox.getHeight();
double cropWidth = cropBox.getWidth();
double cropHeight = cropBox.getHeight();
// we have to center the ImageableArea if the cropBox is smaller than the mediaBox
if (!mediaBox.equals(cropBox))
{
diffWidth = (mediaWidth - cropWidth)/2;
diffHeight = (mediaHeight - cropHeight)/2;
}
Paper paper = new Paper();
paper.setImageableArea( diffWidth, diffHeight, cropWidth, cropHeight);
paper.setSize( mediaWidth, mediaHeight );
PageFormat format = new PageFormat();
format.setPaper( paper );
return format;
}
|
public final Map getPageMap() {
if (pageMap == null)
{
generatePageMap();
}
return pageMap;
}
This will return the Map containing the mapping from object-ids to pagenumbers. |
public Printable getPrintable(int pageIndex) {
return (Printable)getDocumentCatalog().getAllPages().get( pageIndex );
}
|
public SecurityHandler getSecurityHandler() {
return securityHandler;
}
Get the security handler that is used for document encryption. |
public String getUserPasswordForEncryption() {
return null;
} Deprecated! Do - not rely on this method anymore.
The user password that was passed into the encrypt method. You should
never use this method. This will not longer be valid once encryption
has occured. |
public PDPage importPage(PDPage page) throws IOException {
PDPage importedPage = new PDPage( new COSDictionary( page.getCOSDictionary() ) );
InputStream is = null;
OutputStream os = null;
try
{
PDStream src = page.getContents();
PDStream dest = new PDStream( new COSStream( src.getStream(), document.getScratchFile() ) );
importedPage.setContents( dest );
os = dest.createOutputStream();
byte[] buf = new byte[10240];
int amountRead = 0;
is = src.createInputStream();
while((amountRead = is.read(buf,0,10240)) > -1)
{
os.write(buf, 0, amountRead);
}
addPage( importedPage );
}
finally
{
if( is != null )
{
is.close();
}
if( os != null )
{
os.close();
}
}
return importedPage;
}
This will import and copy the contents from another location. Currently
the content stream is stored in a scratch file. The scratch file is
associated with the document. If you are adding a page to this document
from another document and want to copy the contents to this document's
scratch file then use this method otherwise just use the addPage method. |
public boolean isAllSecurityToBeRemoved() {
return allSecurityToBeRemoved;
}
|
public boolean isEncrypted() {
return document.isEncrypted();
}
This will tell if this document is encrypted or not. |
public boolean isOwnerPassword(String password) throws IOException, CryptographyException {
return false;
/*boolean retval = false;
if( password == null )
{
password = "";
}
PDFEncryption encryptor = new PDFEncryption();
PDEncryptionDictionary encryptionDictionary = getEncryptionDictionary();
if( encryptionDictionary == null )
{
throw new IOException( "Error: Document is not encrypted" );
}
else
{
if( encryptionDictionary instanceof PDStandardEncryption )
{
COSString documentID = (COSString)document.getDocumentID().get( 0 );
PDStandardEncryption standard = (PDStandardEncryption)encryptionDictionary;
retval = encryptor.isOwnerPassword(
password.getBytes(),
standard.getUserKey(),
standard.getOwnerKey(),
standard.getPermissions(),
documentID.getBytes(),
standard.getRevision(),
standard.getLength()/8 );
}
else
{
throw new IOException( "Error: Encyption dictionary is not 'Standard'" +
encryptionDictionary.getClass().getName() );
}
}
return retval;*/
} Deprecated!
This will determine if this is the owner password. This only applies when
the document is encrypted and uses standard encryption. |
public boolean isUserPassword(String password) throws IOException, CryptographyException {
return false;
/*boolean retval = false;
if( password == null )
{
password = "";
}
PDFEncryption encryptor = new PDFEncryption();
PDEncryptionDictionary encryptionDictionary = getEncryptionDictionary();
if( encryptionDictionary == null )
{
throw new IOException( "Error: Document is not encrypted" );
}
else
{
if( encryptionDictionary instanceof PDStandardEncryption )
{
COSString documentID = (COSString)document.getDocumentID().get(0);
PDStandardEncryption standard = (PDStandardEncryption)encryptionDictionary;
retval = encryptor.isUserPassword(
password.getBytes(),
standard.getUserKey(),
standard.getOwnerKey(),
standard.getPermissions(),
documentID.getBytes(),
standard.getRevision(),
standard.getLength()/8 );
}
else
{
throw new IOException( "Error: Encyption dictionary is not 'Standard'" +
encryptionDictionary.getClass().getName() );
}
}
return retval;*/
} Deprecated!
This will determine if this is the user password. This only applies when
the document is encrypted and uses standard encryption. |
public static PDDocument load(URL url) throws IOException {
return load( url.openStream() );
}
This will load a document from a url. |
public static PDDocument load(String filename) throws IOException {
return load( new FileInputStream( filename ) );
}
This will load a document from a file. |
public static PDDocument load(File file) throws IOException {
return load( new FileInputStream( file ) );
}
This will load a document from a file. |
public static PDDocument load(InputStream input) throws IOException {
return load( input, null );
}
This will load a document from an input stream. |
public static PDDocument load(URL url,
boolean force) throws IOException {
return load(url.openStream(), force);
}
This will load a document from a url. Used for skipping corrupt
pdf objects |
public static PDDocument load(URL url,
RandomAccess scratchFile) throws IOException {
return load( url.openStream(), scratchFile );
}
This will load a document from a url. |
public static PDDocument load(String filename,
boolean force) throws IOException {
return load(new FileInputStream( filename ), force);
}
This will load a document from a file. Allows for skipping corrupt pdf
objects |
public static PDDocument load(String filename,
RandomAccess scratchFile) throws IOException {
return load( new FileInputStream( filename ), scratchFile );
}
This will load a document from a file. |
public static PDDocument load(File file,
RandomAccess scratchFile) throws IOException {
return load( new FileInputStream( file ) );
}
This will load a document from a file. |
public static PDDocument load(InputStream input,
boolean force) throws IOException {
return load(input, null, force);
}
This will load a document from an input stream.
Allows for skipping corrupt pdf objects |
public static PDDocument load(InputStream input,
RandomAccess scratchFile) throws IOException {
PDFParser parser = new PDFParser( new BufferedInputStream( input ), scratchFile );
parser.parse();
return parser.getPDDocument();
}
This will load a document from an input stream. |
public static PDDocument load(InputStream input,
RandomAccess scratchFile,
boolean force) throws IOException {
PDFParser parser = new PDFParser( new BufferedInputStream( input ), scratchFile, force);
parser.parse();
return parser.getPDDocument();
}
This will load a document from an input stream. Allows for skipping corrupt pdf objects |
public void openProtection(DecryptionMaterial pm) throws BadSecurityHandlerException, IOException, CryptographyException {
PDEncryptionDictionary dict = this.getEncryptionDictionary();
if(dict.getFilter() != null)
{
securityHandler = SecurityHandlersManager.getInstance().getSecurityHandler(dict.getFilter());
securityHandler.decryptDocument(this, pm);
document.dereferenceObjectStreams();
document.setEncryptionDictionary( null );
}
else
{
throw new RuntimeException("This document does not need to be decrypted");
}
}
Tries to decrypt the document in memory using the provided decryption material. |
public void print() throws PrinterException {
print( PrinterJob.getPrinterJob() );
}
This will send the PDF document to a printer. The printing functionality
depends on the org.apache.pdfbox.pdfviewer.PageDrawer functionality. The PageDrawer
is a work in progress and some PDFs will print correctly and some will
not. This is a convenience method to create the java.awt.print.PrinterJob.
The PDDocument implements the java.awt.print.Pageable interface and
PDPage implementes the java.awt.print.Printable interface, so advanced printing
capabilities can be done by using those interfaces instead of this method. |
public void print(PrinterJob printJob) throws PrinterException {
if(printJob == null)
{
throw new PrinterException( "The delivered printJob is null." );
}
AccessPermission currentPermissions = this.getCurrentAccessPermission();
if(!currentPermissions.canPrint())
{
throw new PrinterException( "You do not have permission to print this document." );
}
printJob.setPageable(this);
if( printJob.printDialog() )
{
printJob.print();
}
}
|
public void protect(ProtectionPolicy pp) throws BadSecurityHandlerException {
SecurityHandler handler = SecurityHandlersManager.getInstance().getSecurityHandler(pp);
securityHandler = handler;
}
Protects the document with the protection policy pp. The document content will be really encrypted
when it will be saved. This method only marks the document for encryption. |
public boolean removePage(PDPage page) {
PDPageNode parent = page.getParent();
boolean retval = parent.getKids().remove( page );
if( retval )
{
//do a recursive updateCount starting at the root
//of the document
getDocumentCatalog().getPages().updateCount();
}
return retval;
}
Remove the page from the document. |
public boolean removePage(int pageNumber) {
boolean removed = false;
List allPages = getDocumentCatalog().getAllPages();
if( allPages.size() > pageNumber)
{
PDPage page = (PDPage)allPages.get( pageNumber );
removed = removePage( page );
}
return removed;
}
Remove the page from the document. |
public void save(String fileName) throws IOException, COSVisitorException {
save( new FileOutputStream( fileName ) );
}
This will save this document to the filesystem. |
public void save(OutputStream output) throws IOException, COSVisitorException {
//update the count in case any pages have been added behind the scenes.
getDocumentCatalog().getPages().updateCount();
COSWriter writer = null;
try
{
writer = new COSWriter( output );
writer.write( this );
writer.close();
}
finally
{
if( writer != null )
{
writer.close();
}
}
}
This will save the document to an output stream. |
public void setAllSecurityToBeRemoved(boolean allSecurityToBeRemoved) {
this.allSecurityToBeRemoved = allSecurityToBeRemoved;
}
|
public void setDocumentInformation(PDDocumentInformation info) {
documentInformation = info;
document.getTrailer().setItem( COSName.INFO, info.getDictionary() );
}
This will set the document information for this document. |
public void setEncryptionDictionary(PDEncryptionDictionary encDictionary) throws IOException {
encParameters = encDictionary;
}
This will set the encryption dictionary for this document. |
public void silentPrint() throws PrinterException {
silentPrint( PrinterJob.getPrinterJob() );
}
This will send the PDF to the default printer without prompting the user
for any printer settings. |
public void silentPrint(PrinterJob printJob) throws PrinterException {
if(printJob == null)
{
throw new PrinterException( "The delivered printJob is null." );
}
AccessPermission currentPermissions = this.getCurrentAccessPermission();
if(!currentPermissions.canPrint())
{
throw new PrinterException( "You do not have permission to print this document." );
}
printJob.setPageable(this);
printJob.print();
}
This will send the PDF to the default printer without prompting the user
for any printer settings. |
public boolean wasDecryptedWithOwnerPassword() {
return false;
} Deprecated! use - getCurrentAccessPermission instead
This will tell if the document was decrypted with the master password. This
entry is invalid if the PDF was not decrypted. |
public boolean willEncryptWhenSaving() {
return false;
} Deprecated! Do - not rely on this method anymore. It is the responsibility of
COSWriter to hold this state
Internal method do determine if the document will be encrypted when it is saved. |