You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2015/10/08 17:21:14 UTC
svn commit: r1707569 -
/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java
Author: tilman
Date: Thu Oct 8 15:21:08 2015
New Revision: 1707569
URL: http://svn.apache.org/viewvc?rev=1707569&view=rev
Log:
PDFBOX-3010, PDFBOX-3011: Do not copy source file; do not close options; make code more readable
Modified:
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java?rev=1707569&r1=1707568&r2=1707569&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java Thu Oct 8 15:21:08 2015
@@ -32,6 +32,7 @@ import java.security.cert.CertificateExc
import java.security.interfaces.RSAPrivateKey;
import java.util.Calendar;
import java.util.Enumeration;
+import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
@@ -97,38 +98,29 @@ public class CreateVisibleSignature impl
}
/**
- * Signs the given pdf file.
+ * Sign pdf file and create new file that ends with "_signed.pdf".
*
- * @param document is the pdf document
- * @param signatureProperties
- * @return the signed pdf document
+ * @param documentFile The source pdf document file.
+ * @param signatureProperties The signature properties.
+ * @return the signed pdf document file.
* @throws IOException
*/
- public File signPDF(File document, PDVisibleSigProperties signatureProperties) throws IOException
+ public File signPDF(File documentFile, PDVisibleSigProperties signatureProperties) throws IOException
{
- byte[] buffer = new byte[8 * 1024];
- if (document == null || !document.exists())
+ if (documentFile == null || !documentFile.exists())
{
throw new IOException("Document for signing does not exist");
}
// creating output document and prepare the IO streams.
- String name = document.getName();
+ String name = documentFile.getName();
String substring = name.substring(0, name.lastIndexOf('.'));
- File outputDocument = new File(document.getParent(), substring + "_signed.pdf");
- FileInputStream fis = new FileInputStream(document);
- FileOutputStream fos = new FileOutputStream(outputDocument);
-
- int c;
- while ((c = fis.read(buffer)) != -1)
- {
- fos.write(buffer, 0, c);
- }
- fis.close();
+ File outputDocumentFile = new File(documentFile.getParent(), substring + "_signed.pdf");
+ FileOutputStream fos = new FileOutputStream(outputDocumentFile);
// load document
- PDDocument doc = PDDocument.load(document);
+ PDDocument doc = PDDocument.load(documentFile);
// create signature dictionary
PDSignature signature = new PDSignature();
@@ -143,22 +135,11 @@ public class CreateVisibleSignature impl
signature.setSignDate(Calendar.getInstance());
// register signature dictionary and sign interface
-
if (signatureProperties != null && signatureProperties.isVisualSignEnabled())
{
- try
- {
- options = new SignatureOptions();
- options.setVisualSignature(signatureProperties);
- doc.addSignature(signature, this, options);
- }
- finally
- {
- if (options != null)
- {
- options.close();
- }
- }
+ options = new SignatureOptions();
+ options.setVisualSignature(signatureProperties);
+ doc.addSignature(signature, this, options);
}
else
{
@@ -167,8 +148,13 @@ public class CreateVisibleSignature impl
// write incremental (only for signing purpose)
doc.saveIncremental(fos);
+ doc.close();
+
+ // do not close options before saving, because some COSStream objects within options
+ // are transferred to the signed document.
+ IOUtils.closeQuietly(options);
- return outputDocument;
+ return outputDocumentFile;
}
/**