|
This code snippet is taken from Annotations.cs line 666 in the
Annotations example project.
CommitSignature();
CatalogElement cat = new CatalogElement(Doc.ObjectSoup.Catalog);
cat.EntryAcroForm.EntrySigFlags = 3;
// NB If you don't want your signature to print then set the /F flag to 0
int fieldID = Doc.AddObject("<</Type /Annot /Subtype /Widget /F 4 /FT /Sig /DA (/TimesRoman 0 Tf 0 g)>>");
WidgetAnnotationElement widget = new WidgetAnnotationElement(Doc.ObjectSoup[fieldID]);
SignatureFieldElement field = (SignatureFieldElement)widget.FieldElement;
int sigDictID = Doc.AddObject("<</Type /Sig /Filter /Adobe.PPKLite /SubFilter /adbe.pkcs7.detached>>");
SignatureElement signature = new SignatureElement(Doc.ObjectSoup[sigDictID]);
if (inLocked) {
IndirectObject io = IndirectObject.FromString("<< /Type /SigFieldLock /Action /All /P 1 >>");
field.Host.Soup.Add(io);
SignatureFieldLockElement sigFieldLock = new SignatureFieldLockElement(io);
field.EntryLock = sigFieldLock;
field.Object.Version = 5; // PDF 1.5
}
field.EntryV = signature;
FormField formField = new FormField(this, inName, field.Object.ID);
formField.Widget.Rect = new XRect(inRect);
Doc.Form.Refresh();
mSig = (Signature)Doc.Form[inName];
sign(mSig);
if (inReason != null)
signature.EntryReason = inReason;
if (inLocation != null)
signature.EntryLocation = inLocation;
if (mSig.Signer != null)
signature.EntryName = mSig.Signer;
if (mCertify) {
// make it PDF 1.6, see the second point under "Validating MDP signatures" in the PDF reference
signature.Object.Version = 6;
// make signature MDP
int id = Doc.AddObject("<</Type /SigRef /TransformMethod /DocMDP /TransformParams <</Type /TransformParams /P 2 /V /1.2>>>>");
SignatureReferenceElement sigRef = new SignatureReferenceElement(Doc.ObjectSoup[id]);
signature.EntryReference = new ArrayElement<SignatureReferenceElement>(signature);
signature.EntryReference.Add(sigRef);
if (cat.EntryPerms == null)
cat.EntryPerms = new PermissionsElement(cat);
cat.EntryPerms.EntryDocMDP = signature;
}
if ((inAppearanceTextFormat != null) || (inImage != null)) {
XRect rect = widget.EntryRect.GetRect();
rect.Pin = XRect.Corner.TopLeft;
if (inAppearanceTextFormat != null) {
rect.Width = 200; // Change this to fit the Text
rect.Height = 60; // Change this to fit the Text
}
widget.EntryRect.SetRect(rect);
rect = rect.Clone();
rect.Pin = XRect.Corner.BottomLeft;
rect.Position(0, 0);
if (inImage != null) {
XRect imgRect = XRect.FromSides(0, 0, inImage.Width, inImage.Height);
imgRect.FitIn(rect, ContentAlign.Center, ContentScaleMode.ExactFit);
rect.String = imgRect.String;
}
double validityMessageHeight = 0;
int fontSize = 12;
if (mShowSignatureValidity) {
validityMessageHeight = Math.Min(0.3 * rect.Height, 0.1 * rect.Width);
fontSize = 8;
}
string theRect = Doc.Rect.String;
int theFont = Doc.Font;
int theFontSize = Doc.FontSize;
double charSpacing = Doc.TextStyle.CharSpacing;
double lineSpacing = Doc.TextStyle.LineSpacing;
int pageID = Doc.Page;
// Use a detached page to avoid changes to the current page
// or to the page tree for a new page
Doc.Page = Doc.AddObject("<</Type /Page /MediaBox [" + rect.String + "] >>");
Doc.Rect.String = rect.String;
StringBuilder apStream = new StringBuilder();
string imageResName = null;
int imageID = 0;
if (inImage != null) {
int imageLayerID = Doc.AddImage(inImage);
string imageStream = Doc.GetInfo(imageLayerID, "Stream");
ImageLayer il = (ImageLayer)Doc.ObjectSoup[imageLayerID];
imageID = il.PixMap.ID;
Doc.Delete(imageLayerID);
int l1 = imageStream.IndexOf("/Iabc", 0, imageStream.Length);
int l2 = imageStream.IndexOf(" ", l1, imageStream.Length - l1);
imageResName = imageStream.Substring(l1 + 1, l2 - l1 - 1);
apStream.Append(imageStream);
apStream.AppendLine();
}
string fontResName = null;
int fontID = 0;
if (inAppearanceTextFormat != null) {
if (validityMessageHeight != 0)
Doc.Rect.Top -= validityMessageHeight;
fontID = Doc.AddFont("Times-Roman");
Doc.Font = fontID;
Doc.FontSize = fontSize;
Doc.TextStyle.CharSpacing = 0;
Doc.TextStyle.LineSpacing = 2;
string text = string.Format(inAppearanceTextFormat, mSig.Signer, mSig.SigningUtcTime, inReason, inLocation);
int textID = Doc.AddText(text);
string textStream = Doc.GetInfo(textID, "Stream");
Doc.Delete(textID);
int l1 = textStream.IndexOf("/Fabc", 0, textStream.Length);
int l2 = textStream.IndexOf(" ", l1, textStream.Length - l1);
fontResName = textStream.Substring(l1 + 1, l2 - l1 - 1);
apStream.Append(textStream);
apStream.AppendLine();
}
Doc.Delete(Doc.Page);
Doc.Page = pageID;
Doc.Rect.String = theRect;
Doc.Font = theFont;
Doc.FontSize = theFontSize;
Doc.TextStyle.CharSpacing = charSpacing;
Doc.TextStyle.LineSpacing = lineSpacing;
FormXObjectElement appearance = MakeAppearance(rect, imageResName, imageID, fontResName, fontID, apStream.ToString());
if (mShowSignatureValidity) {
if (mBlankAppearance == null)
mBlankAppearance = MakeAppearance(XRect.FromSides(0, 0, 100, 100), null, 0, null, 0, "");
XRect clone = rect.Clone();
clone.Bottom = rect.Top - validityMessageHeight;
FormXObjectElement validityAppearance = MakeAppearance(clone, null, 0, fontResName, fontID, "");
double side = 0.9 * Math.Min(rect.Width, rect.Height);
string contents = string.Format(NumberFormatInfo.InvariantInfo,
"q 1 0 0 1 0 0 cm /n0 Do Q"
+ "\r\nq {0:0.#####} 0 0 {0:0.#####} {1:0.#####} {2:0.#####} cm /n1 Do Q"
+ "\r\nq 1 0 0 1 0 0 cm /n2 Do Q"
+ "\r\nq {0:0.#####} 0 0 {0:0.#####} {1:0.#####} {2:0.#####} cm /n3 Do Q"
+ "\r\nq 1 0 0 1 0 0 cm /n4 Do Q"
+ "\r\n", side / 100, (rect.Width - side) / 2, (rect.Height - side) / 2);
FormXObjectElement frmElement = MakeAppearance(rect, "", 0, null, 0, contents);
frmElement.EntryResources.EntryXObject.Add("n0", mBlankAppearance);
frmElement.EntryResources.EntryXObject.Add("n1", mBlankAppearance);
frmElement.EntryResources.EntryXObject.Add("n2", appearance);
frmElement.EntryResources.EntryXObject.Add("n3", mBlankAppearance);
frmElement.EntryResources.EntryXObject.Add("n4", validityAppearance);
appearance = MakeAppearance(rect, "", 0, null, 0, "q 1 0 0 1 0 0 cm /FRM Do Q\r\n");
appearance.EntryResources.EntryXObject.Add("FRM", frmElement);
}
widget.EntryAP = new AppearanceElement(widget);
widget.EntryAP.EntryN = appearance;
}
return formField;
CommitSignature()
Dim cat As New CatalogElement(Doc.ObjectSoup.Catalog)
cat.EntryAcroForm.EntrySigFlags = 3
' NB If you don't want your signature to print then set the /F flag to 0
Dim fieldID As Integer = Doc.AddObject("<</Type /Annot /Subtype /Widget /F 4 /FT /Sig /DA (/TimesRoman 0 Tf 0 g)>>")
Dim widget As New WidgetAnnotationElement(Doc.ObjectSoup(fieldID))
Dim field As SignatureFieldElement = CType(widget.FieldElement, SignatureFieldElement)
Dim sigDictID As Integer = Doc.AddObject("<</Type /Sig /Filter /Adobe.PPKLite /SubFilter /adbe.pkcs7.detached>>")
Dim signature As New SignatureElement(Doc.ObjectSoup(sigDictID))
If inLocked Then
Dim io As IndirectObject = IndirectObject.FromString("<< /Type /SigFieldLock /Action /All /P 1 >>")
field.Host.Soup.Add(io)
Dim sigFieldLock As New SignatureFieldLockElement(io)
field.EntryLock = sigFieldLock
field.Object.Version = 5 ' PDF 1.5
End If
field.EntryV = signature
Dim formField As New FormField(Me, inName, field.Object.ID)
formField.Widget.Rect = New XRect(inRect)
Doc.Form.Refresh()
mSig = CType(Doc.Form(inName), Signature)
sign(mSig)
If inReason IsNot Nothing Then
signature.EntryReason = inReason
End If
If inLocation IsNot Nothing Then
signature.EntryLocation = inLocation
End If
If mSig.Signer IsNot Nothing Then
signature.EntryName = mSig.Signer
End If
If mCertify Then
' make it PDF 1.6, see the second point under "Validating MDP signatures" in the PDF reference
signature.Object.Version = 6
' make signature MDP
Dim id As Integer = Doc.AddObject("<</Type /SigRef /TransformMethod /DocMDP /TransformParams <</Type /TransformParams /P 2 /V /1.2>>>>")
Dim sigRef As New SignatureReferenceElement(Doc.ObjectSoup(id))
signature.EntryReference = New ArrayElement(Of SignatureReferenceElement)(signature)
signature.EntryReference.Add(sigRef)
If cat.EntryPerms Is Nothing Then
cat.EntryPerms = New PermissionsElement(cat)
End If
cat.EntryPerms.EntryDocMDP = signature
End If
If (inAppearanceTextFormat IsNot Nothing) OrElse (inImage IsNot Nothing) Then
Dim rect As XRect = widget.EntryRect.GetRect()
rect.Pin = XRect.Corner.TopLeft
If inAppearanceTextFormat IsNot Nothing Then
rect.Width = 200 ' Change this to fit the Text
rect.Height = 60 ' Change this to fit the Text
End If
widget.EntryRect.SetRect(rect)
rect = rect.Clone()
rect.Pin = XRect.Corner.BottomLeft
rect.Position(0, 0)
If inImage IsNot Nothing Then
Dim imgRect As XRect = XRect.FromSides(0, 0, inImage.Width, inImage.Height)
imgRect.FitIn(rect, ContentAlign.Center, ContentScaleMode.ExactFit)
rect.String = imgRect.String
End If
Dim validityMessageHeight As Double = 0
Dim fontSize As Integer = 12
If mShowSignatureValidity Then
validityMessageHeight = Math.Min(0.3 * rect.Height, 0.1 * rect.Width)
fontSize = 8
End If
Dim theRect As String = Doc.Rect.String
Dim theFont As Integer = Doc.Font
Dim theFontSize As Integer = Doc.FontSize
Dim charSpacing As Double = Doc.TextStyle.CharSpacing
Dim lineSpacing As Double = Doc.TextStyle.LineSpacing
Dim pageID As Integer = Doc.Page
' Use a detached page to avoid changes to the current page
' or to the page tree for a new page
Doc.Page = Doc.AddObject("<</Type /Page /MediaBox [" & rect.String & "] >>")
Doc.Rect.String = rect.String
Dim apStream As New StringBuilder()
Dim imageResName As String = Nothing
Dim imageID As Integer = 0
If inImage IsNot Nothing Then
Dim imageLayerID As Integer = Doc.AddImage(inImage)
Dim imageStream As String = Doc.GetInfo(imageLayerID, "Stream")
Dim il As ImageLayer = CType(Doc.ObjectSoup(imageLayerID), ImageLayer)
imageID = il.PixMap.ID
Doc.Delete(imageLayerID)
Dim l1 As Integer = imageStream.IndexOf("/Iabc", 0, imageStream.Length)
Dim l2 As Integer = imageStream.IndexOf(" ", l1, imageStream.Length - l1)
imageResName = imageStream.Substring(l1 + 1, l2 - l1 - 1)
apStream.Append(imageStream)
apStream.AppendLine()
End If
Dim fontResName As String = Nothing
Dim fontID As Integer = 0
If inAppearanceTextFormat IsNot Nothing Then
If validityMessageHeight <> 0 Then
Doc.Rect.Top -= validityMessageHeight
End If
fontID = Doc.AddFont("Times-Roman")
Doc.Font = fontID
Doc.FontSize = fontSize
Doc.TextStyle.CharSpacing = 0
Doc.TextStyle.LineSpacing = 2
Dim text As String = String.Format(inAppearanceTextFormat, mSig.Signer, mSig.SigningUtcTime, inReason, inLocation)
Dim textID As Integer = Doc.AddText(text)
Dim textStream As String = Doc.GetInfo(textID, "Stream")
Doc.Delete(textID)
Dim l1 As Integer = textStream.IndexOf("/Fabc", 0, textStream.Length)
Dim l2 As Integer = textStream.IndexOf(" ", l1, textStream.Length - l1)
fontResName = textStream.Substring(l1 + 1, l2 - l1 - 1)
apStream.Append(textStream)
apStream.AppendLine()
End If
Doc.Delete(Doc.Page)
Doc.Page = pageID
Doc.Rect.String = theRect
Doc.Font = theFont
Doc.FontSize = theFontSize
Doc.TextStyle.CharSpacing = charSpacing
Doc.TextStyle.LineSpacing = lineSpacing
Dim appearance As FormXObjectElement = MakeAppearance(rect, imageResName, imageID, fontResName, fontID, apStream.ToString())
If mShowSignatureValidity Then
If mBlankAppearance Is Nothing Then
mBlankAppearance = MakeAppearance(XRect.FromSides(0, 0, 100, 100), Nothing, 0, Nothing, 0, "")
End If
Dim clone As XRect = rect.Clone()
clone.Bottom = rect.Top - validityMessageHeight
Dim validityAppearance As FormXObjectElement = MakeAppearance(clone, Nothing, 0, fontResName, fontID, "")
Dim side As Double = 0.9 * Math.Min(rect.Width, rect.Height)
Dim contents As String = String.Format(NumberFormatInfo.InvariantInfo, "q 1 0 0 1 0 0 cm /n0 Do Q" & vbCrLf & "q {0:0.#####} 0 0 {0:0.#####} {1:0.#####} {2:0.#####} cm /n1 Do Q" & vbCrLf & "q 1 0 0 1 0 0 cm /n2 Do Q" & vbCrLf & "q {0:0.#####} 0 0 {0:0.#####} {1:0.#####} {2:0.#####} cm /n3 Do Q" & vbCrLf & "q 1 0 0 1 0 0 cm /n4 Do Q" & vbCrLf, side / 100, (rect.Width - side) / 2, (rect.Height - side) / 2)
Dim frmElement As FormXObjectElement = MakeAppearance(rect, "", 0, Nothing, 0, contents)
frmElement.EntryResources.EntryXObject.Add("n0", mBlankAppearance)
frmElement.EntryResources.EntryXObject.Add("n1", mBlankAppearance)
frmElement.EntryResources.EntryXObject.Add("n2", appearance)
frmElement.EntryResources.EntryXObject.Add("n3", mBlankAppearance)
frmElement.EntryResources.EntryXObject.Add("n4", validityAppearance)
appearance = MakeAppearance(rect, "", 0, Nothing, 0, "q 1 0 0 1 0 0 cm /FRM Do Q" & vbCrLf)
appearance.EntryResources.EntryXObject.Add("FRM", frmElement)
End If
widget.EntryAP = New AppearanceElement(widget)
widget.EntryAP.EntryN = appearance
End If
Return formField
|
|
|