How do I use PDFDCEX to generate custom appearance for digital signature field?

47 views
Skip to first unread message

Ryan

unread,
Jan 8, 2016, 2:51:06 PM1/8/16
to PDFTron PDFNet SDK
Q:

I want to use PDFDCEX interface to generate the visual appearance of a digital signature field, but am unable to get the correct output.

----------------------------------------------------------------------
A:

The following code can be placed into the PDFDCTest sample code, and will do what are asking. In particular the file doc_to_sign.pdf is located in the PDFNet SDK sample files (Samples/TestFiles).

PDFDoc pdfdoc(inputPath + "doc_to_sign.pdf");
pdfdoc
.InitSecurityHandler();
// Use PDFDCEX to create a temp page and write graphic content to
PDFDCEX pdfDcEx
;
HDC hDC
= pdfDcEx.Begin(pdfdoc);
::StartPage(hDC);
HFONT hFont
= NULL;
LOGFONT lg
= { 0 };
lg
.lfHeight = 100;
lg
.lfWeight = 0;
lg
.lfOutPrecision = OUT_DEFAULT_PRECIS;
lg
.lfClipPrecision = CLIP_EMBEDDED;
wcscpy_s
(lg.lfFaceName, LF_FACESIZE, L"Arial");
hFont
= CreateFontIndirect(&lg);
SelectObject(hDC, hFont);
TextOutW(hDC, 0, 0, L"abc 不 码 abc", 11);
::EndPage(hDC);
pdfDcEx
.End();
// doc_to_sign now has the temp as the last page of the document
// so grab it and then remove from the document page list
Page formx_src = pdfdoc.GetPage(pdfdoc.GetPageCount());
pdfdoc
.PageRemove(pdfdoc.GetPageIterator(pdfdoc.GetPageCount()));
// reduce source page to minimum size to contain visible content
formx_src
.SetCropBox(formx_src.GetVisibleContentBox());
// Now we can write the PDFDCEX generated graphical content to the target annotation
// which in this case is a signature field
Field field = pdfdoc.GetField("Signature1");
Annots::Widget widget(field.GetSDFObj());
ElementWriter writer;
ElementBuilder builder;
writer
.Begin(pdfdoc);
Element formx = builder.CreateForm(formx_src);
writer
.WritePlacedElement(formx);
SDF
::Obj appearance_stream = writer.End();
appearance_stream
.PutRect("BBox", 0, 0, formx_src.GetCropBox().Width(), formx_src.GetCropBox().Height());
widget
.SetAppearance(appearance_stream);
// Done setting graphical appearance
outputFile
= outputPath + "doc_to_sign.pdf";
pdfdoc
.Save(outputFile.c_str(), SDF::SDFDoc::e_remove_unused, NULL);

Same code above can be used to change the appearance of any annotation, not just fields/widgets.

To actually add the digital signature, see the DigitalSignatures samples.

Reply all
Reply to author
Forward
0 new messages