📄 pdfstamper.java
字号:
* Pass this name as a parameter and your PDF will be
* a portable collection with all the embedded and
* attached files as entries.
* @param initialView can be PdfName.D, PdfName.T or PdfName.H
*/
public void makePackage( PdfName initialView ) {
PdfCollection collection = new PdfCollection(0);
collection.put(PdfName.VIEW, initialView);
stamper.makePackage( collection );
}
/**
* Adds or replaces the Collection Dictionary in the Catalog.
* @param collection the new collection dictionary.
*/
public void makePackage(PdfCollection collection) {
stamper.makePackage(collection);
}
/**
* Sets the viewer preferences.
* @param preferences the viewer preferences
* @see PdfViewerPreferences#setViewerPreferences(int)
*/
public void setViewerPreferences(int preferences) {
stamper.setViewerPreferences(preferences);
}
/** Adds a viewer preference
* @param key a key for a viewer preference
* @param value the value for the viewer preference
* @see PdfViewerPreferences#addViewerPreference
*/
public void addViewerPreference(PdfName key, PdfObject value) {
stamper.addViewerPreference(key, value);
}
/**
* Sets the XMP metadata.
* @param xmp
* @see PdfWriter#setXmpMetadata(byte[])
*/
public void setXmpMetadata(byte[] xmp) {
stamper.setXmpMetadata(xmp);
}
/**
* Gets the 1.5 compression status.
* @return <code>true</code> if the 1.5 compression is on
*/
public boolean isFullCompression() {
return stamper.isFullCompression();
}
/**
* 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 setFullCompression() {
if (stamper.isAppend())
return;
stamper.setFullCompression();
}
/**
* Sets the open and close page additional action.
* @param actionType the action type. It can be <CODE>PdfWriter.PAGE_OPEN</CODE>
* or <CODE>PdfWriter.PAGE_CLOSE</CODE>
* @param action the action to perform
* @param page the page where the action will be applied. The first page is 1
* @throws PdfException if the action type is invalid
*/
public void setPageAction(PdfName actionType, PdfAction action, int page) throws PdfException {
stamper.setPageAction(actionType, action, page);
}
/**
* Sets the display duration for the page (for presentations)
* @param seconds the number of seconds to display the page. A negative value removes the entry
* @param page the page where the duration will be applied. The first page is 1
*/
public void setDuration(int seconds, int page) {
stamper.setDuration(seconds, page);
}
/**
* Sets the transition for the page
* @param transition the transition object. A <code>null</code> removes the transition
* @param page the page where the transition will be applied. The first page is 1
*/
public void setTransition(PdfTransition transition, int page) {
stamper.setTransition(transition, page);
}
/**
* Applies a digital signature to a document, possibly as a new revision, making
* possible multiple signatures. The returned PdfStamper
* can be used normally as the signature is only applied when closing.
* <p>
* A possible use for adding a signature without invalidating an existing one is:
* <p>
* <pre>
* 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();
* </pre>
* @param reader the original document
* @param os the output stream or <CODE>null</CODE> to keep the document in the temporary file
* @param pdfVersion the new pdf version or '\0' to keep the same version as the original
* document
* @param 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 <CODE>os</CODE> is null.
* In that case the document can be retrieved directly from the temporary file. If it's <CODE>null</CODE>
* no temporary file will be created and memory will be used
* @param append if <CODE>true</CODE> the signature and all the other content will be added as a
* new revision thus not invalidating existing signatures
* @return a <CODE>PdfStamper</CODE>
* @throws DocumentException on error
* @throws IOException on error
*/
public static PdfStamper createSignature(PdfReader reader, OutputStream os, char pdfVersion, File tempFile, boolean append) throws DocumentException, IOException {
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;
}
/**
* Applies a digital signature to a document. The returned PdfStamper
* can be used normally as the signature is only applied when closing.
* <p>
* Note that the pdf is created in memory.
* <p>
* A possible use is:
* <p>
* <pre>
* 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();
* </pre>
* @param reader the original document
* @param os the output stream
* @param pdfVersion the new pdf version or '\0' to keep the same version as the original
* document
* @throws DocumentException on error
* @throws IOException on error
* @return a <CODE>PdfStamper</CODE>
*/
public static PdfStamper createSignature(PdfReader reader, OutputStream os, char pdfVersion) throws DocumentException, IOException {
return createSignature(reader, os, pdfVersion, null, false);
}
/**
* Applies a digital signature to a document. The returned PdfStamper
* can be used normally as the signature is only applied when closing.
* <p>
* A possible use is:
* <p>
* <pre>
* 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();
* </pre>
* @param reader the original document
* @param os the output stream or <CODE>null</CODE> to keep the document in the temporary file
* @param pdfVersion the new pdf version or '\0' to keep the same version as the original
* document
* @param 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 <CODE>os</CODE> is null.
* In that case the document can be retrieved directly from the temporary file. If it's <CODE>null</CODE>
* no temporary file will be created and memory will be used
* @return a <CODE>PdfStamper</CODE>
* @throws DocumentException on error
* @throws IOException on error
*/
public static PdfStamper createSignature(PdfReader reader, OutputStream os, char pdfVersion, File tempFile) throws DocumentException, IOException
{
return createSignature(reader, os, pdfVersion, tempFile, false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -