📄 pddocument.java
字号:
}
}
return documentCatalog;
}
/**
* This will tell if this document is encrypted or not.
*
* @return true If this document is encrypted.
*/
public boolean isEncrypted()
{
return document.isEncrypted();
}
/**
* 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.
*
* @return The encryption dictionary(most likely a PDStandardEncryption object)
*
* @throws IOException If there is an error determining which security handler to use.
*/
public PDEncryptionDictionary getEncryptionDictionary() throws IOException
{
if( encParameters == null )
{
if( isEncrypted() )
{
encParameters = new PDEncryptionDictionary(document.getEncryptionDictionary());
}
else
{
encParameters = new PDEncryptionDictionary();
}
}
return encParameters;
}
/**
* This will set the encryption dictionary for this document.
*
* @param encDictionary The encryption dictionary(most likely a PDStandardEncryption object)
*
* @throws IOException If there is an error determining which security handler to use.
*/
public void setEncryptionDictionary( PDEncryptionDictionary encDictionary ) throws IOException
{
encParameters = encDictionary;
}
/**
* This will determine if this is the user password. This only applies when
* the document is encrypted and uses standard encryption.
*
* @param password The plain text user password.
*
* @return true If the password passed in matches the user password used to encrypt the document.
*
* @throws IOException If there is an error determining if it is the user password.
* @throws CryptographyException If there is an error in the encryption algorithms.
*
* @deprecated
*/
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;*/
}
/**
* This will determine if this is the owner password. This only applies when
* the document is encrypted and uses standard encryption.
*
* @param password The plain text owner password.
*
* @return true If the password passed in matches the owner password used to encrypt the document.
*
* @throws IOException If there is an error determining if it is the user password.
* @throws CryptographyException If there is an error in the encryption algorithms.
*
* @deprecated
*/
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;*/
}
/**
* 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.
*
* @param password Either the user or owner password.
*
* @throws CryptographyException If there is an error decrypting the document.
* @throws IOException If there is an error getting the stream data.
* @throws InvalidPasswordException If the password is not a user or owner password.
*
*/
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 tell if the document was decrypted with the master password. This
* entry is invalid if the PDF was not decrypted.
*
* @return true if the pdf was decrypted with the master password.
*
* @deprecated use <code>getCurrentAccessPermission</code> instead
*/
public boolean wasDecryptedWithOwnerPassword()
{
return false;
}
/**
* This will <b>mark</b> 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.
*
* @param ownerPassword The owner password to encrypt the document.
* @param userPassword The user password to encrypt the document.
*
* @throws CryptographyException If an error occurs during encryption.
* @throws IOException If there is an error accessing the data.
*
*/
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);
}
}
/**
* 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.
*
* @return The owner password passed to the encrypt method.
*
* @deprecated Do not rely on this method anymore.
*/
public String getOwnerPasswordForEncryption()
{
return null;
}
/**
* 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.
*
* @return The user password passed to the encrypt method.
*
* @deprecated Do not rely on this method anymore.
*/
public String getUserPasswordForEncryption()
{
return null;
}
/**
* Internal method do determine if the document will be encrypted when it is saved.
*
* @return True if encrypt has been called and the document
* has not been saved yet.
*
* @deprecated Do not rely on this method anymore. It is the responsibility of
* COSWriter to hold this state
*/
public boolean willEncryptWhenSaving()
{
return false;
}
/**
* This shoule only be called by the COSWriter after encryption has completed.
*
* @deprecated Do not rely on this method anymore. It is the responsability of
* COSWriter to hold this state.
*/
public void clearWillEncryptWhenSaving()
{
//method is deprecated.
}
/**
* This will load a document from a url.
*
* @param url The url to load the PDF from.
*
* @return The document that was loaded.
*
* @throws IOException If there is an error reading from the stream.
*/
public static PDDocument load( URL url ) throws IOException
{
return load( url.openStream() );
}
/**
* This will load a document from a url.
*
* @param url The url to load the PDF from.
* @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( URL url, RandomAccess scratchFile ) throws IOException
{
return load( url.openStream(), scratchFile );
}
/**
* This will load a document from a file.
*
* @param filename The name of the file to load.
*
* @return The document that was loaded.
*
* @throws IOException If there is an error reading from the stream.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -