You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ja...@apache.org on 2014/03/11 10:13:14 UTC
svn commit: r1576239 - in /pdfbox/trunk/examples/src:
main/java/org/apache/pdfbox/examples/fdf/
main/java/org/apache/pdfbox/examples/pdmodel/
main/java/org/apache/pdfbox/examples/persistence/
main/java/org/apache/pdfbox/examples/signature/ test/java/or...
Author: jahewson
Date: Tue Mar 11 09:13:13 2014
New Revision: 1576239
URL: http://svn.apache.org/r1576239
Log:
PDFBOX-1973: [Fix] Remove COSVisitorException from examples
Added:
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CMSProcessableInputStream.java
Modified:
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBlankPDF.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateLandscapePDF.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorld.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStampWithImage.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowColorBoxes.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/UsingTextMatrix.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/CopyDoc.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java
pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestRubberStampWithImage.java
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java Tue Mar 11 09:13:13 2014
@@ -17,8 +17,10 @@
package org.apache.pdfbox.examples.fdf;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
-import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
@@ -65,15 +67,16 @@ public class SetField
* @param args command line arguments
*
* @throws IOException If there is an error importing the FDF document.
- * @throws COSVisitorException If there is an error writing the PDF.
*/
- public static void main(String[] args) throws IOException, COSVisitorException
+ public static void main(String[] args)
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
SetField setter = new SetField();
setter.setField(args);
}
- private void setField(String[] args) throws IOException, COSVisitorException
+ private void setField(String[] args)
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
PDDocument pdf = null;
try
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java Tue Mar 11 09:13:13 2014
@@ -19,8 +19,10 @@ package org.apache.pdfbox.examples.pdmod
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
-import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.io.RandomAccessFile;
import org.apache.pdfbox.pdmodel.PDDocument;
@@ -50,10 +52,9 @@ public class AddImageToPDF
* @param outputFile The file to write to the pdf to.
*
* @throws IOException If there is an error writing the data.
- * @throws COSVisitorException If there is an error writing the PDF.
*/
- public void createPDFFromImage( String inputFile, String image, String outputFile )
- throws IOException, COSVisitorException
+ public void createPDFFromImage( String inputFile, String image, String outputFile )
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
// the document
PDDocument doc = null;
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.java Tue Mar 11 09:13:13 2014
@@ -17,10 +17,11 @@
package org.apache.pdfbox.examples.pdmodel;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
import java.util.List;
-import org.apache.pdfbox.exceptions.COSVisitorException;
-
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
@@ -30,7 +31,6 @@ import org.apache.pdfbox.pdmodel.edit.PD
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
-
/**
* This is an example of how to add a message to every page
* in a pdf document.
@@ -56,9 +56,9 @@ public class AddMessageToEachPage
* @param outfile The resulting PDF.
*
* @throws IOException If there is an error writing the data.
- * @throws COSVisitorException If there is an error writing the PDF.
*/
- public void doIt( String file, String message, String outfile ) throws IOException, COSVisitorException
+ public void doIt( String file, String message, String outfile )
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
// the document
PDDocument doc = null;
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBlankPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBlankPDF.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBlankPDF.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBlankPDF.java Tue Mar 11 09:13:13 2014
@@ -17,9 +17,10 @@
package org.apache.pdfbox.examples.pdmodel;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
-import org.apache.pdfbox.exceptions.COSVisitorException;
-
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
@@ -40,9 +41,9 @@ public class CreateBlankPDF
* @param file The name of the file to write to.
*
* @throws IOException If there is an error writing the data.
- * @throws COSVisitorException If there is an error while generating the document.
*/
- public void create( String file ) throws IOException, COSVisitorException
+ public void create( String file )
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
PDDocument document = null;
try
@@ -69,9 +70,9 @@ public class CreateBlankPDF
* @param args The command line arguments.
*
* @throws IOException If there is an error writing the document data.
- * @throws COSVisitorException If there is an error generating the data.
*/
- public static void main( String[] args ) throws IOException, COSVisitorException
+ public static void main( String[] args )
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
if( args.length != 1 )
{
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateLandscapePDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateLandscapePDF.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateLandscapePDF.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateLandscapePDF.java Tue Mar 11 09:13:13 2014
@@ -17,8 +17,10 @@
package org.apache.pdfbox.examples.pdmodel;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
-import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
@@ -26,7 +28,6 @@ import org.apache.pdfbox.pdmodel.edit.PD
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
-
/**
* This is an example of how to create a page with a landscape orientation.
* @version $Revision: 1.0 $
@@ -48,9 +49,9 @@ public class CreateLandscapePDF
* @param outfile The resulting PDF.
*
* @throws IOException If there is an error writing the data.
- * @throws COSVisitorException If there is an error writing the PDF.
*/
- public void doIt( String message, String outfile ) throws IOException, COSVisitorException
+ public void doIt( String message, String outfile )
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
// the document
PDDocument doc = null;
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java Tue Mar 11 09:13:13 2014
@@ -18,13 +18,14 @@ package org.apache.pdfbox.examples.pdmod
import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.List;
-import org.apache.pdfbox.exceptions.COSVisitorException;
-
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
@@ -37,7 +38,6 @@ import org.apache.pdfbox.pdmodel.edit.PD
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
-
/**
* This is an example that creates a simple document and embeds a file into it..
*
@@ -60,9 +60,9 @@ public class EmbeddedFiles
* @param file The file to write the PDF to.
*
* @throws IOException If there is an error writing the data.
- * @throws COSVisitorException If there is an error writing the PDF.
*/
- public void doIt( String file) throws IOException, COSVisitorException
+ public void doIt( String file)
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
// the document
PDDocument doc = null;
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorld.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorld.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorld.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorld.java Tue Mar 11 09:13:13 2014
@@ -17,9 +17,10 @@
package org.apache.pdfbox.examples.pdmodel;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
-import org.apache.pdfbox.exceptions.COSVisitorException;
-
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
@@ -28,7 +29,6 @@ import org.apache.pdfbox.pdmodel.edit.PD
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
-
/**
* This is an example that creates a simple document.
*
@@ -54,9 +54,9 @@ public class HelloWorld
* @param message The message to write in the file.
*
* @throws IOException If there is an error writing the data.
- * @throws COSVisitorException If there is an error writing the PDF.
*/
- public void doIt( String file, String message) throws IOException, COSVisitorException
+ public void doIt( String file, String message)
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
// the document
PDDocument doc = null;
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java Tue Mar 11 09:13:13 2014
@@ -18,8 +18,10 @@
package org.apache.pdfbox.examples.pdmodel;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
-import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
@@ -35,7 +37,6 @@ import org.apache.pdfbox.pdmodel.font.PD
*/
public class HelloWorldTTF
{
-
/**
* create the second sample document from the PDF file format specification.
*
@@ -44,10 +45,9 @@ public class HelloWorldTTF
* @param fontfile The ttf-font file.
*
* @throws IOException If there is an error writing the data.
- * @throws COSVisitorException If there is an error writing the PDF.
*/
- public void doIt(final String file, final String message,
- final String fontfile) throws IOException, COSVisitorException
+ public void doIt(final String file, final String message, final String fontfile)
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
// the document
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java Tue Mar 11 09:13:13 2014
@@ -18,8 +18,10 @@
package org.apache.pdfbox.examples.pdmodel;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
-import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
@@ -44,10 +46,9 @@ public class HelloWorldType1AfmPfb
* @param fontfile The ttf-font file.
*
* @throws IOException If there is an error writing the data.
- * @throws COSVisitorException If there is an error writing the PDF.
*/
- public void doIt(final String file, final String message,
- final String fontfile) throws IOException, COSVisitorException
+ public void doIt(String file, String message, String fontfile)
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
// the document
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java Tue Mar 11 09:13:13 2014
@@ -19,8 +19,10 @@ package org.apache.pdfbox.examples.pdmod
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
-import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.io.RandomAccessFile;
import org.apache.pdfbox.pdmodel.PDDocument;
@@ -49,9 +51,9 @@ public class ImageToPDF
* @param image The filename of the image to put in the PDF.
*
* @throws IOException If there is an error writing the data.
- * @throws COSVisitorException If there is an error writing the PDF.
*/
- public void createPDFFromImage( String file, String image) throws IOException, COSVisitorException
+ public void createPDFFromImage( String file, String image)
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
// the document
PDDocument doc = null;
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStampWithImage.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStampWithImage.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStampWithImage.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStampWithImage.java Tue Mar 11 09:13:13 2014
@@ -17,7 +17,8 @@
package org.apache.pdfbox.examples.pdmodel;
import org.apache.pdfbox.cos.COSDictionary;
-import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
@@ -33,8 +34,8 @@ import org.apache.pdfbox.pdmodel.interac
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
+import java.security.NoSuchAlgorithmException;
import java.text.NumberFormat;
-import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@@ -57,7 +58,8 @@ public class RubberStampWithImage
* @param args the command line arguments
* @throws IOException an exception is thrown if something went wrong
*/
- public void doIt( String[] args ) throws IOException
+ public void doIt( String[] args )
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
if( args.length != 3 )
{
@@ -118,11 +120,6 @@ public class RubberStampWithImage
}
document.save( args[1] );
}
- catch(COSVisitorException exception)
- {
- System.err.println("An error occured during saving the document.");
- System.err.println("Exception:"+exception);
- }
finally
{
if( document != null )
@@ -130,7 +127,7 @@ public class RubberStampWithImage
document.close();
}
}
- }
+ }
}
private void drawXObject( PDImageXObject xobject, PDResources resources, OutputStream os,
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowColorBoxes.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowColorBoxes.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowColorBoxes.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowColorBoxes.java Tue Mar 11 09:13:13 2014
@@ -18,9 +18,10 @@ package org.apache.pdfbox.examples.pdmod
import java.awt.Color;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
-import org.apache.pdfbox.exceptions.COSVisitorException;
-
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
@@ -50,9 +51,9 @@ public class ShowColorBoxes
* @param file The file to write the PDF to.
*
* @throws IOException If there is an error writing the data.
- * @throws COSVisitorException If there is an error writing the PDF.
*/
- public void doIt( String file) throws IOException, COSVisitorException
+ public void doIt( String file)
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
// the document
PDDocument doc = null;
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/UsingTextMatrix.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/UsingTextMatrix.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/UsingTextMatrix.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/UsingTextMatrix.java Tue Mar 11 09:13:13 2014
@@ -17,8 +17,10 @@
package org.apache.pdfbox.examples.pdmodel;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
-import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
@@ -26,7 +28,6 @@ import org.apache.pdfbox.pdmodel.edit.PD
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
-
/**
* This is an example of how to use a text matrix.
* @version $Revision: 1.0 $
@@ -48,9 +49,9 @@ public class UsingTextMatrix
* @param outfile The resulting PDF.
*
* @throws IOException If there is an error writing the data.
- * @throws COSVisitorException If there is an error writing the PDF.
*/
- public void doIt( String message, String outfile ) throws IOException, COSVisitorException
+ public void doIt( String message, String outfile )
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
// the document
PDDocument doc = null;
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/CopyDoc.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/CopyDoc.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/CopyDoc.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/CopyDoc.java Tue Mar 11 09:13:13 2014
@@ -17,15 +17,13 @@
package org.apache.pdfbox.examples.persistence;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
import org.apache.pdfbox.cos.COSDocument;
-
-
-
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdfparser.PDFParser;
-
import org.apache.pdfbox.pdfwriter.COSWriter;
-import org.apache.pdfbox.exceptions.COSVisitorException;
/**
* This is an example used to copy a documents contents from a source doc to destination doc
@@ -51,9 +49,9 @@ public class CopyDoc
* @param out The filename used for output.
*
* @throws IOException If there is an error parsing the document.
- * @throws COSVisitorException If there is an error while copying the document.
*/
- public void doIt(String in, String out) throws IOException, COSVisitorException
+ public void doIt(String in, String out)
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
java.io.InputStream is = null;
java.io.OutputStream os = null;
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CMSProcessableInputStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CMSProcessableInputStream.java?rev=1576239&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CMSProcessableInputStream.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CMSProcessableInputStream.java Tue Mar 11 09:13:13 2014
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.signature;
+
+import org.bouncycastle.cms.CMSException;
+import org.bouncycastle.cms.CMSProcessable;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * Wraps a InputStream into a CMSProcessable object for bouncy castle.
+ * It's an alternative to the CMSProcessableByteArray.
+ * @author Thomas Chojecki
+ */
+class CMSProcessableInputStream implements CMSProcessable
+{
+ private InputStream in;
+
+ public CMSProcessableInputStream(InputStream is)
+ {
+ in = is;
+ }
+
+ public Object getContent()
+ {
+ return null;
+ }
+
+ public void write(OutputStream out) throws IOException, CMSException
+ {
+ // read the content only one time
+ byte[] buffer = new byte[8 * 1024];
+ int read;
+ while ((read = in.read(buffer)) != -1)
+ {
+ out.write(buffer, 0, read);
+ }
+ in.close();
+ }
+}
\ No newline at end of file
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java Tue Mar 11 09:13:13 2014
@@ -18,7 +18,6 @@ package org.apache.pdfbox.examples.signa
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -38,7 +37,7 @@ import java.util.Calendar;
import java.util.Enumeration;
import java.util.List;
-import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.exceptions.CryptographyException;
import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
@@ -52,252 +51,193 @@ import org.bouncycastle.cms.CMSSignedGen
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
- * <p>This is an example for singing a pdf with bouncy castle.</p>
- * <p>A keystore can be created with the java keytool
- * (e.g. keytool -genkeypair -storepass 123456 -storetype pkcs12 -alias test -validity 365 -v -keyalg RSA -keystore keystore.p12 )
- * </p>
- *
+ * An example for singing a PDF with bouncy castle.
+ * A keystore can be created with the java keytool, for example:
+ *
+ * {@code keytool -genkeypair -storepass 123456 -storetype pkcs12 -alias test -validity 365
+ * -v -keyalg RSA -keystore keystore.p12 }
+ *
* @author Thomas Chojecki
- *
*/
public class CreateSignature implements SignatureInterface
{
+ private static BouncyCastleProvider provider = new BouncyCastleProvider();
- private static BouncyCastleProvider provider = new BouncyCastleProvider();
+ private PrivateKey privKey;
+ private Certificate[] cert;
+ private SignatureOptions options;
+
+ /**
+ * Initialize the signature creator with a keystore (pkcs12) and pin that
+ * should be used for the signature.
+ *
+ * @param keystore is a pkcs12 keystore.
+ * @param pin is the pin for the keystore / private key
+ */
+ public CreateSignature(KeyStore keystore, char[] pin)
+ {
+ try
+ {
+ // grabs the first alias from the keystore and get the private key. An
+ // alternative method or constructor could be used for setting a specific
+ // alias that should be used.
+ Enumeration<String> aliases = keystore.aliases();
+ String alias = null;
+ if (aliases.hasMoreElements())
+ {
+ alias = aliases.nextElement();
+ }
+ else
+ {
+ throw new RuntimeException("Could not find alias");
+ }
+ privKey = (PrivateKey) keystore.getKey(alias, pin);
+ cert = keystore.getCertificateChain(alias);
+ }
+ catch (KeyStoreException e)
+ {
+ e.printStackTrace();
+ }
+ catch (UnrecoverableKeyException e)
+ {
+ System.err.println("Could not extract private key.");
+ e.printStackTrace();
+ }
+ catch (NoSuchAlgorithmException e)
+ {
+ System.err.println("Unknown algorithm.");
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Signs the given pdf file.
+ *
+ * @param document is the pdf document
+ * @return the signed pdf document
+ * @throws IOException
+ * @throws SignatureException
+ */
+ public File signPDF(File document)
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
+ {
+ byte[] buffer = new byte[8 * 1024];
+ if (document == null || !document.exists())
+ {
+ new RuntimeException("Document for signing does not exist");
+ }
+
+ // creating output document and prepare the IO streams.
+ String name = document.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();
+ fis = new FileInputStream(outputDocument);
+
+ // load document
+ PDDocument doc = PDDocument.load(document);
+
+ // create signature dictionary
+ PDSignature signature = new PDSignature();
+ signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); // default filter
+ // subfilter for basic and PAdES Part 2 signatures
+ signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
+ signature.setName("signer name");
+ signature.setLocation("signer location");
+ signature.setReason("reason for signature");
+
+ // the signing date, needed for valid signature
+ signature.setSignDate(Calendar.getInstance());
+
+ // register signature dictionary and sign interface
+ if (options==null)
+ {
+ doc.addSignature(signature, this);
+ }
+ else
+ {
+ doc.addSignature(signature, this, options);
+ }
+
+ // write incremental (only for signing purpose)
+ doc.saveIncremental(fis, fos);
+
+ return outputDocument;
+ }
+
+ /**
+ * SignatureInterface implementation.
+ *
+ * This method will be called from inside of the pdfbox and create the pkcs7 signature.
+ * The given InputStream contains the bytes that are given by the byte range.
+ *
+ * This method is for internal use only. <-- TODO this method should be private
+ *
+ * Use your favorite cryptographic library to implement pkcs7 signature creation.
+ */
+ public byte[] sign(InputStream content) throws SignatureException,
+ IOException
+ {
+ CMSProcessableInputStream input = new CMSProcessableInputStream(content);
+ CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
+ // CertificateChain
+ List<Certificate> certList = Arrays.asList(cert);
+
+ CertStore certStore = null;
+ try
+ {
+ certStore = CertStore.getInstance("Collection",
+ new CollectionCertStoreParameters(certList), provider);
+ gen.addSigner(privKey, (X509Certificate) certList.get(0),
+ CMSSignedGenerator.DIGEST_SHA256);
+ gen.addCertificatesAndCRLs(certStore);
+ CMSSignedData signedData = gen.generate(input, false, provider);
+ return signedData.getEncoded();
+ }
+ catch (Exception e)
+ {
+ // should be handled
+ System.err.println("Error while creating pkcs7 signature.");
+ e.printStackTrace();
+ }
+ throw new RuntimeException("Problem while preparing signature");
+ }
+
+ public static void main(String[] args) throws KeyStoreException, CertificateException,
+ IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
+ {
+ if (args.length != 3)
+ {
+ usage();
+ System.exit(1);
+ }
+ else
+ {
+ File ksFile = new File(args[0]);
+ KeyStore keystore = KeyStore.getInstance("PKCS12", provider);
+ char[] pin = args[1].toCharArray();
+ keystore.load(new FileInputStream(ksFile), pin);
+
+ File document = new File(args[2]);
+
+ CreateSignature signing = new CreateSignature(keystore, pin.clone());
+ signing.signPDF(document);
+ }
- private PrivateKey privKey;
-
- private Certificate[] cert;
-
- private SignatureOptions options;
-
- /**
- * Initialize the signature creator with a keystore (pkcs12) and pin that
- * should be used for the signature.
- *
- * @param keystore
- * is a pkcs12 keystore.
- * @param pin
- * is the pin for the keystore / private key
- */
- public CreateSignature(KeyStore keystore, char[] pin)
- {
- try
- {
- /*
- * grabs the first alias from the keystore and get the private key. An
- * alternative method or constructor could be used for setting a specific
- * alias that should be used.
- */
- Enumeration<String> aliases = keystore.aliases();
- String alias = null;
- if (aliases.hasMoreElements())
- {
- alias = aliases.nextElement();
- }
- else
- {
- throw new RuntimeException("Could not find alias");
- }
- privKey = (PrivateKey) keystore.getKey(alias, pin);
- cert = keystore.getCertificateChain(alias);
- }
- catch (KeyStoreException e)
- {
- e.printStackTrace();
- }
- catch (UnrecoverableKeyException e)
- {
- System.err.println("Could not extract private key.");
- e.printStackTrace();
- }
- catch (NoSuchAlgorithmException e)
- {
- System.err.println("Unknown algorithm.");
- e.printStackTrace();
- }
- }
-
-
-
- /**
- * Signs the given pdf file.
- *
- * @param document is the pdf document
- * @return the signed pdf document
- * @throws IOException
- * @throws COSVisitorException
- * @throws SignatureException
- */
- public File signPDF(File document) throws IOException, COSVisitorException,
- SignatureException
- {
- byte[] buffer = new byte[8 * 1024];
- if (document == null || !document.exists())
- {
- new RuntimeException("Document for signing does not exist");
- }
-
- // creating output document and prepare the IO streams.
- String name = document.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();
- fis = new FileInputStream(outputDocument);
-
- // load document
- PDDocument doc = PDDocument.load(document);
-
- // create signature dictionary
- PDSignature signature = new PDSignature();
- signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); // default filter
- // subfilter for basic and PAdES Part 2 signatures
- signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
- signature.setName("signer name");
- signature.setLocation("signer location");
- signature.setReason("reason for signature");
-
- // the signing date, needed for valid signature
- signature.setSignDate(Calendar.getInstance());
-
- // register signature dictionary and sign interface
- if (options==null)
- {
- doc.addSignature(signature, this);
- }
- else
- {
- doc.addSignature(signature, this, options);
}
-
- // write incremental (only for signing purpose)
- doc.saveIncremental(fis, fos);
-
- return outputDocument;
- }
-
- /**
- * <p>
- * SignatureInterface implementation.
- * </p>
- *
- * <p>
- * This method will be called from inside of the pdfbox and create the pkcs7
- * signature. The given InputStream contains the bytes that are providen by
- * the byte range.
- * </p>
- *
- * <p>
- * This method is for internal use only.
- * </p>
- *
- * <p>
- * Here the user should use his favorite cryptographic library and implement a
- * pkcs7 signature creation.
- * </p>
- */
- public byte[] sign(InputStream content) throws SignatureException,
- IOException
- {
- CMSProcessableInputStream input = new CMSProcessableInputStream(content);
- CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
- // CertificateChain
- List<Certificate> certList = Arrays.asList(cert);
-
- CertStore certStore = null;
- try
- {
- certStore = CertStore.getInstance("Collection",
- new CollectionCertStoreParameters(certList), provider);
- gen.addSigner(privKey, (X509Certificate) certList.get(0),
- CMSSignedGenerator.DIGEST_SHA256);
- gen.addCertificatesAndCRLs(certStore);
- CMSSignedData signedData = gen.generate(input, false, provider);
- return signedData.getEncoded();
- }
- catch (Exception e)
- {
- // should be handled
- System.err.println("Error while creating pkcs7 signature.");
- e.printStackTrace();
- }
- throw new RuntimeException("Problem while preparing signature");
- }
-
- public static void main(String[] args) throws KeyStoreException,
- NoSuchAlgorithmException, CertificateException, FileNotFoundException,
- IOException, COSVisitorException, SignatureException
- {
- if (args.length != 3)
- {
- usage();
- System.exit(1);
- }
- else
- {
- File ksFile = new File(args[0]);
- KeyStore keystore = KeyStore.getInstance("PKCS12", provider);
- char[] pin = args[1].toCharArray();
- keystore.load(new FileInputStream(ksFile), pin);
-
- File document = new File(args[2]);
-
- CreateSignature signing = new CreateSignature(keystore, pin.clone());
- signing.signPDF(document);
- }
-
- }
-
- /**
- * This will print the usage for this program.
- */
- private static void usage()
- {
- System.err.println("Usage: java " + CreateSignature.class.getName()
- + " <pkcs12-keystore-file> <pin> <input-pdf>");
- }
-}
-
-/**
- * Wrap a InputStream into a CMSProcessable object for bouncy castle. It's an
- * alternative to the CMSProcessableByteArray.
- *
- * @author Thomas Chojecki
- *
- */
-class CMSProcessableInputStream implements CMSProcessable
-{
-
- InputStream in;
- public CMSProcessableInputStream(InputStream is)
- {
- in = is;
- }
-
- public Object getContent()
- {
- return null;
- }
-
- public void write(OutputStream out) throws IOException, CMSException
- {
- // read the content only one time
- byte[] buffer = new byte[8 * 1024];
- int read;
- while ((read = in.read(buffer)) != -1)
+ private static void usage()
{
- out.write(buffer, 0, read);
+ System.err.println("Usage: java " + CreateSignature.class.getName()
+ + " <pkcs12-keystore-file> <pin> <input-pdf>");
}
- in.close();
- }
}
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=1576239&r1=1576238&r2=1576239&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 Tue Mar 11 09:13:13 2014
@@ -37,7 +37,7 @@ import java.util.Calendar;
import java.util.Enumeration;
import java.util.List;
-import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.exceptions.CryptographyException;
import org.apache.pdfbox.exceptions.SignatureException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
@@ -51,216 +51,215 @@ import org.bouncycastle.cms.CMSSignedGen
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
- * <p>
- * This is an example for signing a pdf with bouncy castle.
- * </p>
- * <p>
- * And also you can create visible signature too
- * </p>
- * <p>
- * A keystore can be created with the java keytool (e.g. keytool -genkeypair -storepass 123456 -storetype pkcs12 -alias
- * test -validity 365 -v -keyalg RSA -keystore keystore.p12 )
- * </p>
- *
- * @author Vakhtang koroghlishvili (Gogebashvili)
+ * This is an example for visual signing a pdf with bouncy castle.
+ *
+ * {@see org.apache.pdfbox.examples.signature.CreateSignature}
+ * @author Vakhtang Koroghlishvili
*/
public class CreateVisibleSignature implements SignatureInterface
{
+ private static BouncyCastleProvider provider = new BouncyCastleProvider();
- private static BouncyCastleProvider provider = new BouncyCastleProvider();
-
- private PrivateKey privKey;
-
- private Certificate[] cert;
-
- private SignatureOptions options;
-
- /**
- * Initialize the signature creator with a keystore (pkcs12) and pin that
- * should be used for the signature.
- *
- * @param keystore
- * is a pkcs12 keystore.
- * @param pin
- * is the pin for the keystore / private key
- */
- public CreateVisibleSignature(KeyStore keystore, char[] pin)
- {
- try {
- /*
- * grabs the first alias from the keystore and get the private key. An
- * alternative method or constructor could be used for setting a specific
- * alias that should be used.
- */
- Enumeration<String> aliases = keystore.aliases();
- String alias = null;
- if (aliases.hasMoreElements()) {
- alias = aliases.nextElement();
- } else {
- throw new RuntimeException("Could not find alias");
- }
- privKey = (PrivateKey) keystore.getKey(alias, pin);
- cert = keystore.getCertificateChain(alias);
- } catch (KeyStoreException e) {
- e.printStackTrace();
- } catch (UnrecoverableKeyException e) {
- System.err.println("Could not extract private key.");
- e.printStackTrace();
- } catch (NoSuchAlgorithmException e) {
- System.err.println("Unknown algorithm.");
- e.printStackTrace();
- }
- }
-
- /**
- * Signs the given pdf file.
- *
- * @param document is the pdf document
- * @param signatureProperties
- * @return the signed pdf document
- * @throws IOException
- * @throws COSVisitorException
- * @throws SignatureException
- */
- public File signPDF(File document, PDVisibleSigProperties signatureProperties) throws IOException,
- COSVisitorException, SignatureException
- {
- byte[] buffer = new byte[8 * 1024];
- if (document == null || !document.exists()) {
- new RuntimeException("Document for signing does not exist");
- }
-
- // creating output document and prepare the IO streams.
- String name = document.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();
- fis = new FileInputStream(outputDocument);
-
- // load document
- PDDocument doc = PDDocument.load(document);
-
- // create signature dictionary
- PDSignature signature = new PDSignature();
- signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); // default filter
- // subfilter for basic and PAdES Part 2 signatures
- signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
- signature.setName("signer name");
- signature.setLocation("signer location");
- signature.setReason("reason for signature");
-
- // the signing date, needed for valid signature
- signature.setSignDate(Calendar.getInstance());
-
- // register signature dictionary and sign interface
-
- if (signatureProperties != null && signatureProperties.isVisualSignEnabled()) {
- options = new SignatureOptions();
- options.setVisualSignature(signatureProperties);
- // options.setPage(signatureProperties.getPage());
- // options.setPreferedSignatureSize(signatureProperties.getPreferredSize());
- doc.addSignature(signature, this, options);
- } else {
- doc.addSignature(signature, this);
+ private PrivateKey privKey;
+ private Certificate[] cert;
+ private SignatureOptions options;
+
+ /**
+ * Initialize the signature creator with a keystore (pkcs12) and pin that
+ * should be used for the signature.
+ *
+ * @param keystore is a pkcs12 keystore.
+ * @param pin is the pin for the keystore / private key
+ */
+ public CreateVisibleSignature(KeyStore keystore, char[] pin)
+ {
+ try
+ {
+ // grabs the first alias from the keystore and get the private key. An
+ // alternative method or constructor could be used for setting a specific
+ // alias that should be used.
+ Enumeration<String> aliases = keystore.aliases();
+ String alias = null;
+ if (aliases.hasMoreElements())
+ {
+ alias = aliases.nextElement();
+ }
+ else
+ {
+ throw new RuntimeException("Could not find alias");
+ }
+ privKey = (PrivateKey) keystore.getKey(alias, pin);
+ cert = keystore.getCertificateChain(alias);
+ }
+ catch (KeyStoreException e)
+ {
+ e.printStackTrace();
+ }
+ catch (UnrecoverableKeyException e)
+ {
+ System.err.println("Could not extract private key.");
+ e.printStackTrace();
+ }
+ catch (NoSuchAlgorithmException e)
+ {
+ System.err.println("Unknown algorithm.");
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Signs the given pdf file.
+ *
+ * @param document is the pdf document
+ * @param signatureProperties
+ * @return the signed pdf document
+ * @throws IOException
+ * @throws SignatureException
+ */
+ public File signPDF(File document, PDVisibleSigProperties signatureProperties)
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
+ {
+ byte[] buffer = new byte[8 * 1024];
+ if (document == null || !document.exists())
+ {
+ new RuntimeException("Document for signing does not exist");
+ }
+
+ // creating output document and prepare the IO streams.
+ String name = document.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();
+ fis = new FileInputStream(outputDocument);
+
+ // load document
+ PDDocument doc = PDDocument.load(document);
+
+ // create signature dictionary
+ PDSignature signature = new PDSignature();
+ signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); // default filter
+ // subfilter for basic and PAdES Part 2 signatures
+ signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
+ signature.setName("signer name");
+ signature.setLocation("signer location");
+ signature.setReason("reason for signature");
+
+ // the signing date, needed for valid signature
+ signature.setSignDate(Calendar.getInstance());
+
+ // register signature dictionary and sign interface
+
+ if (signatureProperties != null && signatureProperties.isVisualSignEnabled())
+ {
+ options = new SignatureOptions();
+ options.setVisualSignature(signatureProperties);
+ // options.setPage(signatureProperties.getPage());
+ // options.setPreferedSignatureSize(signatureProperties.getPreferredSize());
+ doc.addSignature(signature, this, options);
+ }
+ else
+ {
+ doc.addSignature(signature, this);
+ }
+
+ // write incremental (only for signing purpose)
+ doc.saveIncremental(fis, fos);
+
+ return outputDocument;
+ }
+
+ /**
+ * SignatureInterface implementation.
+ *
+ * This method will be called from inside of the pdfbox and create the pkcs7 signature.
+ * The given InputStream contains the bytes that are given by the byte range.
+ *
+ * This method is for internal use only. <-- TODO this method should be private
+ *
+ * Use your favorite cryptographic library to implement pkcs7 signature creation.
+ */
+ @Override
+ public byte[] sign(InputStream content) throws SignatureException, IOException
+ {
+ CMSProcessableInputStream input = new CMSProcessableInputStream(content);
+ CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
+ // CertificateChain
+ List<Certificate> certList = Arrays.asList(cert);
+
+ CertStore certStore = null;
+ try
+ {
+ certStore = CertStore.getInstance("Collection", new CollectionCertStoreParameters(certList), provider);
+ gen.addSigner(privKey, (X509Certificate) certList.get(0), CMSSignedGenerator.DIGEST_SHA256);
+ gen.addCertificatesAndCRLs(certStore);
+ CMSSignedData signedData = gen.generate(input, false, provider);
+ return signedData.getEncoded();
+ }
+ catch (Exception e)
+ {
+ // should be handled
+ System.err.println("Error while creating pkcs7 signature.");
+ e.printStackTrace();
+ }
+ throw new RuntimeException("Problem while preparing signature");
+ }
+
+ /**
+ * Arguments are
+ * [0] key store
+ * [1] pin
+ * [2] document that will be signed
+ * [3] image of visible signature
+ */
+ public static void main(String[] args) throws KeyStoreException, CertificateException,
+ IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
+ {
+
+ if (args.length != 4)
+ {
+ usage();
+ System.exit(1);
+ }
+ else
+ {
+ File ksFile = new File(args[0]);
+ KeyStore keystore = KeyStore.getInstance("PKCS12", provider);
+ char[] pin = args[1].toCharArray();
+ keystore.load(new FileInputStream(ksFile), pin);
+
+ File document = new File(args[2]);
+
+ CreateVisibleSignature signing = new CreateVisibleSignature(keystore, pin.clone());
+
+ FileInputStream image = new FileInputStream(args[3]);
+
+ PDVisibleSignDesigner visibleSig = new PDVisibleSignDesigner(args[2], image, 1);
+ visibleSig.xAxis(0).yAxis(0).zoom(-50).signatureFieldName("signature");
+
+ PDVisibleSigProperties signatureProperties = new PDVisibleSigProperties();
+
+ signatureProperties.signerName("name").signerLocation("location").signatureReason("Security").preferredSize(0)
+ .page(1).visualSignEnabled(true).setPdVisibleSignature(visibleSig).buildSignature();
+
+ signing.signPDF(document, signatureProperties);
+ }
+ }
+
+ /**
+ * This will print the usage for this program.
+ */
+ private static void usage()
+ {
+ System.err.println("Usage: java " + CreateSignature.class.getName()
+ + " <pkcs12-keystore-file> <pin> <input-pdf> <sign-image>");
}
-
- // write incremental (only for signing purpose)
- doc.saveIncremental(fis, fos);
-
- return outputDocument;
- }
-
- /**
- * <p>
- * SignatureInterface implementation.
- * </p>
- * <p>
- * This method will be called from inside of the pdfbox and create the pkcs7 signature. The given InputStream contains
- * the bytes that are providen by the byte range.
- * </p>
- * <p>
- * This method is for internal use only.
- * </p>
- * <p>
- * Here the user should use his favorite cryptographic library and implement a pkcs7 signature creation.
- * </p>
- */
- @Override
- public byte[] sign(InputStream content) throws SignatureException, IOException
- {
- CMSProcessableInputStream input = new CMSProcessableInputStream(content);
- CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
- // CertificateChain
- List<Certificate> certList = Arrays.asList(cert);
-
- CertStore certStore = null;
- try {
- certStore = CertStore.getInstance("Collection", new CollectionCertStoreParameters(certList), provider);
- gen.addSigner(privKey, (X509Certificate) certList.get(0), CMSSignedGenerator.DIGEST_SHA256);
- gen.addCertificatesAndCRLs(certStore);
- CMSSignedData signedData = gen.generate(input, false, provider);
- return signedData.getEncoded();
- } catch (Exception e) {
- // should be handled
- System.err.println("Error while creating pkcs7 signature.");
- e.printStackTrace();
- }
- throw new RuntimeException("Problem while preparing signature");
- }
-
- /**
- * Arguments are
- * [0] key store
- * [1] pin
- * [2] document that will be signed
- * [3] image of visible signature
- */
- public static void main(String[] args) throws KeyStoreException, NoSuchAlgorithmException, CertificateException,
- FileNotFoundException, IOException, COSVisitorException, SignatureException
- {
-
- if (args.length != 4) {
- usage();
- System.exit(1);
- } else {
- File ksFile = new File(args[0]);
- KeyStore keystore = KeyStore.getInstance("PKCS12", provider);
- char[] pin = args[1].toCharArray();
- keystore.load(new FileInputStream(ksFile), pin);
-
- File document = new File(args[2]);
-
- CreateVisibleSignature signing = new CreateVisibleSignature(keystore, pin.clone());
-
- FileInputStream image = new FileInputStream(args[3]);
-
- PDVisibleSignDesigner visibleSig = new PDVisibleSignDesigner(args[2], image, 1);
- visibleSig.xAxis(0).yAxis(0).zoom(-50).signatureFieldName("signature");
-
- PDVisibleSigProperties signatureProperties = new PDVisibleSigProperties();
-
- signatureProperties.signerName("name").signerLocation("location").signatureReason("Security").preferredSize(0)
- .page(1).visualSignEnabled(true).setPdVisibleSignature(visibleSig).buildSignature();
-
- signing.signPDF(document, signatureProperties);
- }
-
- }
-
- /**
- * This will print the usage for this program.
- */
- private static void usage()
- {
- System.err.println("Usage: java " + CreateSignature.class.getName()
- + " <pkcs12-keystore-file> <pin> <input-pdf> <sign-image>");
- }
}
Modified: pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestRubberStampWithImage.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestRubberStampWithImage.java?rev=1576239&r1=1576238&r2=1576239&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestRubberStampWithImage.java (original)
+++ pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestRubberStampWithImage.java Tue Mar 11 09:13:13 2014
@@ -17,16 +17,20 @@
package org.apache.pdfbox.examples.pdmodel;
import junit.framework.TestCase;
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.SignatureException;
import java.io.File;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
/**
* Test for RubberStampWithImage
*/
public class TestRubberStampWithImage extends TestCase
{
- public void test() throws IOException
+ public void test()
+ throws IOException, CryptographyException, SignatureException, NoSuchAlgorithmException
{
String documentFile = "src/test/resources/org.apache.pdfbox.examples.pdmodel/document.pdf";
String stampFile = "src/test/resources/org.apache.pdfbox.examples.pdmodel/stamp.jpg";