public class PdfStamper extends Object implements PdfViewerPreferences, PdfEncryptionSettings
It is also possible to change the field values and to flatten them. New fields can be added but not flattened.
| Constructor and Description |
|---|
PdfStamper(PdfReader reader,
OutputStream os)
Starts the process of adding extra content to an existing PDF
document.
|
PdfStamper(PdfReader reader,
OutputStream os,
Calendar globalDate)
Starts the process of adding extra content to an existing PDF
document.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addAnnotation(PdfAnnotation annot,
int page)
Adds an annotation of form field in a specific page.
|
void |
addViewerPreference(PdfName key,
PdfObject value)
Adds a viewer preference
|
void |
close()
Closes the document.
|
void |
close(Calendar globalDate)
Closes the document.
|
static PdfStamper |
createSignature(PdfReader reader,
OutputStream os,
char pdfVersion,
File tempFile,
boolean append)
Applies a digital signature to a document, possibly as a new revision, making
possible multiple signatures.
|
static PdfStamper |
createSignature(PdfReader reader,
OutputStream os,
char pdfVersion,
File tempFile,
boolean append,
Calendar globalDate)
Applies a digital signature to a document, possibly as a new revision, making
possible multiple signatures.
|
AcroFields |
getAcroFields()
Gets the
AcroFields object that allows to get and set field values
and to merge FDF forms. |
Map |
getMoreInfo()
Gets the optional
String map to add or change values in
the info dictionary. |
PdfContentByte |
getOverContent(int pageNum)
Gets a
PdfContentByte to write over the page of
the original document. |
Map |
getPdfLayers()
Gets the PdfLayer objects in an existing document as a Map
with the names/titles of the layers as keys.
|
PdfReader |
getReader()
Gets the underlying PdfReader.
|
PdfSignatureAppearance |
getSignatureAppearance()
Gets the signing instance.
|
PdfWriter |
getWriter()
Gets the underlying PdfWriter.
|
void |
insertPage(int pageNumber,
Rectangle mediabox)
Inserts a blank page.
|
boolean |
isFullCompression()
Gets the 1.5 compression status.
|
boolean |
isRotateContents()
Checks if the content is automatically adjusted to compensate
the original page rotation.
|
void |
setEncryption(byte[] userPassword,
byte[] ownerPassword,
int permissions,
int encryptionType)
Sets the encryption options for this document.
|
void |
setEncryption(Certificate[] certs,
int[] permissions,
int encryptionType)
Sets the certificate encryption options for this document.
|
void |
setFormFlattening(boolean flat)
Determines if the fields are flattened on close.
|
void |
setFreeTextFlattening(boolean flat)
Determines if the FreeText annotations are flattened on close.
|
void |
setFullCompression()
Sets the document's compression to the new 1.5 mode with object streams and xref
streams.
|
void |
setMoreInfo(Map moreInfo)
An optional
String map to add or change values in
the info dictionary. |
void |
setOutlines(List outlines)
Sets the bookmarks.
|
void |
setRotateContents(boolean rotateContents)
Flags the content to be automatically adjusted to compensate
the original page rotation.
|
void |
setViewerPreferences(int preferences)
Sets the viewer preferences.
|
void |
setXmpMetadata(byte[] xmp)
Sets the XMP metadata.
|
public PdfStamper(PdfReader reader, OutputStream os, Calendar globalDate) throws DocumentException, IOException
reader - the original document. It cannot be reusedos - the output streamglobalDate - global dateDocumentException - on errorIOException - on errorpublic PdfStamper(PdfReader reader, OutputStream os) throws DocumentException, IOException
reader - the original document. It cannot be reusedos - the output streamDocumentException - on errorIOException - on error.public Map getMoreInfo()
String map to add or change values in
the info dictionary.null.public void setMoreInfo(Map moreInfo)
String map to add or change values in
the info dictionary. Entries with null
values delete the key in the original info dictionarymoreInfo - additional entries to the info dictionary.public void insertPage(int pageNumber,
Rectangle mediabox)
pageNumber will
be shifted up. If pageNumber is bigger than the total number of pages
the new page will be the last one.pageNumber - the page number position where the new page will be insertedmediabox - the size of the new pagepublic PdfSignatureAppearance getSignatureAppearance()
public void close()
throws DocumentException,
IOException
If closing a signed document with an external signature the closing must be done
in the PdfSignatureAppearance instance.
DocumentException - on errorIOException - on error.public void close(Calendar globalDate) throws DocumentException, IOException
If closing a signed document with an external signature the closing must be done
in the PdfSignatureAppearance instance.
globalDate - global dateDocumentException - on errorIOException - on error.public boolean isRotateContents()
public void setRotateContents(boolean rotateContents)
true.rotateContents - true to set auto-rotation, false
otherwisepublic void setEncryption(byte[] userPassword,
byte[] ownerPassword,
int permissions,
int encryptionType)
throws DocumentException
setEncryption in interface PdfEncryptionSettingsuserPassword - the user password. Can be null or emptyownerPassword - the owner password. Can be null or emptypermissions - the user permissionsencryptionType - 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 cleartextDocumentException - if the document is already openpublic void setEncryption(Certificate[] certs, int[] permissions, int encryptionType) throws DocumentException
setEncryption in interface PdfEncryptionSettingscerts - the public certificates to be used for the encryptionpermissions - the user permissions for each of the certificatesencryptionType - the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128.DocumentException - if the encryption was set too latepublic PdfWriter getWriter()
public PdfReader getReader()
public AcroFields getAcroFields()
AcroFields object that allows to get and set field values
and to merge FDF forms.AcroFields objectpublic void setFormFlattening(boolean flat)
addAnnotation(PdfAnnotation,int) will never be flattened.flat - true to flatten the fields, false
to keep the fieldspublic void setFreeTextFlattening(boolean flat)
flat - true to flatten the FreeText annotations, false
(the default) to keep the FreeText annotations as active content.public void addAnnotation(PdfAnnotation annot, int page)
PdfAnnotation.setPlaceInPage(int).annot - the annotationpage - the pagepublic void setOutlines(List outlines)
SimpleBookmark.outlines - the bookmarks or null to remove anypublic void setViewerPreferences(int preferences)
setViewerPreferences in interface PdfViewerPreferencespreferences - the viewer preferencesPdfViewerPreferences.setViewerPreferences(int)public void addViewerPreference(PdfName key, PdfObject value)
addViewerPreference in interface PdfViewerPreferenceskey - a key for a viewer preferencevalue - the value for the viewer preferencePdfViewerPreferences.addViewerPreference(com.aowagie.text.pdf.PdfName, com.aowagie.text.pdf.PdfObject)public void setXmpMetadata(byte[] xmp)
xmp - info with xmpPdfWriter.setXmpMetadata(byte[])public boolean isFullCompression()
true if the 1.5 compression is onpublic void setFullCompression()
public static PdfStamper createSignature(PdfReader reader, OutputStream os, char pdfVersion, File tempFile, boolean append) throws DocumentException, IOException
A possible use for adding a signature without invalidating an existing one is:
KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(new FileInputStream("my_private_key.pfx"), "my_password".toCharArray());
String alias = (String)ks.aliases().nextElement();
PrivateKey key = (PrivateKey)ks.getKey(alias, "my_password".toCharArray());
Certificate[] chain = ks.getCertificateChain(alias);
PdfReader reader = new PdfReader("original.pdf");
FileOutputStream fout = new FileOutputStream("signed.pdf");
PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0', new
File("/temp"), true);
PdfSignatureAppearance sap = stp.getSignatureAppearance();
sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
sap.setReason("I'm the author");
sap.setLocation("Lisbon");
// comment next line to have an invisible signature
sap.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, null);
stp.close();
reader - the original documentos - the output stream or null to keep the document in the temporary filepdfVersion - the new pdf version or '\0' to keep the same version as the original
documenttempFile - location of the temporary file. If it's a directory a temporary file will be created there.
If it's a file it will be used directly. The file will be deleted on exit unless os is null.
In that case the document can be retrieved directly from the temporary file. If it's null
no temporary file will be created and memory will be usedappend - if true the signature and all the other content will be added as a
new revision thus not invalidating existing signaturesPdfStamperDocumentException - on errorIOException - on errorpublic static PdfStamper createSignature(PdfReader reader, OutputStream os, char pdfVersion, File tempFile, boolean append, Calendar globalDate) throws DocumentException, IOException
A possible use for adding a signature without invalidating an existing one is:
KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(new FileInputStream("my_private_key.pfx"), "my_password".toCharArray());
String alias = (String)ks.aliases().nextElement();
PrivateKey key = (PrivateKey)ks.getKey(alias, "my_password".toCharArray());
Certificate[] chain = ks.getCertificateChain(alias);
PdfReader reader = new PdfReader("original.pdf");
FileOutputStream fout = new FileOutputStream("signed.pdf");
PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0', new
File("/temp"), true);
PdfSignatureAppearance sap = stp.getSignatureAppearance();
sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
sap.setReason("I'm the author");
sap.setLocation("Lisbon");
// comment next line to have an invisible signature
sap.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, null);
stp.close();
reader - the original documentos - the output stream or null to keep the document in the temporary filepdfVersion - the new pdf version or '\0' to keep the same version as the original
documenttempFile - location of the temporary file. If it's a directory a temporary file will be created there.
If it's a file it will be used directly. The file will be deleted on exit unless os is null.
In that case the document can be retrieved directly from the temporary file. If it's null
no temporary file will be created and memory will be usedappend - if true the signature and all the other content will be added as a
new revision thus not invalidating existing signaturesglobalDate - global datePdfStamperDocumentException - on errorIOException - on errorpublic PdfContentByte getOverContent(int pageNum)
PdfContentByte to write over the page of
the original document.pageNum - the page number where the extra content is writtenPdfContentByte to write over the page of
the original documentpublic Map getPdfLayers()
Copyright © 2021. All rights reserved.