# Digital Signature

This is a beta feature and may be changed in subsequent versions. To demonstrate how to sign and certify a document, we provide an openssl-based signature back-end in ./server/pkcs7in the SDK package.

# Back-end

The signature backend folder ./server/pkcs7/bin/ contains a program pkcs7.exe which is derivative from openssl, and the signature.cpp that runs the source code. The main functions are:

  • digest(POST)
  • sign(POST)
  • verify(POST)

# Start node.js service

The signature back requires node.js to run. On the command line window, enter into ./server/pkcs7, execute npm i to install dependencies, then execute node ./index.js to start node.js which will listen on 7777 port.

# Front-end

Start a web service and run Complete_webViewer.You may want to check out Quickly run samples.

# Sign a document

Open a PDF document you want to place a signature on, open the Browser Developer Tool and run the code below on Console to sign a document.

  var pdfviewer = await pdfui.getPDFViewer();
  //var pdfviewer = new PDFViewerCtrl.PDFViewer({...})
await pdfviewer.registerSignatureHandler(
	//register backend apis, that will be called in the process of sign or verify.
	'Adobe.PPKLite',
	'adbe.pkcs7.sha1',
	{
		sign: 'http://localhost:7777/sign',
		verify: 'http://localhost:7777/verify',
		digest: 'http://localhost:7777/digest',
	}
);
var pdfdoc = await pdfui.getCurrentPDFDoc();

file = await pdfdoc.sign({
    rect:{left:0,right:100,top:400,bottom:350},
	pageIndex: 0,
	signer: 'signer',
	reason: 'reason',
	email: 'email',
	DN: 'DN',
	location: 'loc',
	text: 'text',
	filter: 'Adobe.PPKLite',
	subfilter: 'adbe.pkcs7.sha1',
	hashAlgorithm: 0,
});
saveAs(file,"signed.pdf"); //save file

After signing, if you want to verify it, you should re-open the signed.pdf document, and then continue the following steps.

# Verify a dccument

Foxit PDF SDK for Web uses PDFDoc.verifySignature() to Verify. If the backend APIs haven't yet been registered, you should call registerSignatureHandler() to regsiter them first.

var pdfviewer = await pdfui.getPDFViewer();
  //var pdfviewer = new PDFViewerCtrl.PDFViewer({...})
await pdfviewer.registerSignatureHandler(
	'Adobe.PPKLite',
	'adbe.pkcs7.sha1',
	{
		sign: 'http://localhost:7777/sign',
		verify: 'http://localhost:7777/verify',
		digest: 'http://localhost:7777/digest',
	}
);
var pdfdoc = await pdfui.getCurrentPDFDoc();
var pdfform = pdfdoc.getPDFForm();
var signature = pdfform.getField('Signature_0');
if(!signature) throw new Error('signature not found');
var state = await pdfdoc.verifySignature(signature);

For the constant variable, you may want to check out API Reference PDF.constant.Signature_State.