Skip to content
Browse files

Pdfs know and can set certification level

Also removed the warnings because KeyStore and String constants
were being used in multiple namespaces.
  • Loading branch information...
1 parent 7f91f8d commit 72120471a7c7009d176399c61627697c7566011e @scizo committed Jul 27, 2011
Showing with 63 additions and 9 deletions.
  1. +5 −8 lib/jpdfer/key_store.rb
  2. +33 −1 lib/jpdfer/pdf.rb
  3. +25 −0 spec/acceptance/jpdfer/pdf_acceptance_spec.rb
View
13 lib/jpdfer/key_store.rb
@@ -1,15 +1,12 @@
class KeyStore
- class ConfigurationError < Exception; end
- module J
- include_package "java.io"
- include_package "java.security"
- include_package "java.lang"
- end
+ JavaFileInputStream = java.io.FileInputStream
+ JavaKeyStore = java.security.KeyStore
+ JavaString = java.lang.String
def initialize(keystore_path, _alias, password)
@alias, @password = _alias, password
- @keystore = J::KeyStore::getInstance(J::KeyStore::getDefaultType())
- @keystore.load(J::FileInputStream.new(keystore_path), J::String.new(@password).toCharArray)
+ @keystore = JavaKeyStore::getInstance(JavaKeyStore::getDefaultType())
+ @keystore.load(JavaFileInputStream.new(keystore_path), JavaString.new(@password).toCharArray)
end
def private_key
View
34 lib/jpdfer/pdf.rb
@@ -5,7 +5,6 @@
class Pdf
class NonexistentFieldError < Exception; end
class ReadOnlyError < Exception; end
- class ConfigurationError < Exception; end
include_package "com.itextpdf.text.pdf"
include_package "com.itextpdf.text.xml.xmp"
@@ -157,4 +156,37 @@ def has_flattened_fields?
flattened_fields.size > 0 ? true : false
end
+ # Returns the certification level of the pdf
+ def certification_level
+ case @stamper.reader.getCertificationLevel
+ when PdfSignatureAppearance::CERTIFIED_FORM_FILLING
+ level = :form_filling
+ when PdfSignatureAppearance::CERTIFIED_FORM_FILLING_AND_ANNOTATIONS
+ level = :form_filling_and_annotations
+ when PdfSignatureAppearance::CERTIFIED_NO_CHANGES_ALLOWED
+ level = :no_changes_allowed
+ when PdfSignatureAppearance::NOT_CERTIFIED
+ level = :not_certified
+ end
+ level
+ end
+
+ # Set the certification level on a pdf initialized with an optional keystore
+ #
+ # *level* must be one of :form_filling, :form_filling_and_annotations,
+ # :no_changes_allowed, :not_certified
+ def set_certification_level(level)
+ case level
+ when :form_filling
+ certification_level = PdfSignatureAppearance::CERTIFIED_FORM_FILLING
+ when :form_filling_and_annotations
+ certification_level = PdfSignatureAppearance::CERTIFIED_FORM_FILLING_AND_ANNOTATIONS
+ when :no_changes_allowed
+ certification_level = PdfSignatureAppearance::CERTIFIED_NO_CHANGES_ALLOWED
+ when :not_certified
+ level = PdfSignatureAppearance::NOT_CERTIFIED
+ end
+ @stamper.getSignatureAppearance.setCertificationLevel(certification_level)
+ end
+
end
View
25 spec/acceptance/jpdfer/pdf_acceptance_spec.rb
@@ -63,6 +63,15 @@
end
end
+ describe '#set_certification_level' do
+ it 'should have the given certification level when saved' do
+ @pdf.set_certification_level(:no_changes_allowed)
+ @pdf.save_as(@signed_pdf_path)
+ @pdf = Pdf.new(@signed_pdf_path)
+ @pdf.certification_level.should == :no_changes_allowed
+ end
+ end
+
# TODO: I would like to add this functionality but havn't researched the java
# KeyStore and encryption classes enough yet
#
@@ -95,6 +104,22 @@
end
end
+ describe '#certification_level' do
+ it 'should return nil for an unsigned pdf' do
+ @pdf.certification_level.should == :not_certified
+ end
+
+ describe 'called on a signed pdf' do
+ before(:each) do
+ @pdf = Pdf.new(File.join(@data_path, 'simple_form_flattened_signed.pdf'))
+ end
+
+ it 'it should return the certification level' do
+ @pdf.certification_level.should == :no_changes_allowed
+ end
+ end
+ end
+
describe '#fields' do
it 'should return a hash of field name value pairs' do
@pdf.fields.should == @unfilled_fields

0 comments on commit 7212047

Please sign in to comment.
Something went wrong with that request. Please try again.