📄 pdfstamper.java
字号:
stamper.setRotateContents(rotateContents);
}
/** Sets the encryption options for this document. The userPassword and the
* ownerPassword can be null or have zero length. In this case the ownerPassword
* is replaced by a random string. The open permissions for the document can be
* AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
* AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
* The permissions can be combined by ORing them.
* @param userPassword the user password. Can be null or empty
* @param ownerPassword the owner password. Can be null or empty
* @param permissions the user permissions
* @param strength128Bits <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
* @throws DocumentException if anything was already written to the output
*/
public void setEncryption(byte userPassword[], byte ownerPassword[], int permissions, boolean strength128Bits) throws DocumentException {
if (stamper.isAppend())
throw new DocumentException("Append mode does not support changing the encryption status.");
if (stamper.isContentWritten())
throw new DocumentException("Content was already written to the output.");
stamper.setEncryption(userPassword, ownerPassword, permissions, strength128Bits ? PdfWriter.STANDARD_ENCRYPTION_128 : PdfWriter.STANDARD_ENCRYPTION_40);
}
/** Sets the encryption options for this document. The userPassword and the
* ownerPassword can be null or have zero length. In this case the ownerPassword
* is replaced by a random string. The open permissions for the document can be
* AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
* AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
* The permissions can be combined by ORing them.
* @param userPassword the user password. Can be null or empty
* @param ownerPassword the owner password. Can be null or empty
* @param permissions the user permissions
* @param encryptionType the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128.
* Optionally DO_NOT_ENCRYPT_METADATA can be ored to output the metadata in cleartext
* @throws DocumentException if the document is already open
*/
public void setEncryption(byte userPassword[], byte ownerPassword[], int permissions, int encryptionType) throws DocumentException {
if (stamper.isAppend())
throw new DocumentException("Append mode does not support changing the encryption status.");
if (stamper.isContentWritten())
throw new DocumentException("Content was already written to the output.");
stamper.setEncryption(userPassword, ownerPassword, permissions, encryptionType);
}
/**
* Sets the encryption options for this document. The userPassword and the
* ownerPassword can be null or have zero length. In this case the ownerPassword
* is replaced by a random string. The open permissions for the document can be
* AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
* AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
* The permissions can be combined by ORing them.
* @param strength <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
* @param userPassword the user password. Can be null or empty
* @param ownerPassword the owner password. Can be null or empty
* @param permissions the user permissions
* @throws DocumentException if anything was already written to the output
*/
public void setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions) throws DocumentException {
setEncryption(DocWriter.getISOBytes(userPassword), DocWriter.getISOBytes(ownerPassword), permissions, strength);
}
/**
* Sets the encryption options for this document. The userPassword and the
* ownerPassword can be null or have zero length. In this case the ownerPassword
* is replaced by a random string. The open permissions for the document can be
* AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
* AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
* The permissions can be combined by ORing them.
* @param encryptionType the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128.
* Optionally DO_NOT_ENCRYPT_METADATA can be ored to output the metadata in cleartext
* @param userPassword the user password. Can be null or empty
* @param ownerPassword the owner password. Can be null or empty
* @param permissions the user permissions
* @throws DocumentException if anything was already written to the output
*/
public void setEncryption(int encryptionType, String userPassword, String ownerPassword, int permissions) throws DocumentException {
setEncryption(DocWriter.getISOBytes(userPassword), DocWriter.getISOBytes(ownerPassword), permissions, encryptionType);
}
/**
* Sets the certificate encryption options for this document. An array of one or more public certificates
* must be provided together with an array of the same size for the permissions for each certificate.
* The open permissions for the document can be
* AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
* AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
* The permissions can be combined by ORing them.
* Optionally DO_NOT_ENCRYPT_METADATA can be ored to output the metadata in cleartext
* @param certs the public certificates to be used for the encryption
* @param permissions the user permissions for each of the certicates
* @param encryptionType the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128.
* @throws DocumentException if the encryption was set too late
*/
public void setEncryption(Certificate[] certs, int[] permissions, int encryptionType) throws DocumentException {
if (stamper.isAppend())
throw new DocumentException("Append mode does not support changing the encryption status.");
if (stamper.isContentWritten())
throw new DocumentException("Content was already written to the output.");
stamper.setEncryption(certs, permissions, encryptionType);
}
/** Gets a page from other PDF document. Note that calling this method more than
* once with the same parameters will retrieve the same object.
* @param reader the PDF document where the page is
* @param pageNumber the page number. The first page is 1
* @return the template representing the imported page
*/
public PdfImportedPage getImportedPage(PdfReader reader, int pageNumber) {
return stamper.getImportedPage(reader, pageNumber);
}
/** Gets the underlying PdfWriter.
* @return the underlying PdfWriter
*/
public PdfWriter getWriter() {
return stamper;
}
/** Gets the underlying PdfReader.
* @return the underlying PdfReader
*/
public PdfReader getReader() {
return stamper.reader;
}
/** Gets the <CODE>AcroFields</CODE> object that allows to get and set field values
* and to merge FDF forms.
* @return the <CODE>AcroFields</CODE> object
*/
public AcroFields getAcroFields() {
return stamper.getAcroFields();
}
/** Determines if the fields are flattened on close. The fields added with
* {@link #addAnnotation(PdfAnnotation,int)} will never be flattened.
* @param flat <CODE>true</CODE> to flatten the fields, <CODE>false</CODE>
* to keep the fields
*/
public void setFormFlattening(boolean flat) {
stamper.setFormFlattening(flat);
}
/** Determines if the FreeText annotations are flattened on close.
* @param flat <CODE>true</CODE> to flatten the FreeText annotations, <CODE>false</CODE>
* (the default) to keep the FreeText annotations as active content.
*/
public void setFreeTextFlattening(boolean flat) {
stamper.setFreeTextFlattening(flat);
}
/**
* Adds an annotation of form field in a specific page. This page number
* can be overridden with {@link PdfAnnotation#setPlaceInPage(int)}.
* @param annot the annotation
* @param page the page
*/
public void addAnnotation(PdfAnnotation annot, int page) {
stamper.addAnnotation(annot, page);
}
/**
* Adds the comments present in an FDF file.
* @param fdf the FDF file
* @throws IOException on error
*/
public void addComments(FdfReader fdf) throws IOException {
stamper.addComments(fdf);
}
/**
* Sets the bookmarks. The list structure is defined in
* {@link SimpleBookmark}.
* @param outlines the bookmarks or <CODE>null</CODE> to remove any
*/
public void setOutlines(List outlines) {
stamper.setOutlines(outlines);
}
/**
* Sets the thumbnail image for a page.
* @param image the image
* @param page the page
* @throws PdfException on error
* @throws DocumentException on error
*/
public void setThumbnail(Image image, int page) throws PdfException, DocumentException {
stamper.setThumbnail(image, page);
}
/**
* Adds <CODE>name</CODE> to the list of fields that will be flattened on close,
* all the other fields will remain. If this method is never called or is called
* with invalid field names, all the fields will be flattened.
* <p>
* Calling <CODE>setFormFlattening(true)</CODE> is needed to have any kind of
* flattening.
* @param name the field name
* @return <CODE>true</CODE> if the field exists, <CODE>false</CODE> otherwise
*/
public boolean partialFormFlattening(String name) {
return stamper.partialFormFlattening(name);
}
/** Adds a JavaScript action at the document level. When the document
* opens all this JavaScript runs. The existing JavaScript will be replaced.
* @param js the JavaScript code
*/
public void addJavaScript(String js) {
stamper.addJavaScript(js, !PdfEncodings.isPdfDocEncoding(js));
}
/** Adds a file attachment at the document level. Existing attachments will be kept.
* @param description the file description
* @param fileStore an array with the file. If it's <CODE>null</CODE>
* the file will be read from the disk
* @param file the path to the file. It will only be used if
* <CODE>fileStore</CODE> is not <CODE>null</CODE>
* @param fileDisplay the actual file name stored in the pdf
* @throws IOException on error
*/
public void addFileAttachment(String description, byte fileStore[], String file, String fileDisplay) throws IOException {
addFileAttachment(description, PdfFileSpecification.fileEmbedded(stamper, file, fileDisplay, fileStore));
}
/** Adds a file attachment at the document level. Existing attachments will be kept.
* @param description the file description
* @param fs the file specification
*/
public void addFileAttachment(String description, PdfFileSpecification fs) throws IOException {
stamper.addFileAttachment(description, fs);
}
/**
* This is the most simple way to change a PDF into a
* portable collection. Choose one of the following names:
* <ul>
* <li>PdfName.D (detailed view)
* <li>PdfName.T (tiled view)
* <li>PdfName.H (hidden)
* </ul>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -