📄 pddocument.java
字号:
public static PDDocument load( String filename ) throws IOException
{
return load( new FileInputStream( filename ) );
}
/**
* This will load a document from a file.
*
* @param filename The name of the file to load.
* @param scratchFile A location to store temp PDFBox data for this document.
*
* @return The document that was loaded.
*
* @throws IOException If there is an error reading from the stream.
*/
public static PDDocument load( String filename, RandomAccess scratchFile ) throws IOException
{
return load( new FileInputStream( filename ), scratchFile );
}
/**
* This will load a document from a file.
*
* @param file The name of the file to load.
*
* @return The document that was loaded.
*
* @throws IOException If there is an error reading from the stream.
*/
public static PDDocument load( File file ) throws IOException
{
return load( new FileInputStream( file ) );
}
/**
* This will load a document from a file.
*
* @param file The name of the file to load.
* @param scratchFile A location to store temp PDFBox data for this document.
*
* @return The document that was loaded.
*
* @throws IOException If there is an error reading from the stream.
*/
public static PDDocument load( File file, RandomAccess scratchFile ) throws IOException
{
return load( new FileInputStream( file ) );
}
/**
* This will load a document from an input stream.
*
* @param input The stream that contains the document.
*
* @return The document that was loaded.
*
* @throws IOException If there is an error reading from the stream.
*/
public static PDDocument load( InputStream input ) throws IOException
{
return load( input, null );
}
/**
* This will load a document from an input stream.
*
* @param input The stream that contains the document.
* @param scratchFile A location to store temp PDFBox data for this document.
*
* @return The document that was loaded.
*
* @throws IOException If there is an error reading from the stream.
*/
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 save this document to the filesystem.
*
* @param fileName The file to save as.
*
* @throws IOException If there is an error saving the document.
* @throws COSVisitorException If an error occurs while generating the data.
*/
public void save( String fileName ) throws IOException, COSVisitorException
{
save( new FileOutputStream( fileName ) );
}
/**
* This will save the document to an output stream.
*
* @param output The stream to write to.
*
* @throws IOException If there is an error writing the document.
* @throws COSVisitorException If an error occurs while generating the data.
*/
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 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!!
*
* @return The total number of pages in the PDF document.
* @deprecated Use the getNumberOfPages method instead!
*/
public int getPageCount()
{
return getNumberOfPages();
}
/**
* {@inheritDoc}
*/
public int getNumberOfPages()
{
PDDocumentCatalog cat = getDocumentCatalog();
return (int)cat.getPages().getCount();
}
/**
* {@inheritDoc}
*/
public PageFormat getPageFormat(int pageIndex)
{
PDPage page = (PDPage)getDocumentCatalog().getAllPages().get( pageIndex );
PDRectangle mediaBox = page.findMediaBox();
PageFormat format = new PageFormat();
Paper paper = new Paper();
//hmm the imageable area might need to be the CropBox instead
//of the media box???
double width=mediaBox.getWidth();
double height=mediaBox.getHeight();
if( width > height )
{
format.setOrientation( PageFormat.LANDSCAPE );
width=mediaBox.getHeight();
height=mediaBox.getWidth();
}
paper.setImageableArea( 0,0,width,height);
paper.setSize( width, height );
format.setPaper( paper );
return format;
}
/**
* {@inheritDoc}
*/
public Printable getPrintable(int pageIndex)
{
return (Printable)getDocumentCatalog().getAllPages().get( pageIndex );
}
/**
* This will send the PDF document to a printer. The printing functionality
* depends on the org.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.
*
* @throws PrinterException If there is an error while sending the PDF to
* the printer, or you do not have permissions to print this document.
*/
public void print() throws PrinterException
{
AccessPermission currentPermissions = this.getCurrentAccessPermission();
if(!currentPermissions.canPrint())
{
throw new PrinterException( "You do not have permission to print this document." );
}
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPageable(this);
if( printJob.printDialog() )
{
printJob.print();
}
}
/**
* This will send the PDF to the default printer without prompting the user
* for any printer settings.
*
* @see PDDocument#print()
*
* @throws PrinterException If there is an error while printing.
*/
public void silentPrint() throws PrinterException
{
AccessPermission currentPermissions = this.getCurrentAccessPermission();
if(!currentPermissions.canPrint())
{
throw new PrinterException( "You do not have permission to print this document." );
}
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPageable(this);
printJob.print();
}
/**
* This will close the underlying COSDocument object.
*
* @throws IOException If there is an error releasing resources.
*/
public void close() throws IOException
{
document.close();
}
/**
* 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.
*
* @see org.pdfbox.pdmodel.encryption.StandardProtectionPolicy
* @see org.pdfbox.pdmodel.encryption.PublicKeyProtectionPolicy
*
* @param pp The protection policy.
*
* @throws BadSecurityHandlerException If there is an error during protection.
*/
public void protect(ProtectionPolicy pp) throws BadSecurityHandlerException
{
SecurityHandler handler = SecurityHandlersManager.getInstance().getSecurityHandler(pp);
securityHandler = handler;
}
/**
* Tries to decrypt the document in memory using the provided decryption material.
*
* @see org.pdfbox.pdmodel.encryption.StandardDecryptionMaterial
* @see org.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial
*
* @param pm The decryption material (password or certificate).
*
* @throws BadSecurityHandlerException If there is an error during decryption.
* @throws IOException If there is an error reading cryptographic information.
* @throws CryptographyException If there is an error during decryption.
*/
public void openProtection(DecryptionMaterial pm)
throws BadSecurityHandlerException, IOException, CryptographyException
{
PDEncryptionDictionary dict = this.getEncryptionDictionary();
if(dict.getFilter() != null)
{
SecurityHandler handler = SecurityHandlersManager.getInstance().getSecurityHandler(dict.getFilter());
securityHandler = handler;
handler.decryptDocument(this, pm);
document.dereferenceObjectStreams();
}
else
{
throw new RuntimeException("This document does not need to be decrypted");
}
}
/**
* 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.
*
* @return the access permissions for the current user on the document.
*/
public AccessPermission getCurrentAccessPermission()
{
if(this.securityHandler == null)
{
return AccessPermission.getOwnerAccessPermission();
}
return securityHandler.getCurrentAccessPermission();
}
/**
* Get the security handler that is used for document encryption.
*
* @return The handler used to encrypt/decrypt the document.
*/
public SecurityHandler getSecurityHandler()
{
return securityHandler;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -