Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Add Digital Signature Using C# and Acrobat SDK

New Here ,
Feb 21, 2008 Feb 21, 2008
Hi everybody!

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!
TOPICS
Acrobat SDK and JavaScript
46.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 03, 2008 Mar 03, 2008
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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 04, 2009 Sep 04, 2009

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);
            }

-------------------------------------------------------------------------------------------------------------------------------------

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 03, 2008 Mar 03, 2008
Hi Felipe

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#.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 03, 2008 Mar 03, 2008
Which methods are you using to try and sign the document?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 03, 2008 Mar 03, 2008
hi PDL

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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 04, 2008 Mar 04, 2008
Sumit, here I am specifying better my problem:
http://www.adobeforums.com/webx/.3c064c08/1
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 04, 2008 Mar 04, 2008
Why are you opening the file twice?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 04, 2008 Mar 04, 2008
Sumit is open wrong.. He is using my old example, that is wrong.

Sumit, try to convert my last VB.NET post to C#, you don't need to open twice.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 05, 2008 Mar 05, 2008
Hi Felipe,

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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 06, 2008 Mar 06, 2008
Hi PDL/Felipie,

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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 06, 2008 Mar 06, 2008
As I said before, I couldn't use C#. I think that there is differences between your VB.NET code and mine code. Try my VB code:

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).
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 09, 2008 Mar 09, 2008
Hi Felipe,

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);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 10, 2008 Mar 10, 2008
Thank you very much Sumit.d, much better than VB.NET!!
It works fine!!

Thank you!!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 11, 2008 Mar 11, 2008
Sumit.d, or anybody that can help, Is it possible to catch the exceptions that may occur in the javascript execution, to show in my .NET application?

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?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 11, 2008 Mar 11, 2008
Use a try/catch in your JavaScript and in the catch section have the function return a value of your choice. That will be the return value of executing your function in your .NET app.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 11, 2008 Mar 11, 2008
PDL, I tried what you said but it is not working, please, could you take a look if I am doing something wrong?:

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!!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 11, 2008 Mar 11, 2008
I do not use Sumit.d's method which uses InvokeMember, I call my functions directly. You would have to research the InvokeMember method (which is not part of the Acrobat SDK, it is C#.NET) and see how it handles return values from the member functions it invokes.

Again, I find it much easier to just call the functions directly.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 13, 2008 Mar 13, 2008
Hi PDL/Felipe,

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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 14, 2008 Mar 14, 2008
>Do I need to buy Adobe Acrobat Professional

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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 14, 2008 Mar 14, 2008
Hi Aandi,

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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 14, 2008 Mar 14, 2008
What alternative could there be? I don't get what you are looking for.

Aandi Inston
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 14, 2008 Mar 14, 2008
thanx for reply AAndi

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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 14, 2008 Mar 14, 2008
>I wanted to know is there any adobe plug in ... i can use to sign document rather than buying Adobe Acrobat Professional or Acrobat Standard

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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 14, 2008 Mar 14, 2008
I am not expert in Acrobat, but I think that the only way is buying the Professional or the Standard version. But there are many 3rd party tools that you can use to sign. You can try iTextSharp for example, it works well with C# and is free. There are many samples in the internet of how use it.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 14, 2008 Mar 14, 2008
I did a C# application that signs a document. It signs using "PFX" certificates.
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!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines