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;
}
Example #2
0
PDFNet::Initialize();
$doc = new PDFDoc();
// Create three layers...
$image_layer = CreateLayer($doc, "Image Layer");
$text_layer = CreateLayer($doc, "Text Layer");
$vector_layer = CreateLayer($doc, "Vector Layer");
// Start a new page ------------------------------------
$page = $doc->PageCreate();
$builder = new ElementBuilder();
// ElementBuilder is used to build new Element objects
$writer = new ElementWriter();
// ElementWriter is used to write Elements to the page
$writer->Begin($page);
// Begin writing to the page
// Add new content to the page and associate it with one of the layers.
$element = $builder->CreateForm(CreateGroup1($doc, $image_layer->GetSDFObj()));
$writer->WriteElement($element);
$element = $builder->CreateForm(CreateGroup2($doc, $vector_layer->GetSDFObj()));
$writer->WriteElement($element);
// Add the text layer to the page...
if (false) {
    // A bit more advanced example of how to create an OCMD text layer that
    // is visible only if text, image and path layers are all 'ON'.
    // An example of how to set 'Visibility Policy' in OCMD.
    $ocgs = $doc->CreateIndirectArray();
    $ocgs->PushBack($image_layer->GetSDFObj());
    $ocgs->PushBack($vector_layer->GetSDFObj());
    $ocgs->PushBack($text_layer->GetSDFObj());
    $text_ocmd = OCMD::Create($doc, $ocgs, OCMD::e_AllOn);
    $element = $builde->CreateForm(CreateGroup3($doc, $text_ocmd->GetSDFObj()));
} else {
}
$new_doc = new PDFDoc();
$imported_pages = $new_doc->ImportPages($import_pages);
// Paper dimension for A3 format in points. Because one inch has
// 72 points, 11.69 inch 72 = 841.69 points
$media_box = new Rect(0.0, 0.0, 1190.88, 841.6900000000001);
$mid_point = $media_box->Width() / 2;
$builder = new ElementBuilder();
$writer = new ElementWriter();
for ($i = 0; $i < $imported_pages->size(); ++$i) {
    // Create a blank new A3 page and place on it two pages from the input document.
    $new_page = $new_doc->PageCreate($media_box);
    $writer->Begin($new_page);
    // Place the first page
    $src_page = $imported_pages->get($i);
    $element = $builder->CreateForm($src_page);
    $sc_x = $mid_point / $src_page->GetPageWidth();
    $sc_y = $media_box->Height() / $src_page->GetPageHeight();
    $scale = $sc_x < $sc_y ? $sc_x : $sc_y;
    // min(sc_x, sc_y)
    $element->GetGState()->SetTransform($scale, 0.0, 0.0, $scale, 0.0, 0.0);
    $writer->WritePlacedElement($element);
    // Place the second page
    ++$i;
    if ($i < $imported_pages->size()) {
        $src_page = $imported_pages->get($i);
        $element = $builder->CreateForm($src_page);
        $sc_x = $mid_point / $src_page->GetPageWidth();
        $sc_y = $media_box->Height() / $src_page->GetPageHeight();
        $scale = $sc_x < $sc_y ? $sc_x : $sc_y;
        // min(sc_x, sc_y)