function SignPDF()
{
    $infile = '../../TestFiles/doc_to_sign.pdf';
    $outfile = '../../TestFiles/Output/signed_doc.pdf';
    $certfile = '../../TestFiles/pdftron.pfx';
    $imagefile = '../../TestFiles/signature.jpg';
    $result = true;
    try {
        echo nl2br('Signing PDF document: "' . $infile . '".' . PHP_EOL);
        // Open an existing PDF
        $doc = new PDFDoc($infile);
        // Add an StdSignatureHandler instance to PDFDoc, making sure to keep track of it using the ID returned.
        $sigHandlerId = $doc->AddStdSignatureHandler($certfile, "password");
        // Obtain the signature form field from the PDFDoc via Annotation.
        $sigField = $doc->GetField('Signature1');
        $widgetAnnot = new Widget($sigField->GetSDFObj());
        # Tell PDFNetC to use the SignatureHandler created to sign the new signature form field.
        $sigDict = $sigField->UseSignatureHandler($sigHandlerId);
        // 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 verification.');
        // Create a signature appearance
        $apWriter = new ElementWriter();
        $apBuilder = new ElementBuilder();
        $apWriter->Begin($doc->GetSDFDoc());
        $sigImg = Image::Create($doc->GetSDFDoc(), $imagefile);
        $w = floatval($sigImg->GetImageWidth());
        $h = floatval($sigImg->GetImageHeight());
        $apElement = $apBuilder->CreateImage($sigImg, 0.0, 0.0, $w, $h);
        $apWriter->WritePlacedElement($apElement);
        $apObj = $apWriter->End();
        $apObj->PutRect('BBox', 0.0, 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, 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, PDFNetC will also sign the document using the information
        // provided.
        $doc->Save($outfile, 0);
        echo nl2br('Finished signing PDF document' . PHP_EOL);
        $doc->Close();
    } catch (Exception $e) {
        echo nl2br($e->getMessage() . PHP_EOL);
        echo nl2br($e->getTraceAsString() . PHP_EOL);
        $result = false;
    }
    return $result;
}
        case Field::e_text:
            echo nl2br("Text\n");
            // Edit all variable text in the document
            $itr->Current()->SetValue("This is a new value. The old one was: " . $str_val);
            break;
        case Field::e_choice:
            echo nl2br("Choice\n");
            break;
        case Field::e_signature:
            echo nl2br("Signature\n");
            break;
    }
    echo "------------------------------\n";
}
// Search for a specific field
$f = $doc->GetField("employee.name.first");
if ($f) {
    echo nl2br("Field search for " . $f->GetName() . " was successful\n");
} else {
    echo nl2br("Field search failed\n");
}
// Regenerate field appearances.
$doc->RefreshFieldAppearances();
$doc->Save($output_path . "forms_test_edit.pdf", 0);
echo nl2br("Done.\n");
//----------------------------------------------------------------------------------
// Sample: Form templating
// Replicate pages and form data within a document. Then rename field names to make
// them unique.
//----------------------------------------------------------------------------------
// Sample: Copying the page with forms within the same document