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/04/23 07:35:21 UTC
svn commit: r1675543 -
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
Author: tilman
Date: Thu Apr 23 05:35:20 2015
New Revision: 1675543
URL: http://svn.apache.org/r1675543
Log:
PDFBOX-2576: split long method
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1675543&r1=1675542&r2=1675543&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Thu Apr 23 05:35:20 2015
@@ -277,86 +277,13 @@ public class PDDocument implements Close
COSDocument visualSignature = options.getVisualSignature();
// Distinction of case for visual and non-visual signature
- if (visualSignature == null) // non-visual signature
+ if (visualSignature == null)
{
- // Set rectangle for non-visual signature to 0 0 0 0
- signatureField.getWidget().setRectangle(new PDRectangle()); // rectangle array [ 0 0 0 0 ]
- // Clear AcroForm / Set DefaultRessource
- acroForm.setDefaultResources(null);
- // Set empty Appearance-Dictionary
- PDAppearanceDictionary ap = new PDAppearanceDictionary();
-
- COSStream apsStream = getDocument().createCOSStream();
- apsStream.createUnfilteredStream();
- PDAppearanceStream aps = new PDAppearanceStream(apsStream);
- COSDictionary cosObject = (COSDictionary) aps.getCOSObject();
- cosObject.setItem(COSName.SUBTYPE, COSName.FORM);
- cosObject.setItem(COSName.BBOX, new PDRectangle());
-
- ap.setNormalAppearance(aps);
- ap.getCOSObject().setDirect(true);
- signatureField.getWidget().setAppearance(ap);
+ prepareNonVisibleSignature(signatureField, acroForm);
}
else
- // visual signature
{
- // Obtain visual signature object
- List<COSObject> cosObjects = visualSignature.getObjects();
-
- boolean annotNotFound = true;
- boolean sigFieldNotFound = true;
- COSDictionary acroFormDict = acroForm.getDictionary();
- for (COSObject cosObject : cosObjects)
- {
- if (!annotNotFound && !sigFieldNotFound)
- {
- break;
- }
-
- COSBase base = cosObject.getObject();
- if (base instanceof COSDictionary)
- {
- COSDictionary cosBaseDict = (COSDictionary) base;
- COSBase ft = cosBaseDict.getDictionaryObject(COSName.FT);
- COSBase type = cosBaseDict.getDictionaryObject(COSName.TYPE);
- COSBase apDict = cosBaseDict.getDictionaryObject(COSName.AP);
-
- // Search for signature annotation
- if (annotNotFound && COSName.ANNOT.equals(type))
- {
- // Read and set the Rectangle for visual signature
- COSArray rectAry = (COSArray) cosBaseDict.getDictionaryObject(COSName.RECT);
- PDRectangle rect = new PDRectangle(rectAry);
- signatureField.getWidget().setRectangle(rect);
- annotNotFound = false;
- }
-
- // Search for Signature-Field
- if (sigFieldNotFound && COSName.SIG.equals(ft) && apDict != null)
- {
- // read and set Appearance Dictionary
- PDAppearanceDictionary ap =
- new PDAppearanceDictionary((COSDictionary)cosBaseDict.getDictionaryObject(COSName.AP));
- ap.getCOSObject().setDirect(true);
- signatureField.getWidget().setAppearance(ap);
-
- // read and set AcroForm DefaultResource
- COSDictionary dr = (COSDictionary) cosBaseDict.getDictionaryObject(COSName.DR);
- if (dr != null)
- {
- dr.setDirect(true);
- dr.setNeedToBeUpdated(true);
- acroFormDict.setItem(COSName.DR, dr);
- }
- sigFieldNotFound = false;
- }
- }
- }
-
- if (annotNotFound || sigFieldNotFound)
- {
- throw new IllegalArgumentException("Template is missing required objects");
- }
+ prepareVisibleSignature(signatureField, acroForm, visualSignature);
}
// Get the annotations of the page and append the signature-annotation to it
@@ -371,6 +298,89 @@ public class PDDocument implements Close
page.getCOSObject().setNeedToBeUpdated(true);
}
+ private void prepareVisibleSignature(PDSignatureField signatureField, PDAcroForm acroForm,
+ COSDocument visualSignature) throws IllegalArgumentException
+ {
+ // Obtain visual signature object
+ List<COSObject> cosObjects = visualSignature.getObjects();
+
+ boolean annotNotFound = true;
+ boolean sigFieldNotFound = true;
+ COSDictionary acroFormDict = acroForm.getDictionary();
+ for (COSObject cosObject : cosObjects)
+ {
+ if (!annotNotFound && !sigFieldNotFound)
+ {
+ break;
+ }
+
+ COSBase base = cosObject.getObject();
+ if (base instanceof COSDictionary)
+ {
+ COSDictionary cosBaseDict = (COSDictionary) base;
+ COSBase ft = cosBaseDict.getDictionaryObject(COSName.FT);
+ COSBase type = cosBaseDict.getDictionaryObject(COSName.TYPE);
+ COSBase apDict = cosBaseDict.getDictionaryObject(COSName.AP);
+
+ // Search for signature annotation
+ if (annotNotFound && COSName.ANNOT.equals(type))
+ {
+ // Read and set the Rectangle for visual signature
+ COSArray rectAry = (COSArray) cosBaseDict.getDictionaryObject(COSName.RECT);
+ PDRectangle rect = new PDRectangle(rectAry);
+ signatureField.getWidget().setRectangle(rect);
+ annotNotFound = false;
+ }
+
+ // Search for Signature-Field
+ if (sigFieldNotFound && COSName.SIG.equals(ft) && apDict != null)
+ {
+ // read and set Appearance Dictionary
+ PDAppearanceDictionary ap =
+ new PDAppearanceDictionary((COSDictionary)cosBaseDict.getDictionaryObject(COSName.AP));
+ ap.getCOSObject().setDirect(true);
+ signatureField.getWidget().setAppearance(ap);
+
+ // read and set AcroForm DefaultResource
+ COSDictionary dr = (COSDictionary) cosBaseDict.getDictionaryObject(COSName.DR);
+ if (dr != null)
+ {
+ dr.setDirect(true);
+ dr.setNeedToBeUpdated(true);
+ acroFormDict.setItem(COSName.DR, dr);
+ }
+ sigFieldNotFound = false;
+ }
+ }
+ }
+
+ if (annotNotFound || sigFieldNotFound)
+ {
+ throw new IllegalArgumentException("Template is missing required objects");
+ }
+ }
+
+ private void prepareNonVisibleSignature(PDSignatureField signatureField, PDAcroForm acroForm) throws IOException
+ {
+ // Set rectangle for non-visual signature to 0 0 0 0
+ signatureField.getWidget().setRectangle(new PDRectangle()); // rectangle array [ 0 0 0 0 ]
+ // Clear AcroForm / Set DefaultRessource
+ acroForm.setDefaultResources(null);
+ // Set empty Appearance-Dictionary
+ PDAppearanceDictionary ap = new PDAppearanceDictionary();
+
+ COSStream apsStream = getDocument().createCOSStream();
+ apsStream.createUnfilteredStream();
+ PDAppearanceStream aps = new PDAppearanceStream(apsStream);
+ COSDictionary cosObject = (COSDictionary) aps.getCOSObject();
+ cosObject.setItem(COSName.SUBTYPE, COSName.FORM);
+ cosObject.setItem(COSName.BBOX, new PDRectangle());
+
+ ap.setNormalAppearance(aps);
+ ap.getCOSObject().setDirect(true);
+ signatureField.getWidget().setAppearance(ap);
+ }
+
/**
* This will add a signature field to the document.
*