Add Digital Signature Using C# and Acrobat SDK
Copy link to clipboard
Copied
Please, how can I digitally sign PDF documents using Acrobat Professional 8 API and C# language?
I know that I need use JavaScript APIs (IAC) but I can't find anything really helpful in the Acrobat SDK Documentation. Could anyone post a sample of how use javascript manipulation inside C# (Framework 2.0), or give me a direction?
Thank in advance!
Copy link to clipboard
Copied
Once you have your JSObject:
jso.AddSignature();
If you are not able to call any functions via the JSObject then you have not declared or assigned it properly.
Copy link to clipboard
Copied
In regards to the post by Patrick I must be missing something and was wondering if someone could help me out:
"It most certainly is possible to call JS from C#.
Once you have your JSObject:
jso.AddSignature();
If you are not able to call any functions via the JSObject then you have not declared or assigned it properly"
I have a C# app with a reference to Interop.Acrobat.dll and I am trying to add a bookmark to the PDF. Since this dll is COM I am assuming that it is wrapped with RCW(Runtime Callable Wrapper)? I grabbed the dll from the SDK example project BasicIacJsoVB. I am able to view the properties and methods for strongly typed objects such as AcroApp and CAcroAVDoc. However, for the object returned from GetJSObject() intellisense just shows the methods for the object base class(ToString, Equals, GetHashCode, GetType). I don't see any other methods and the project won't build with the code below. I would prefer not to use late binding as the examples shown in previous posts. Any ideas?
----------------------------------------------------------------------------------------------------------------------------------
string gPDFPath;
object jso;
object BMR;
object BMC;
//hard coding for a PDF to open, it can be changed when needed.
gPDFPath = @"C:\test1\1.pdf";
Acrobat.AcroApp gApp = new Acrobat.AcroAppClass();
// show Acrobat
gApp.Show();
//Set AVDoc object
Acrobat.CAcroAVDoc gAvDoc = new Acrobat.AcroAVDocClass();
// open the PDF
if (gAvDoc.Open(gPDFPath, ""))
{
Acrobat.CAcroPDDoc gPdDoc = gAvDoc.GetPDDoc() as Acrobat.CAcroPDDoc;
jso = gPdDoc.GetJSObject();
BMR = jso.BookmarkRoot();
//BMR.createchild("First Topic", "this.pageNum= 0", 0);
}
-------------------------------------------------------------------------------------------------------------------------------------
Copy link to clipboard
Copied
Even i am encountering same problem. There is no error or exception while executing the code but pdf is not getting signed.
PDL -- can you please post the code that you have written in C#.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Here is the code i m using in C#
Acrobat.AcroAVDoc acroAVDoc = new Acrobat.AcroAVDoc();
acroAVDoc.Open(@"c:\T-form.pdf", "TempTitle");
Acrobat.CAcroPDDoc acroDoc = (Acrobat.CAcroPDDoc)acroAVDoc.GetPDDoc();
acroDoc.Open(@"c:\T-form.pdf");
Object jso = acroDoc.GetJSObject();
but i am not getting any method AddSignature in jso
Copy link to clipboard
Copied
http://www.adobeforums.com/webx/.3c064c08/1
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Sumit, try to convert my last VB.NET post to C#, you don't need to open twice.
Copy link to clipboard
Copied
when i am signing the document.
I believe i am missing something in my code. After executing the code to sign, when i open it i get "The following signature fields are not signed".
Please let me know where i m doing wrong?
Here is my VB.NET Code
Dim gapp As Acrobat.CAcroApp
Dim gpddoc As Acrobat.CAcroPDDoc
Dim jso As Object
gapp = CreateObject("acroexch.app")
gpddoc = CreateObject("acroexch.pddoc")
If gpddoc.Open("C:\Test1.pdf") Then
jso = gpddoc.GetJSObject()
jso.SetUserPassword("'testpassword12'")
'jso.ShowMyMessage("SetUserDigitalIDPath")
jso.SetUserDigitalIDPath("'c:\\DrTest.pfx'")
jso.app.execMenuItem("ADBESDK:AddSignature")
jso.AddSignature(jso)
gapp.Show()
and here is the javascript
// password to use the digital signature
var sigUserPwd = "UNKNOWN";
// to test the sample without user input, specify:
// var sigUserPwd = "testpassword";
// path to the digital signature file
var sigDigitalIDPath = "UNKNOWN";
// to test the sample without user input, specify:
//var sigDigitalIDPath = "/C/DrTest.pfx";
// other variables the user can modify
var sigHandlerName = "Adobe.PPKLite";
var sigFieldname = "sdkSignatureTest";
var sigReason = "I want to test my digital signature program.";
var sigLocation = "San Jose, CA";
var sigContactInfo = "sendme@testinc.com";
/* Add a menu item for AddSignature */
app.addMenuItem( { cName: "ADBESDK:AddSignature", cUser: "Add My Signature", cParent: "Advanced",
cEnable: "event.rc = (event.target != null);",
cExec: "AddSignature(event.target)" });
// main function
AddSignature=app.trustedFunction(function (doc)
{
app.beginPriv(); // explicitly raise privilege
// if sigDigitalIDPath is not spcified, ask for user input
if(sigDigitalIDPath == "UNKNOWN"){
var cResponse = app.response({
cQuestion: "Input your digital ID path:",
cTitle: "Digital Signature",
cDefault: "/C/DrTest.pfx",
});
if ( cResponse == null) {
app.alert("No input.");
return;
}
else
SetUserDigitalIDPath(cResponse);
}
// if sigUserPwd is not spcified, ask for user input
if(sigUserPwd == "UNKNOWN"){
var cResponse = app.response({
cQuestion: "Input your password:",
cTitle: "Digital Signature",
cDefault: "testpassword",
});
if ( cResponse == null) {
app.alert("No input.");
return
}
else
SetUserPassword(cResponse);
}
// create a new signature field
var signatureField = AddSignatureField(doc);
// sign it
if(signatureField) Sign(signatureField, sigHandlerName);
app.endPriv();
})
// create a signature field in the upper left conner with name of sigFieldname
function AddSignatureField(doc)
{
var inch=72;
var aRect = doc.getPageBox( {nPage: 0} );
aRect[0] += 0.5*inch; // from upper left hand corner of page.
aRect[2] = aRect[0]+2*inch; // Make it 2 inch wide
aRect[1] -= 0.5*inch;
aRect[3] = aRect[1] - 0.5*inch; // and 0.5 inch high
var sigField = null;
try {
sigField = doc.addField(sigFieldname, "signature", 0, aRect );
} catch (e) {
console.println("An error occurred: " + e);
}
return sigField;
}
// define the Sign function as a privileged function
Sign = app.trustedFunction (
function( sigField, DigSigHandlerName )
{
try {
app.beginPriv();
var myEngine = security.getHandler(DigSigHandlerName);
myEngine.login( sigUserPwd, sigDigitalIDPath);
sigField.signatureSign({oSig: myEngine,
bUI: false,
oInfo: { password: sigUserPwd,
reason: sigReason,
location: sigLocation,
contactInfo: sigContactInfo}
});
app.endPriv
} catch (e) {
console.println("An error occurred: " + e);
}
}
);
// set a correct password for using the signature, so you can quietly sign a doc.
function SetUserPassword(pwd)
{
sigUserPwd = pwd;
}
// set path to the digital signature file
function SetUserDigitalIDPath(idPath)
{
sigDigitalIDPath = idPath;
}
Sumit
Copy link to clipboard
Copied
I could sign the PDF using VB.NET code.
Can you please help me to convert this code in C#.
Here is my code:
Type AcrobatCAcroAppType;
AcrobatCAcroAppType = Type.GetTypeFromProgID("AcroExch.PDDoc");
Acrobat.CAcroApp gapp = (Acrobat.CAcroApp)Activator.CreateInstance(AcrobatCAcroAppType);
Type AcrobatPDDocType;
AcrobatPDDocType = Type.GetTypeFromProgID("AcroExch.PDDoc");
Acrobat.CAcroPDDoc gpddoc = (Acrobat.CAcroPDDoc)Activator.CreateInstance(AcrobatPDDocType);
object jso ;
if(gpddoc.Open("c:\\s.pdf"))
{
jso = gpddoc.GetJSObject();
jso.SetUserPassword("testpassword");
jso.SetUserDigitalIDPath("C:\\DrTest.pfx");
jso.AddSignature(jso);
}
I get error in following lines :
jso.SetUserPassword("testpassword");
jso.SetUserDigitalIDPath("C:\\DrTest.pfx");
jso.AddSignature(jso);
Thanks
sumit
Copy link to clipboard
Copied
Private Sub ExecuteJavaScript()
Dim gapp As Acrobat.CAcroApp
Dim gpddoc As Acrobat.CAcroPDDoc
Dim jso As Object
gapp = CreateObject("acroexch.app")
gpddoc = CreateObject("acroexch.pddoc")
If gpddoc.Open("c:YourFile.pdf") Then
jso = gpddoc.GetJSObject()
jso.SetUserPassword("testpassword")
jso.SetUserDigitalIDPath("/C/DrTest.pfx")
jso.AddSignature(jso)
gapp.Show()
End If
End Sub
Now, about your javascript code, I think that is the same I used. The javascript code that I used is exactly the same that came with Acrobat SDK Documentation (sdkAddSignature.js file).
Copy link to clipboard
Copied
Here is the code in C#.
Type AcrobatCAcroAppType;
AcrobatCAcroAppType = Type.GetTypeFromProgID("AcroExch.app");
Acrobat.CAcroApp gapp = (Acrobat.CAcroApp)Activator.CreateInstance(AcrobatCAcroAppType);
Type AcrobatPDDocType;
AcrobatPDDocType = Type.GetTypeFromProgID("AcroExch.PDDoc");
Acrobat.CAcroPDDoc gpddoc = (Acrobat.CAcroPDDoc)Activator.CreateInstance(AcrobatPDDocType);
object jso ;
if(gpddoc.Open("c:\\s.pdf"))
{
jso = gpddoc.GetJSObject();
object[] param = new object[1];
param[0] = "testpassword";
object con = jso.GetType().InvokeMember("SetUserPassword",
BindingFlags.InvokeMethod , null, jso, param);
param[0] = "C:\\DrTest.pfx";
con = jso.GetType().InvokeMember("SetUserDigitalIDPath",
BindingFlags.InvokeMethod , null, jso, param);
param[0] = jso;
con = jso.GetType().InvokeMember("AddSignature",
BindingFlags.InvokeMethod , null, jso, param);
Copy link to clipboard
Copied
It works fine!!
Thank you!!
Copy link to clipboard
Copied
Let me explain better with a example. When the user try to sign a document using my C# application and he type a wrong password, the document is not signed, but we just see the error opening the javascript debugger, inside the Acrobat Professional. Is it possible catch the error message to show in my c# application?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I have this javascript function:
Sign = app.trustedFunction (function( sigField, DigSigHandlerName )
{
try
{
var status;
app.beginPriv();
var myEngine = security.getHandler(DigSigHandlerName);
myEngine.login( sigUserPwd, sigDigitalIDPath);
sigField.signatureSign({oSig: myEngine,
bUI: false, oInfo: { password: sigUserPwd, reason: sigReason, location: sigLocation, contactInfo: sigContactInfo}});
app.endPriv
}
catch (e)
{
console.println("An error occurred: " + e);
status = e;
return status;
}
}
);
I am returning "status" as a error, but I can't catch this in my C# application. I am using the Sumit's C# code, like below:
Type AcrobatPDDocType;
AcrobatPDDocType = Type.GetTypeFromProgID("AcroExch.PDDoc");
Acrobat.CAcroPDDoc gpddoc = (Acrobat.CAcroPDDoc)Activator.CreateInstance(AcrobatPDDocType);
object jso ;
STRING STATUS;
if(gpddoc.Open("c:\\MyFile.pdf"))
{
jso = gpddoc.GetJSObject();
object[] param = new object[1];
param[0] = "testpassword";
object con = jso.GetType().InvokeMember("SetUserPassword",
BindingFlags.InvokeMethod , null, jso, param);
param[0] = "C:\\DrTest.pfx";
con = jso.GetType().InvokeMember("SetUserDigitalIDPath",
BindingFlags.InvokeMethod , null, jso, param);
param[0] = jso;
STATUS = (jso.GetType().InvokeMember("AddSignature",
BindingFlags.InvokeMethod , null, jso, param)).TOSTRING;
MESSAGEBOX.SHOW(STATUS)
Please, atenttion to the text in the upper case, I am trying to cacth the exception, but "status" always are null.
Do you see any mistake?
Thank you PDL!!
Copy link to clipboard
Copied
Again, I find it much easier to just call the functions directly.
Copy link to clipboard
Copied
Do I need to buy Adobe Acrobat Professional to sign the document or there is some other adobe plug in comes to sign the document.
My requirement is just to sign the document.
Thankx
Sumit
Copy link to clipboard
Copied
The Acrobat SDK is for automating Acrobat. No plug-in can change that
basic idea. Most of the SDK works with Acrobat Standard too.
Aandi Inston
Copy link to clipboard
Copied
Thanks alot for reply.
If i m not wrong you mean to say i need to buy Adobe Acrobat Professional or Acrobat Standard to sign the document.
Thankx
sumit
Copy link to clipboard
Copied
Aandi Inston
Copy link to clipboard
Copied
I wanted to know is there any adobe plug in or dll which i can use to sign document rather than buying Adobe Acrobat Professional or Acrobat Standard
Copy link to clipboard
Copied
But plug-ins require Acrobat, so where does that get us? I think the
answer is going to be no - for programmatic signing, at least. Reader
can manually sign Reader enabled files.
Aandi Inston
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Please, somebody knows how can I sign a document using a "ID Card" (accessible via PKCS#11 Devices)?
When I use a PKCS#11 Device inside the Acrobat, it recognises the device and I can sign de document using the device (no PFX certificates are needed). How can I sign a PDF document using PKCS#11 Devices and C#? How to recognise that the device is plugged in my pc? I think that I will need to change my javascript code that signs, but I have no ideias how I will work with this. Some help, please??
Thank you very much again!

