Some test text!

menu
Sign a documentkeyboard_arrow_down

Sign a PDF document in UWP

To sign an existing approval signature field in a PDF Document:

// Open an existing PDF
using (PDFDoc doc = new PDFDoc(docpath))
{
    // Create a new instance of the SignatureHandler.
    MySignatureHandler sh = new MySignatureHandler();

    // Add the SignatureHandler instance to PDFDoc, making sure to keep track of it using the ID returned.
    var sigHandlerId = doc.AddSignatureHandler(sh);

    // Retrieve the unsigned approval signature field.
    Field found_approval_field = doc.GetField(approval_field_name);

    Widget widgetAnnot = new Widget(found_approval_field.GetSDFObj());

    // Tell PDFNet to use the SignatureHandler created to sign the new signature form field.
    Obj sigDict = found_approval_field.UseSignatureHandler(sigHandlerId);
    
    // (OPTIONAL) Add more information to the signature dictionary.
    sigDict.PutName("SubFilter", "adbe.pkcs7.detached");
    sigDict.PutString("Name", "PDFTron");
    sigDict.PutString("Location", "Vancouver, BC");
    sigDict.PutString("Reason", "Document approval.");
    
    // (OPTIONAL) Add an appearance to the signature field.
    ElementWriter apWriter = new ElementWriter();
    ElementBuilder apBuilder = new ElementBuilder();
    apWriter.Begin(doc.GetSDFDoc());
    Image sigImg = Image.Create(doc.GetSDFDoc(), image_file_path);
    double w = sigImg.GetImageWidth(), h = sigImg.GetImageHeight();
    Element apElement = apBuilder.CreateImage(sigImg, 0, 0, w, h);
    apWriter.WritePlacedElement(apElement);
    Obj apObj = apWriter.End();
    apObj.PutRect("BBox", 0, 0, w, h);
    apObj.PutName("Subtype", "Form");
    apObj.PutName("Type", "XObject");
    apWriter.Begin(doc.GetSDFDoc());
    apElement = apBuilder.CreateForm(apObj);
    apWriter.WritePlacedElement(apElement);
    apObj = apWriter.End();
    apObj.PutRect("BBox", 0, 0, w, h);
    apObj.PutName("Subtype", "Form");
    apObj.PutName("Type", "XObject");

    widgetAnnot.SetAppearance(apObj);
    widgetAnnot.RefreshAppearance();
    
    // Save the PDFDoc. Once the method below is called, PDFNet will also sign the document using the information
    // provided.
    await doc.SaveAsync(output_file_path, 0);
}

 class MySignatureHandler : ISignatureHandler
    {
        private List<byte> m_data = null;

        public MySignatureHandler()
        {
            m_data = new List<byte>();
        }

        public void AppendData(byte[] data)
        {
            m_data.AddRange(data);
        }

        public byte[] CreateSignature()
        {
            return (new byte[] { 0x00, 0x00, 0x00, 0x00 });
        }

        public String GetName()
        {
            return "Adobe.PPKLite";
        }

        public bool Reset()
        {
            m_data.Clear();
            return (true);
        }
    }

Digitally sign PDF files
Full code sample which demonstrates using the digital signature API to digitally sign and/or certify PDF documents.

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.