| Method from com.lowagie.text.pdf.PdfStamper Detail: |
public void addAnnotation(PdfAnnotation annot,
int page) {
stamper.addAnnotation(annot, page);
}
|
public void addComments(FdfReader fdf) throws IOException {
stamper.addComments(fdf);
}
Adds the comments present in an FDF file. |
public void addFileAttachment(String description,
PdfFileSpecification fs) throws IOException {
stamper.addFileAttachment(description, fs);
}
Adds a file attachment at the document level. Existing attachments will be kept. |
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. |
public void addJavaScript(String js) {
stamper.addJavaScript(js, !PdfEncodings.isPdfDocEncoding(js));
}
Adds a JavaScript action at the document level. When the document
opens all this JavaScript runs. The existing JavaScript will be replaced. |
public void addViewerPreference(PdfName key,
PdfObject value) {
stamper.addViewerPreference(key, value);
}
|
public void close() throws IOException, DocumentException {
if (!hasSignature) {
stamper.close(moreInfo);
return;
}
sigApp.preClose();
PdfSigGenericPKCS sig = sigApp.getSigStandard();
PdfLiteral lit = (PdfLiteral)sig.get(PdfName.CONTENTS);
int totalBuf = (lit.getPosLength() - 2) / 2;
byte buf[] = new byte[8192];
int n;
InputStream inp = sigApp.getRangeStream();
try {
while ((n = inp.read(buf)) > 0) {
sig.getSigner().update(buf, 0, n);
}
}
catch (SignatureException se) {
throw new ExceptionConverter(se);
}
buf = new byte[totalBuf];
byte[] bsig = sig.getSignerContents();
System.arraycopy(bsig, 0, buf, 0, bsig.length);
PdfString str = new PdfString(buf);
str.setHexWriting(true);
PdfDictionary dic = new PdfDictionary();
dic.put(PdfName.CONTENTS, str);
sigApp.close(dic);
stamper.reader.close();
}
Closes the document. No more content can be written after the
document is closed.
If closing a signed document with an external signature the closing must be done
in the PdfSignatureAppearance instance. |
public static PdfStamper createSignature(PdfReader reader,
OutputStream os,
char pdfVersion) throws IOException, DocumentException {
return createSignature(reader, os, pdfVersion, null, false);
}
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();
|
public static PdfStamper createSignature(PdfReader reader,
OutputStream os,
char pdfVersion,
File tempFile) throws IOException, DocumentException {
return createSignature(reader, os, pdfVersion, tempFile, false);
}
Applies a digital signature to a document. The returned PdfStamper
can be used normally as the signature is only applied when closing.
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();
|
public static PdfStamper createSignature(PdfReader reader,
OutputStream os,
char pdfVersion,
File tempFile,
boolean append) throws IOException, DocumentException {
PdfStamper stp;
if (tempFile == null) {
ByteBuffer bout = new ByteBuffer();
stp = new PdfStamper(reader, bout, pdfVersion, append);
stp.sigApp = new PdfSignatureAppearance(stp.stamper);
stp.sigApp.setSigout(bout);
}
else {
if (tempFile.isDirectory())
tempFile = File.createTempFile("pdf", null, tempFile);
FileOutputStream fout = new FileOutputStream(tempFile);
stp = new PdfStamper(reader, fout, pdfVersion, append);
stp.sigApp = new PdfSignatureAppearance(stp.stamper);
stp.sigApp.setTempFile(tempFile);
}
stp.sigApp.setOriginalout(os);
stp.sigApp.setStamper(stp);
stp.hasSignature = true;
PdfDictionary catalog = reader.getCatalog();
PdfDictionary acroForm = (PdfDictionary)PdfReader.getPdfObject(catalog.get(PdfName.ACROFORM), catalog);
if (acroForm != null) {
acroForm.remove(PdfName.NEEDAPPEARANCES);
stp.stamper.markUsed(acroForm);
}
return stp;
}
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();
|
public AcroFields getAcroFields() {
return stamper.getAcroFields();
}
Gets the AcroFields object that allows to get and set field values
and to merge FDF forms. |
public PdfImportedPage getImportedPage(PdfReader reader,
int pageNumber) {
return stamper.getImportedPage(reader, pageNumber);
}
Gets a page from other PDF document. Note that calling this method more than
once with the same parameters will retrieve the same object. |
public HashMap getMoreInfo() {
return this.moreInfo;
}
Gets the optional String map to add or change values in
the info dictionary. |
public PdfContentByte getOverContent(int pageNum) {
return stamper.getOverContent(pageNum);
}
Gets a PdfContentByte to write over the page of
the original document. |
public Map getPdfLayers() {
return stamper.getPdfLayers();
}
Gets the PdfLayer objects in an existing document as a Map
with the names/titles of the layers as keys. |
public PdfReader getReader() {
return stamper.reader;
}
Gets the underlying PdfReader. |
public PdfSignatureAppearance getSignatureAppearance() {
return sigApp;
}
Gets the signing instance. The appearances and other parameters can the be set. |
public PdfContentByte getUnderContent(int pageNum) {
return stamper.getUnderContent(pageNum);
}
Gets a PdfContentByte to write under the page of
the original document. |
public PdfWriter getWriter() {
return stamper;
}
Gets the underlying PdfWriter. |
public void insertPage(int pageNumber,
Rectangle mediabox) {
stamper.insertPage(pageNumber, mediabox);
}
Inserts a blank page. All the pages above and including pageNumber will
be shifted up. If pageNumber is bigger than the total number of pages
the new page will be the last one. |
public boolean isFullCompression() {
return stamper.isFullCompression();
}
Gets the 1.5 compression status. |
public boolean isRotateContents() {
return stamper.isRotateContents();
}
Checks if the content is automatically adjusted to compensate
the original page rotation. |
public void makePackage(PdfName initialView) {
PdfCollection collection = new PdfCollection(0);
collection.put(PdfName.VIEW, initialView);
stamper.makePackage( collection );
}
This is the most simple way to change a PDF into a
portable collection. Choose one of the following names:
- PdfName.D (detailed view)
- PdfName.T (tiled view)
- PdfName.H (hidden)
Pass this name as a parameter and your PDF will be
a portable collection with all the embedded and
attached files as entries. |
public void makePackage(PdfCollection collection) {
stamper.makePackage(collection);
}
Adds or replaces the Collection Dictionary in the Catalog. |
public boolean partialFormFlattening(String name) {
return stamper.partialFormFlattening(name);
}
|
public void replacePage(PdfReader r,
int pageImported,
int pageReplaced) {
stamper.replacePage(r, pageImported, pageReplaced);
}
Replaces a page from this document with a page from other document. Only the content
is replaced not the fields and annotations. This method must be called before
getOverContent() or getUndercontent() are called for the same page. |
public void setDuration(int seconds,
int page) {
stamper.setDuration(seconds, page);
}
Sets the display duration for the page (for presentations) |
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);
}
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 |
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. |
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. |
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. |
public void setEncryption(int encryptionType,
String userPassword,
String ownerPassword,
int permissions) throws DocumentException {
setEncryption(DocWriter.getISOBytes(userPassword), DocWriter.getISOBytes(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. |
public void setFormFlattening(boolean flat) {
stamper.setFormFlattening(flat);
}
|
public void setFreeTextFlattening(boolean flat) {
stamper.setFreeTextFlattening(flat);
}
Determines if the FreeText annotations are flattened on close. |
public void setFullCompression() {
if (stamper.isAppend())
return;
stamper.setFullCompression();
}
Sets the document's compression to the new 1.5 mode with object streams and xref
streams. It can be set at any time but once set it can't be unset. |
public void setMoreInfo(HashMap moreInfo) {
this.moreInfo = moreInfo;
}
An optional String map to add or change values in
the info dictionary. Entries with null
values delete the key in the original info dictionary |
public void setOutlines(List outlines) {
stamper.setOutlines(outlines);
}
|
public void setPageAction(PdfName actionType,
PdfAction action,
int page) throws PdfException {
stamper.setPageAction(actionType, action, page);
}
Sets the open and close page additional action. |
public void setRotateContents(boolean rotateContents) {
stamper.setRotateContents(rotateContents);
}
Flags the content to be automatically adjusted to compensate
the original page rotation. The default is true. |
public void setThumbnail(Image image,
int page) throws PdfException, DocumentException {
stamper.setThumbnail(image, page);
}
Sets the thumbnail image for a page. |
public void setTransition(PdfTransition transition,
int page) {
stamper.setTransition(transition, page);
}
Sets the transition for the page |
public void setViewerPreferences(int preferences) {
stamper.setViewerPreferences(preferences);
}
Sets the viewer preferences. |
public void setXmpMetadata(byte[] xmp) {
stamper.setXmpMetadata(xmp);
}
|