Example #1
0
function CreateImageTilingPattern($doc)
{
    $writer = new ElementWriter();
    $builder = new ElementBuilder();
    // Create a new pattern content stream - a single bitmap object ----------
    $writer->Begin($doc->GetSDFDoc());
    global $input_path;
    $image = Image::Create($doc->GetSDFDoc(), $input_path . "dice.jpg");
    $img_element = $builder->CreateImage($image, 0.0, 0.0, (double) $image->GetImageWidth(), (double) $image->GetImageHeight());
    $writer->WritePlacedElement($img_element);
    $pattern_dict = $writer->End();
    // Initialize pattern dictionary. For details on what each parameter represents please
    // refer to Table 4.22 (Section '4.6.2 Tiling Patterns') in PDF Reference Manual.
    $pattern_dict->PutName("Type", "Pattern");
    $pattern_dict->PutNumber("PatternType", 1);
    // TilingType - Constant spacing.
    $pattern_dict->PutNumber("TilingType", 1);
    // This is a Type1 pattern - A colored tiling pattern.
    $pattern_dict->PutNumber("PaintType", 1);
    // Set bounding box
    $pattern_dict->PutRect("BBox", -253, 0, 253, 545);
    // Create and set the matrix
    $pattern_mtx = new Matrix2D(0.3, 0.0, 0.0, 0.3, 0.0, 0.0);
    $pattern_dict->PutMatrix("Matrix", $pattern_mtx);
    // Set the desired horizontal and vertical spacing between pattern cells,
    // measured in the pattern coordinate system.
    $pattern_dict->PutNumber("XStep", 300);
    $pattern_dict->PutNumber("YStep", 300);
    return $pattern_dict;
    // finished creating the Pattern resource
}
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 #3
0
function CreateGroup1($doc, $layer)
{
    $writer = new ElementWriter();
    $writer->Begin($doc->GetSDFDoc());
    global $input_path;
    // Create an Image that can be reused in the document or on the same page.
    $img = Image::Create($doc->GetSDFDoc(), $input_path . "peppers.jpg");
    $builder = new ElementBuilder();
    $element = $builder->CreateImage($img, new Matrix2D((double) $img->GetImageWidth() / 2, -145.0, 20.0, (double) $img->GetImageHeight() / 2, 200.0, 150.0));
    $writer->WritePlacedElement($element);
    $gstate = $element->GetGState();
    // use the same image (just change its matrix)
    $gstate->SetTransform(200.0, 0.0, 0.0, 300.0, 50.0, 450.0);
    $writer->WritePlacedElement($element);
    // use the same image again (just change its matrix).
    $writer->WritePlacedElement($builder->CreateImage($img, 300.0, 600.0, 200.0, -150.0));
    $grp_obj = $writer->End();
    // Indicate that this form (content group) belongs to the given layer (OCG).
    $grp_obj->PutName("Subtype", "Form");
    $grp_obj->Put("OC", $layer);
    $grp_obj->PutRect("BBox", 0, 0, 1000, 1000);
    // Set the clip box for the content.
    return $grp_obj;
}
Example #4
0
$input_path = getcwd() . "/../../TestFiles/";
$output_path = $input_path . "Output/";
echo nl2br("-------------------------------------------------\n");
$doc = new PDFDoc();
$builder = new ElementBuilder();
// Used to build new Element objects
$writer = new ElementWriter();
// Used to write Elements to the page
$page = $doc->PageCreate();
// Start a new page
$writer->Begin($page);
// Begin writing to this page
// ----------------------------------------------------------
// Add JPEG image to the output file
$img = Image::Create($doc->GetSDFDoc(), $input_path . "peppers.jpg");
$element = $builder->CreateImage($img, 50.0, 500.0, (double) $img->GetImageWidth() / 2, (double) $img->GetImageHeight() / 2);
$writer->WritePlacedElement($element);
// ----------------------------------------------------------
// Add a PNG image to the output file
$img = Image::Create($doc->GetSDFDoc(), $input_path . "butterfly.png");
$element = $builder->CreateImage($img, new Matrix2D(100.0, 0.0, 0.0, 100.0, 300.0, 500.0));
$writer->WritePlacedElement($element);
// ----------------------------------------------------------
// The following section is excluded because gif is not supported in Linux
// Add a GIF image to the output file
//$img = Image::Create($doc->GetSDFDoc(), $input_path."pdfnet.gif");
//$element = $builder->CreateImage($img, new Matrix2D((double)($img->GetImageWidth()), 0.0, 0.0, (double)($img->GetImageHeight()), 50.0, 350.0));
//$writer->WritePlacedElement($element);
// ----------------------------------------------------------
// Add a TIFF image to the output file
$img = Image::Create($doc->GetSDFDoc(), $input_path . "grayscale.tif");
Example #5
0
function Create3DAnnotation($doc, $annots)
{
    // ---------------------------------------------------------------------------------
    // Create a 3D annotation based on U3D content. PDF 1.6 introduces the capability
    // for collections of three-dimensional objects, such as those used by CAD software,
    // to be embedded in PDF files.
    $link_3D = $doc->CreateIndirectDict();
    $link_3D->PutName("Subtype", "3D");
    // Annotation location on the page
    $link_3D_rect = new Rect(25.0, 180.0, 585.0, 643.0);
    $link_3D->PutRect("Rect", $link_3D_rect->x1, $link_3D_rect->y1, $link_3D_rect->x2, $link_3D_rect->y2);
    $annots->PushBack($link_3D);
    // The 3DA entry is an activation dictionary (see Table 9.34 in the PDF Reference Manual)
    // that determines how the state of the annotation and its associated artwork can change.
    $activation_dict_3D = $link_3D->PutDict("3DA");
    // Set the annotation so that it is activated as soon as the page containing the
    // annotation is opened. Other options are: PV (page view) and XA (explicit) activation.
    $activation_dict_3D->PutName("A", "PO");
    // Embed U3D Streams (3D Model/Artwork).
    global $input_path;
    $u3d_file = new MappedFile($input_path . "dice.u3d");
    $u3d_reader = new FilterReader($u3d_file);
    // To embed 3D stream without compression, you can omit the second parameter in CreateIndirectStream.
    $u3d_data_dict = $doc->CreateIndirectStream($u3d_reader, new FlateEncode(new Filter()));
    $u3d_data_dict->PutName("Subtype", "U3D");
    $link_3D->Put("3DD", $u3d_data_dict);
    // Set the initial view of the 3D artwork that should be used when the annotation is activated.
    $view3D_dict = $link_3D->PutDict("3DV");
    $view3D_dict->PutString("IN", "Unnamed");
    $view3D_dict->PutString("XN", "Default");
    $view3D_dict->PutName("MS", "M");
    $view3D_dict->PutNumber("CO", 27.5);
    // A 12-element 3D transformation matrix that specifies a position and orientation
    // of the camera in world coordinates.
    $tr3d = $view3D_dict->PutArray("C2W");
    $tr3d->PushBackNumber(1);
    $tr3d->PushBackNumber(0);
    $tr3d->PushBackNumber(0);
    $tr3d->PushBackNumber(0);
    $tr3d->PushBackNumber(0);
    $tr3d->PushBackNumber(-1);
    $tr3d->PushBackNumber(0);
    $tr3d->PushBackNumber(1);
    $tr3d->PushBackNumber(0);
    $tr3d->PushBackNumber(0);
    $tr3d->PushBackNumber(-27.5);
    $tr3d->PushBackNumber(0);
    // Create annotation appearance stream, a thumbnail which is used during printing or
    // in PDF processors that do not understand 3D data.
    $ap_dict = $link_3D->PutDict("AP");
    $builder = new ElementBuilder();
    $writer = new ElementWriter();
    $writer->Begin($doc->GetSDFDoc());
    $thumb_pathname = $input_path . "dice.jpg";
    $image = Image::Create($doc->GetSDFDoc(), $thumb_pathname);
    $writer->WritePlacedElement($builder->CreateImage($image, 0.0, 0.0, $link_3D_rect->Width(), $link_3D_rect->Height()));
    $normal_ap_stream = $writer->End();
    $normal_ap_stream->PutName("Subtype", "Form");
    $normal_ap_stream->PutRect("BBox", 0, 0, $link_3D_rect->Width(), $link_3D_rect->Height());
    $ap_dict->Put("N", $normal_ap_stream);
}
# Relative path to the folder containing the test files.
$input_path = getcwd() . "/../../TestFiles/";
$output_path = $input_path . "Output/";
PDFNet::Initialize();
$doc = new PDFDoc();
$builder = new ElementBuilder();
// ElementBuilder is used to build new Element objects
$writer = new ElementWriter();
// ElementWriter is used to write Elements to the page
// Start a new page ------------------------------------
$page = $doc->PageCreate(new Rect(0.0, 0.0, 612.0, 794.0));
$writer->Begin($page);
// begin writing to the page
// Create an Image that can be reused in the document or on the same page.
$img = Image::Create($doc->GetSDFDoc(), $input_path . "peppers.jpg");
$element = $builder->CreateImage($img, new Matrix2D((double) ($img->GetImageWidth() / 2), -145.0, 20.0, (double) ($img->GetImageHeight() / 2), 200.0, 150.0));
$writer->WritePlacedElement($element);
$gstate = $element->GetGState();
// use the same image (just change its matrix)
$gstate->SetTransform(200.0, 0.0, 0.0, 300.0, 50.0, 450.0);
$writer->WritePlacedElement($element);
// use the same image again (just change its matrix).
$writer->WritePlacedElement($builder->CreateImage($img, 300.0, 600.0, 200.0, -150.0));
$writer->End();
// save changes to the current page
$doc->PagePushBack($page);
// Start a new page ------------------------------------
// Construct and draw a path object using different styles
$page = $doc->PageCreate(new Rect(0.0, 0.0, 612.0, 794.0));
$writer->Begin($page);
// begin writing to this page