Question about digitally signing PDF's

If you are a C/C++/C# developer and have any questions about using our products from your application here is the place to post them.
Post Reply
lukenine45
Posts: 15
Joined: Wed Jan 11 2006

Question about digitally signing PDF's

Post by lukenine45 »

Hi, I've been using your PDF printer for some time now from C++, and it works great, but I can't figure out how to digitally sign a PDF from C++. There doesn't appear to be any documentatation for the non-activeX version of the DigitalSignature function. I found this in another thread on this board:
5) You can set Digital signature using the ActiveX interface of CDintf or using the following function of the DLL:
int WINAPI DocDigitalSignatureA(EXTDOCHANDLE edhDocument, LPCSTR SignedBy, LPCSTR Reason, LPCSTR ImageFile, LPCSTR Location, long PageNumber, long HorzPos, long VertPos, long Width, long Height, long Flags)
I really want to use this function, but it's not documented anywhere that I can find. What is an EXTDOCHANDLE? If you could direct me to documentation, or better yet an example use of this function, I would really appreciate it.

Thanks!
Jose
Amyuni Team
Posts: 553
Joined: Tue Oct 01 2002
Contact:

Post by Jose »

Hello,

Below is a complete code snippet which illustrates how to use the digital signature feature of the PDF Converter through the DLL interface.

Hope this helps?


/************document processing functions*************/
typedef HANDLE EXTDOCHANDLE;
typedef int(CALLBACK* lpfnDocOpenAProc)(EXTDOCHANDLE* pedhDocument, LPCSTR szFileName, LPCSTR szPassword);
typedef int(CALLBACK* lpfnDocSaveAProc)(EXTDOCHANDLE edhDocument, LPCSTR szFileName);
typedef int(CALLBACK* lpfnDocCloseProc)(EXTDOCHANDLE edhDocument);
typedef bool(CALLBACK* lpfnSetLicenseKeyAProc)(LPCSTR szCompany, LPCSTR szLicKey);
typedef int(CALLBACK* lpfnDocDigitalSignatureAProc)(EXTDOCHANDLE edhDocument,
LPCSTR SignedBy,
LPCSTR Reason,
LPCSTR ImageFile,
LPCSTR Location,
long PageNumber,
long HorzPos,
long VertPos,
long Width,
long Height,
long Flags);
lpfnDocOpenAProc DocOpenA = NULL;
lpfnDocSaveAProc DocSaveA = NULL;
lpfnDocCloseProc DocClose = NULL;
lpfnDocDigitalSignatureAProc DocDigitalSignatureA = NULL;
lpfnSetLicenseKeyAProc SetLicenseKeyA = NULL;




void CPDFConverterDllDlg::OnBnClickedBtnSignDoc()
{
EXTDOCHANDLE doc; // Handle to Document
HINSTANCE hDLL; // Handle to DLL
hDLL = LoadLibrary( _T("CDINTF250.DLL") );
if ( hDLL != NULL )
{

//Must check return codes of all functions
DocOpenA = (lpfnDocOpenAProc) GetProcAddress (hDLL, _T("DocOpenA"));
DocDigitalSignatureA = (lpfnDocDigitalSignatureAProc) GetProcAddress (hDLL, _T("DocDigitalSignatureA"));
DocSaveA = (lpfnDocSaveAProc) GetProcAddress (hDLL, _T("DocSaveA"));
DocClose = (lpfnDocCloseProc) GetProcAddress (hDLL, _T("DocClose"));
SetLicenseKeyA = (lpfnSetLicenseKeyAProc) GetProcAddress (hDLL, _T("SetLicenseKeyA"));


(*SetLicenseKeyA) (_T("Amyuni Pro Eval"), _T("07EFCD....F18A32494BCF8"));

(*DocOpenA)(&doc, _T("c:\\temp\\FifteenPages.pdf"), _T(""));

/* Flags
Signer name 1
Reason for signing 2
Location 4
Associated image 8
Date of signing 32
Signature type 64
*/

(*DocDigitalSignatureA) (doc,
_T("my Cert"),
_T("My Reason"),
_T("c:\\sign.bmp"),
_T("My Location"),
1,
0,
0,
1000,
1000,
1 + 2 + 4 + 8 + 32 + 64);

(*DocSaveA)(doc, _T("c:\\temp\\FifteenPages_sign.pdf"));
(*DocClose)(doc);

FreeLibrary(hDLL);
}
}
lukenine45
Posts: 15
Joined: Wed Jan 11 2006

