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.
Modifier and Type | Field and Description |
---|---|
protected boolean |
hasSignature |
protected PdfSignatureAppearance |
sigApp |
protected XmlSignatureAppearance |
sigXmlApp |
protected com.itextpdf.text.pdf.PdfStamperImp |
stamper
The writer
|
Modifier | Constructor and Description |
---|---|
protected |
PdfStamper() |
|
PdfStamper(PdfReader reader, OutputStream os)
Starts the process of adding extra content to an existing PDF document.
|
|
PdfStamper(PdfReader reader, OutputStream os, char pdfVersion)
Starts the process of adding extra content to an existing PDF document.
|
|
PdfStamper(PdfReader reader, OutputStream os, char pdfVersion, boolean append)
Starts the process of adding extra content to an existing PDF document, possibly as a new revision.
|
Modifier and Type | Method and Description |
---|---|
void |
addAnnotation(PdfAnnotation annot, int page)
Adds an annotation of form field in a specific page.
|
void |
addComments(FdfReader fdf)
Adds the comments present in an FDF file.
|
void |
addFileAttachment(String description, byte[] fileStore, String file, String fileDisplay)
Adds a file attachment at the document level.
|
void |
addFileAttachment(String description, PdfFileSpecification fs)
Adds a file attachment at the document level.
|
void |
addJavaScript(String js)
Adds a JavaScript action at the document level.
|
void |
addJavaScript(String name, String js)
Adds a JavaScript action at the document level.
|
boolean |
addNamedDestination(String name, int page, PdfDestination dest) |
PdfFormField |
addSignature(String name, int page, float llx, float lly, float urx, float ury)
Adds an empty signature.
|
void |
addViewerPreference(PdfName key, PdfObject value)
Adds a viewer preference
|
void |
close()
Closes the document.
|
static PdfStamper |
createSignature(PdfReader reader, OutputStream os, char pdfVersion)
Applies a digital signature to a document.
|
static PdfStamper |
createSignature(PdfReader reader, OutputStream os, char pdfVersion, File tempFile)
Applies a digital signature to a 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 |
createXmlSignature(PdfReader reader, OutputStream os) |
void |
createXmpMetadata() |
void |
flush()
Causes any pending changes to the direct under/over content (e.g.
|
AcroFields |
getAcroFields()
Gets the AcroFields object that allows to get and set field values and to merge FDF forms.
|
PdfImportedPage |
getImportedPage(PdfReader reader, int pageNumber)
Gets a page from other PDF document.
|
LtvVerification |
getLtvVerification() |
Map<String,String> |
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<String,PdfLayer> |
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.
|
PdfContentByte |
getUnderContent(int pageNum)
Gets a PdfContentByte to write under the page of the original document.
|
PdfWriter |
getWriter()
Gets the underlying PdfWriter.
|
XmlSignatureAppearance |
getXmlSignatureAppearance()
Gets the xml signing instance.
|
XmpWriter |
getXmpWriter() |
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 |
makePackage(PdfCollection collection)
Adds or replaces the Collection Dictionary in the Catalog.
|
void |
makePackage(PdfName initialView)
This is the most simple way to change a PDF into a portable collection.
|
void |
markUsed(PdfObject obj)
Marks the specified object as used or changed.
|
boolean |
partialFormFlattening(String name)
Adds name to the list of fields that will be flattened on close, all the other fields will remain.
|
void |
replacePage(PdfReader r, int pageImported, int pageReplaced)
Replaces a page from this document with a page from other document.
|
void |
setAnnotationFlattening(boolean flat)
Flatten annotations with an appearance stream on close().
|
void |
setDuration(int seconds, int page)
Sets the display duration for the page (for presentations)
|
void |
setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions)
Sets the encryption options for this document.
|
void |
setEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, boolean strength128Bits)
Sets the encryption options for this document.
|
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 |
setEncryption(int encryptionType, String userPassword, String ownerPassword, int permissions)
Sets the 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<String,String> moreInfo)
An optional String map to add or change values in the info dictionary.
|
void |
setOutlines(List<HashMap<String,Object>> outlines)
Sets the bookmarks.
|
void |
setPageAction(PdfName actionType, PdfAction action, int page)
Sets the open and close page additional action.
|
void |
setRotateContents(boolean rotateContents)
Flags the content to be automatically adjusted to compensate the original page rotation.
|
void |
setThumbnail(Image image, int page)
Sets the thumbnail image for a page.
|
void |
setTransition(PdfTransition transition, int page)
Sets the transition for the page
|
void |
setViewerPreferences(int preferences)
Sets the viewer preferences.
|
void |
setXmpMetadata(byte[] xmp)
Sets the XMP metadata.
|
protected com.itextpdf.text.pdf.PdfStamperImp stamper
protected boolean hasSignature
protected PdfSignatureAppearance sigApp
protected XmlSignatureAppearance sigXmlApp
public PdfStamper(PdfReader reader, OutputStream os) throws DocumentException, IOException
The reader will be closed when this PdfStamper is closed
reader
- the original document. It cannot be reused
os
- the output stream
DocumentException
- on error
IOException
- on error
public PdfStamper(PdfReader reader, OutputStream os, char pdfVersion) throws DocumentException, IOException
The reader will be closed when this PdfStamper is closed
reader
- the original document. It cannot be reused
os
- the output stream
pdfVersion
- the new pdf version or '\0' to keep the same version as the original document
DocumentException
- on error
IOException
- on error
public PdfStamper(PdfReader reader, OutputStream os, char pdfVersion, boolean append) throws DocumentException, IOException
The reader will be closed when this PdfStamper is closed
reader
- the original document. It cannot be reused
os
- the output stream
pdfVersion
- the new pdf version or '\0' to keep the same version as the original document
append
- if true
appends the document changes as a new revision. This is only useful for multiple signatures as nothing is gained in speed or memory
DocumentException
- on error
IOException
- on error
protected PdfStamper()
public Map<String,String> getMoreInfo()
String
map to add or change values in the info dictionary.
null
public void setMoreInfo(Map<String,String> moreInfo)
String
map to add or change values in the info dictionary. Entries with null
values delete the key in the original info dictionary
moreInfo
- additional entries to the info dictionary
public void replacePage(PdfReader r, int pageImported, int pageReplaced)
r
- the PdfReader
from where the new page will be imported
pageImported
- the page number of the imported page
pageReplaced
- the page to replace in this document
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 inserted
mediabox
- the size of the new page
public PdfSignatureAppearance getSignatureAppearance()
public XmlSignatureAppearance getXmlSignatureAppearance()
public void flush()
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 error
IOException
- on error
public PdfContentByte getUnderContent(int pageNum)
PdfContentByte
to write under the page of the original document.
pageNum
- the page number where the extra content is written
PdfContentByte
to write under the page of the original document
public PdfContentByte getOverContent(int pageNum)
PdfContentByte
to write over the page of the original document.
pageNum
- the page number where the extra content is written
PdfContentByte
to write over the page of the original document
public boolean isRotateContents()
public void setRotateContents(boolean rotateContents)
true
.
rotateContents
- true
to set auto-rotation, false
otherwise
public void setEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, boolean strength128Bits) throws DocumentException
userPassword
- the user password. Can be null or empty
ownerPassword
- the owner password. Can be null or empty
permissions
- the user permissions
strength128Bits
- true
for 128 bit key length, false
for 40 bit key length
DocumentException
- if anything was already written to the output
public void setEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType) throws DocumentException
setEncryption
in interface PdfEncryptionSettings
userPassword
- the user password. Can be null or empty
ownerPassword
- the owner password. Can be null or empty
permissions
- the user permissions
encryptionType
- the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128, ENCRYPTION_AES128 or ENCRYPTION_AES256. Optionally DO_NOT_ENCRYPT_METADATA can be ored to output the metadata in cleartext
DocumentException
- if the document is already open
public void setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions) throws DocumentException
strength
- true
for 128 bit key length, false
for 40 bit key length
userPassword
- the user password. Can be null or empty
ownerPassword
- the owner password. Can be null or empty
permissions
- the user permissions
DocumentException
- if anything was already written to the output
public void setEncryption(int encryptionType, String userPassword, String ownerPassword, int permissions) throws DocumentException
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
userPassword
- the user password. Can be null or empty
ownerPassword
- the owner password. Can be null or empty
permissions
- the user permissions
DocumentException
- if anything was already written to the output
public void setEncryption(Certificate[] certs, int[] permissions, int encryptionType) throws DocumentException
setEncryption
in interface PdfEncryptionSettings
certs
- the public certificates to be used for the encryption
permissions
- the user permissions for each of the certificates
encryptionType
- the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128, ENCRYPTION_AES128 or ENCRYPTION_AES256.
DocumentException
- if the encryption was set too late
public PdfImportedPage getImportedPage(PdfReader reader, int pageNumber)
reader
- the PDF document where the page is
pageNumber
- the page number. The first page is 1
public PdfWriter getWriter()
public PdfReader getReader()
public AcroFields getAcroFields()
AcroFields
object that allows to get and set field values and to merge FDF forms.
AcroFields
object
public void setFormFlattening(boolean flat)
addAnnotation(PdfAnnotation,int)
will never be flattened.
flat
- true
to flatten the fields, false
to keep the fields
public void setFreeTextFlattening(boolean flat)
flat
- true
to flatten the FreeText annotations, false
(the default) to keep the FreeText annotations as active content.
public void setAnnotationFlattening(boolean flat)
flat
- boolean to indicate whether iText should flatten annotations or not.
public void addAnnotation(PdfAnnotation annot, int page)
PdfAnnotation.setPlaceInPage(int)
.
annot
- the annotation
page
- the page
public PdfFormField addSignature(String name, int page, float llx, float lly, float urx, float ury)
name
- the name of the signature
page
- the page number
llx
- lower left x coordinate of the signature's position
lly
- lower left y coordinate of the signature's position
urx
- upper right x coordinate of the signature's position
ury
- upper right y coordinate of the signature's position
public void addComments(FdfReader fdf) throws IOException
fdf
- the FDF file
IOException
- on error
public void setOutlines(List<HashMap<String,Object>> outlines)
SimpleBookmark
.
outlines
- the bookmarks or null
to remove any
public void setThumbnail(Image image, int page) throws PdfException, DocumentException
image
- the image
page
- the page
PdfException
- on error
DocumentException
- on error
public boolean partialFormFlattening(String name)
name
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.
Calling setFormFlattening(true)
is needed to have any kind of flattening.
name
- the field name
true
if the field exists, false
otherwise
public void addJavaScript(String js)
js
- the JavaScript code
public void addJavaScript(String name, String js)
name
- the name for the JavaScript snippet in the name tree
js
- the JavaScript code
public void addFileAttachment(String description, byte[] fileStore, String file, String fileDisplay) throws IOException
description
- the file description
fileStore
- an array with the file. If it's null
the file will be read from the disk
file
- the path to the file. It will only be used if fileStore
is not null
fileDisplay
- the actual file name stored in the pdf
IOException
- on error
public void addFileAttachment(String description, PdfFileSpecification fs) throws IOException
description
- the file description
fs
- the file specification
IOException
public void makePackage(PdfName initialView)
initialView
- can be PdfName.D, PdfName.T or PdfName.H
public void makePackage(PdfCollection collection)
collection
- the new collection dictionary.
public void setViewerPreferences(int preferences)
setViewerPreferences
in interface PdfViewerPreferences
preferences
- the viewer preferences
PdfViewerPreferences.setViewerPreferences(int)
public void addViewerPreference(PdfName key, PdfObject value)
addViewerPreference
in interface PdfViewerPreferences
key
- a key for a viewer preference
value
- the value for the viewer preference
PdfViewerPreferences.addViewerPreference(com.itextpdf.text.pdf.PdfName, com.itextpdf.text.pdf.PdfObject)
public void setXmpMetadata(byte[] xmp)
xmp
-
PdfWriter.setXmpMetadata(byte[])
public void createXmpMetadata()
public XmpWriter getXmpWriter()
public boolean isFullCompression()
true
if the 1.5 compression is on
public void setFullCompression() throws DocumentException
DocumentException
public void setPageAction(PdfName actionType, PdfAction action, int page) throws PdfException
actionType
- the action type. It can be PdfWriter.PAGE_OPEN
or PdfWriter.PAGE_CLOSE
action
- the action to perform
page
- the page where the action will be applied. The first page is 1
PdfException
- if the action type is invalid
public void setDuration(int seconds, int page)
seconds
- the number of seconds to display the page. A negative value removes the entry
page
- the page where the duration will be applied. The first page is 1
public void setTransition(PdfTransition transition, int page)
transition
- the transition object. A null
removes the transition
page
- the page where the transition will be applied. The first page is 1
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 document
os
- the output stream or null
to keep the document in the temporary file
pdfVersion
- the new pdf version or '\0' to keep the same version as the original document
tempFile
- 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 used
append
- if true
the signature and all the other content will be added as a new revision thus not invalidating existing signatures
PdfStamper
DocumentException
- on error
IOException
- on error
public static PdfStamper createSignature(PdfReader reader, OutputStream os, char pdfVersion) throws DocumentException, IOException
Note that the pdf is created in memory.
A possible use 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'); 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 document
os
- the output stream
pdfVersion
- the new pdf version or '\0' to keep the same version as the original document
PdfStamper
DocumentException
- on error
IOException
- on error
public static PdfStamper createSignature(PdfReader reader, OutputStream os, char pdfVersion, File tempFile) throws DocumentException, IOException
A possible use 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")); 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 document
os
- the output stream or null
to keep the document in the temporary file
pdfVersion
- the new pdf version or '\0' to keep the same version as the original document
tempFile
- 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 used
PdfStamper
DocumentException
- on error
IOException
- on error
public static PdfStamper createXmlSignature(PdfReader reader, OutputStream os) throws IOException, DocumentException
IOException
DocumentException
public Map<String,PdfLayer> getPdfLayers()
public void markUsed(PdfObject obj)
obj
- the used PDF object
public LtvVerification getLtvVerification()
public boolean addNamedDestination(String name, int page, PdfDestination dest) throws IOException
IOException
Copyright © 1998–2024. All rights reserved.