Post by lukenine45 »

Thank you very much, that helps a lot.

However, DocDigitalSignatureA is failing for me, returning error -1099 (0xfffffbb5).

Can you direct me to a list of possible error codes and what they mean?

Thanks for your help!
Joan
Amyuni Team
Posts: 2799
Joined: Wed Sep 11 2002
Contact:

Post by Joan »

Hello,

Please check that the signature name you are using is a valid signature on your system. Please note that C# is case sensitive.

I don't have a list of the returned values of DocDigitalSignatureA because this is a new function and it is not documented yet. I will try to get this list and post it here.
Jose
Amyuni Team
Posts: 553
Joined: Tue Oct 01 2002
Contact:

Post by Jose »

Hello,

As mentioned by Joanna, the situation you have encountered typically occurs when the digital signatures function is unable to successfully apply the digital signature to the document. From the error you are receiving it looks like you have not specify a valid digital signature value in the DocDigitalSignatureA() function.

Our Digital Signatures option uses the Microsoft signature handler (PPKMS digital signatures types) and the resulting signed documents are viewable in Adobe Reader (windows) versions 4.0 and higher.

Also, the digital signature option of the PDF Converter functions with digital certificates which are stored in the "My" or "Personal" store

Hope this helps?
Joan
Amyuni Team
Posts: 2799
Joined: Wed Sep 11 2002
Contact:

Post by Joan »

Hello,

I will add here the return values of DocDigitalSignatureA():

-0 in case of success.
-100 in case the license does not cover digital signatures.
-1001 in case of lack of resources
-1099 in case signing failed, most probably it is either
1- The page number is incorrect
2- The certificate cannot be used for document signing

Hope this helps.
lukenine45
Posts: 15
Joined: Wed Jan 11 2006

Post by lukenine45 »

Thanks, that helps. I was passing 0 for the page and I guess that was why it was failing. That seems odd though, since I wasn't specifying the associated image flag (8).

When I gave it a page number (1) it then succeeded, but adobe reader still didn't pop up the message to say the document was signed.

I then added an image, and now when I open it up in reader, I can click on the image and it will tell me the document is signed.

So now my question: Why doesn't reader automatically pop up the dialog to say the document is signed? (It does for the other signed PDF's I have) Also, must I give it an image, or is that optional? As it stands, if I don't give an image, I can't tell that it's signed it reader.

Also, is there a way to enumerate the available certificates on a computer? Or must I have the user type in the cert's name?

Oh, I'm using a free certificate from thawte for my testing, if that makes any difference.

Thanks again for all your help...
Joan
Amyuni Team
Posts: 2799
Joined: Wed Sep 11 2002
Contact:

Post by Joan »

Hello,

I tried to open two files having a signature one with picture and the other without picture, in both cases i didn't get any pop up from Adobe Reader and in both cases the files appear to be signed because the signature is there and there is a small sign on the horizontal bar stating that the document is signed.

I am using version 7.0 of Acrobat Reader.
lukenine45
Posts: 15
Joined: Wed Jan 11 2006

Post by lukenine45 »

Thank you once again, I didn't notice the icon there.

One last question: Is there a way to enumerate the available certificates on a computer? Or must I have the user type in the cert's name?
Joan
Amyuni Team
Posts: 2799
Joined: Wed Sep 11 2002
Contact:

Post by Joan »

Hello,

For enumerating the signatures, i am not sure i really get what you mean.

If you are using the End User version of our Converter or Creator, you can, as End User, click on a browse button in our interface that will open a dialog box from which you can select the signature.

You may implement the same thing in your application but i am not sure how to do that, you need to check the Windows APIs that lists the signatures available on the system.
Post Reply