Ejemplo n.º 1
0
}
$in1_doc->Save($output_path . "newsletter_page_insert.pdf", 0);
echo nl2br("Done. Result saved in newsletter_page_insert.pdf...\n");
//Close the open document to free up document memory sooner than waiting for the
//garbage collector
$in1_doc->Close();
$in2_doc->Close();
// Sample 5 - Replicate pages within a single document
echo nl2br("_______________________________________________\n");
echo nl2br("Sample 5 - Replicate pages within a single document...\n");
echo nl2br("Opening the input pdf...\n");
$doc = new PDFDoc($input_path . "newsletter.pdf");
$doc->InitSecurityHandler();
// Replicate the cover page three times (copy page #1 and place it before the
// seventh page in the document page sequence)
$cover = $doc->GetPage(1);
$p7 = $doc->GetPageIterator(7);
$doc->PageInsert($p7, $cover);
$doc->PageInsert($p7, $cover);
$doc->PageInsert($p7, $cover);
// Replicate the cover page two more times by placing it before and after
// existing pages.
$doc->PagePushFront($cover);
$doc->PagePushBack($cover);
$doc->Save($output_path . "newsletter_page_clone.pdf", 0);
echo nl2br("Done. Result saved in newsletter_page_clone.pdf...\n");
$doc->Close();
// Sample 6 - Use ImportPages() in order to copy multiple pages at once
// in order to preserve shared resources between pages (e.g. images, fonts,
// colorspaces, etc.)
echo nl2br("_______________________________________________\n");
Ejemplo n.º 2
0
$zoom_rect = new Rect(216.0, 522.0, 330.0, 600.0);
$page->SetCropBox($zoom_rect);
// Set the page crop box.
// Select the crop region to be used for drawing.
$draw->SetPageBox(Page::e_crop);
$draw->SetDPI(900);
// Set the output image resolution to 900 DPI.
$draw->Export($page, $output_path . "tiger_zoom_900dpi.png", "PNG");
echo nl2br("Example 5: " . $output_path . "tiger_zoom_900dpi.png. Done.\n");
$draw->SetImageSize(50, 50);
// Set the thumbnail to be 50x50 pixel image.
$draw->Export($page, $output_path . "tiger_zoom_50x50.png", "PNG");
echo nl2br("Example 5: " . $output_path . "tiger_zoom_50x50.png. Done.\n");
$cmyk_hint = $hint_set->CreateDict();
$cmyk_hint->PutName("ColorSpace", "CMYK");
//--------------------------------------------------------------------------------
// Example 6) Convert the first PDF page to CMYK TIFF at 92 DPI.
// A three step tutorial to convert PDF page to an image
echo nl2br("Example 6:\n");
// A) Open the PDF document.
$doc = new PDFDoc($input_path . "tiger.pdf");
// Initialize the security handler, in case the PDF is encrypted.
$doc->InitSecurityHandler();
// B) The output resolution is set to 92 DPI.
$draw->SetDPI(92);
// C) Rasterize the first page in the document and save the result as TIFF.
$pg = $doc->GetPage(1);
$draw->Export($pg, $output_path . "out1.tif", "TIFF", $cmyk_hint);
echo nl2br("Example 6: Result saved in " . $output_path . "out1.tif\n");
$doc->Close();
echo "Done.";
Ejemplo n.º 3
0
$doc->Save($output_path . "pdf_layers.pdf", SDFDoc::e_linearized);
echo nl2br("Done.\n");
// The following is a code snippet shows how to selectively render
// and export PDF layers.
$doc = new PDFDoc($output_path . "pdf_layers.pdf");
$doc->InitSecurityHandler();
if (!$doc->HasOC()) {
    echo nl2br("The document does not contain 'Optional Content'\n");
} else {
    $init_cfg = $doc->GetOCGConfig();
    $ctx = new Context($init_cfg);
    $pdfdraw = new PDFDraw();
    $pdfdraw->SetImageSize(1000, 1000);
    $pdfdraw->SetOCGContext($ctx);
    // Render the page using the given OCG context.
    $page = $doc->GetPage(1);
    // Get the first page in the document.
    $pdfdraw->Export($page, $output_path . "pdf_layers_default.png");
    // Disable drawing of content that is not optional (i.e. is not part of any layer).
    $ctx->SetNonOCDrawing(false);
    // Now render each layer in the input document to a separate image.
    $ocgs = $doc->GetOCGs();
    // Get the array of all OCGs in the document.
    if ($ocgs != null) {
        $sz = $ocgs->Size();
        for ($i = 0; $i < $sz; ++$i) {
            $ocg = new Group($ocgs->GetAt($i));
            $ctx->ResetStates(false);
            $ctx->SetState($ocg, true);
            $fname = $output_path . "pdf_layers_" . $ocg->GetName() . ".png";
            echo nl2br($fname . "\n");
Ejemplo n.º 4
0
    $doc->Close();
    echo nl2br("-----------------------------------------------------------\n");
}
if ($example5_low_level) {
    $doc = new PDFDoc($input_path);
    $doc->InitSecurityHandler();
    // Example 1. Extract all text content from the document
    $reader = new ElementReader();
    //  Read every page
    for ($itr = $doc->GetPageIterator(); $itr->HasNext(); $itr->Next()) {
        $reader->Begin($itr->Current());
        DumpAllText($reader);
        $reader->End();
    }
    // Example 2. Extract text content based on the
    // selection rectangle.
    echo nl2br("\n----------------------------------------------------");
    echo nl2br("\nExtract text based on the selection rectangle.");
    echo nl2br("\n----------------------------------------------------\n");
    $first_page = $doc->GetPage(1);
    $s1 = ReadTextFromRect($first_page, new Rect(27.0, 392.0, 563.0, 534.0), $reader);
    echo nl2br("\nField 1: " . $s1);
    $s1 = ReadTextFromRect($first_page, new Rect(28.0, 551.0, 106.0, 623.0), $reader);
    echo nl2br("\nField 2: " . $s1);
    $s1 = ReadTextFromRect($first_page, new Rect(208.0, 550.0, 387.0, 621.0), $reader);
    echo nl2br("\nField 3: " . $s1);
    // ...
    $doc->Close();
    echo nl2br("-----------------------------------------------------------\n");
    echo nl2br("Done.\n");
}
Ejemplo n.º 5
0
$s->StampText($doc, "Goodbye\nMoon", $ps);
$doc->Save($output_path . $input_filename . ".ex5.pdf", SDFDoc::e_linearized);
$doc->Close();
//--------------------------------------------------------------------------------
// Example 6) Add first page as stamp to all even pages
$doc = new PDFDoc($input_path . $input_filename);
$doc->InitSecurityHandler();
$fish_doc = new PDFDoc($input_path . "fish.pdf");
$fish_doc->InitSecurityHandler();
$s = new Stamper(Stamper::e_relative_scale, 0.3, 0.3);
$s->SetOpacity(1);
$s->SetRotation(270);
$s->SetAsBackground(true);
$s->SetPosition(0.5, 0.5, true);
$s->SetAlignment(Stamper::e_horizontal_left, Stamper::e_vertical_bottom);
$page_one = $fish_doc->GetPage(1);
$ps = new PageSet(1, $doc->GetPageCount(), PageSet::e_even);
$s->StampPage($doc, $page_one, $ps);
$doc->Save($output_path . $input_filename . ".ex6.pdf", SDFDoc::e_linearized);
$doc->Close();
//--------------------------------------------------------------------------------
// Example 7) Add image stamp at top right corner in every pages
$doc = new PDFDoc($input_path . $input_filename);
$doc->InitSecurityHandler();
$s = new Stamper(Stamper::e_relative_scale, 0.1, 0.1);
$s->SetOpacity(0.8);
$s->SetRotation(135);
$s->SetAsBackground(false);
$s->ShowsOnPrint(false);
$s->SetAlignment(Stamper::e_horizontal_left, Stamper::e_vertical_top);
$s->SetPosition(10.0, 10.0);
Ejemplo n.º 6
0
for ($i = 1; $i <= $num_pages; ++$i) {
    $itr = $doc->GetPageIterator(2 * $i - 1);
    $reader->Begin($itr->Current());
    $new_page = $doc->PageCreate($itr->Current()->GetMediaBox());
    $next_page = $itr;
    $next_page->Next();
    $doc->PageInsert($next_page, $new_page);
    $writer->Begin($new_page);
    while (($element = $reader->Next()) != null) {
        //if ($element->GetType() == Element::e_path)
        $writer->WriteElement($element);
    }
    $writer->End();
    $reader->End();
}
$doc->Save($output_path . "doc_memory_edit.pdf", SDFDoc::e_remove_unused);
// Save the document to a memory buffer.
$buffer = $doc->Save(SDFDoc::e_remove_unused);
// Write the contents of the buffer to the disk
$outfile = fopen($output_path . "doc_memory_edit.txt", "w");
fwrite($outfile, $buffer);
fclose($outfile);
// Read some data from the file stored in memory
$reader->Begin($doc->GetPage(1));
while (($element = $reader->Next()) != null) {
    if ($element->GetType() == Element::e_path) {
        echo "Path, ";
    }
}
$reader->End();
echo nl2br("\n\nDone. Result saved in doc_memory_edit.pdf and doc_memory_edit.txt ...\n");
function CertifyPDF()
{
    $infile = '../../TestFiles/newsletter.pdf';
    $outfile = '../../TestFiles/Output/newsletter_certified.pdf';
    $certfile = '../../TestFiles/pdftron.pfx';
    $result = true;
    try {
        echo nl2br('Certifying 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");
        // Create a new signature form field in the PDFDoc.
        $sigField = $doc->FieldCreate('Signature1', Field::e_signature);
        // Assign the form field as an annotation widget to the PDFDoc so that a signature appearance can be added.
        $page1 = $doc->GetPage(1);
        $widgetAnnot = Widget::Create($doc->GetSDFDoc(), new Rect(0.0, 0.0, 0.0, 0.0), $sigField);
        $page1->AnnotPushBack($widgetAnnot);
        $widgetAnnot->SetPage($page1);
        $widgetObj = $widgetAnnot->GetSDFObj();
        $widgetObj->PutNumber('F', 132.0);
        $widgetObj->PutName('Type', 'Annot');
        // 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.');
        // Appearance can be added to the widget annotation. Please see the "SignPDF()" function for details.
        // Add this sigDict as DocMDP in Perms dictionary from root
        $root = $doc->GetRoot();
        $perms = $root->PutDict('Perms');
        // add the sigDict as DocMDP (indirect) in Perms
        $perms->Put('DocMDP', $sigDict);
        // add the additional DocMDP transform params
        $refObj = $sigDict->PutArray("Reference");
        $transform = $refObj->PushBackDict();
        $transform->PutName("TransformMethod", "DocMDP");
        $transform->PutName("Type", "SigRef");
        $transformParams = $transform->PutDict("TransformParams");
        $transformParams->PutNumber("P", 1);
        // Set permissions as necessary.
        $transformParams->PutName("Type", "TransformParams");
        $transformParams->PutName("V", "1.2");
        // 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 certifying 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;
}
Ejemplo n.º 8
0
// Finish the block of text
$writer->WriteElement($builder->CreateTextEnd());
// Draw an image that will be clipped by the above text
$writer->WriteElement($builder->CreateImage($img, 10.0, 100.0, 1300.0, 720.0));
$writer->End();
// save changes to the current page
$doc->PagePushBack($page);
// Start a new page ------------------------------------
//
// The example illustrates how to embed the external font in a PDF document.
// The example also shows how ElementReader can be used to copy and modify
// Elements between pages.
$reader = new ElementReader();
// Start reading Elements from the last page. We will copy all Elements to
// a new page but will modify the font associated with text.
$reader->Begin($doc->GetPage($doc->GetPageCount()));
$page = $doc->PageCreate(new Rect(0.0, 0.0, 1300.0, 794.0));
$writer->Begin($page);
// begin writing to this page
$builder->Reset();
// Reset the GState to default
// Embed an external font in the document.
$font = Font::CreateTrueTypeFont($doc->GetSDFDoc(), $input_path . "font.ttf");
while (($element = $reader->Next()) != null) {
    if ($element->GetType() == Element::e_text) {
        $element->GetGState()->SetFont($font, 12);
    }
    $writer->WriteElement($element);
}
$reader->End();
$writer->End();
} 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
$doc = new PDFDoc($output_path . "forms_test1.pdf");
$doc->InitSecurityHandler();
$src_page = $doc->GetPage(1);
$doc->PagePushBack($src_page);
// Append several copies of the first page
$doc->PagePushBack($src_page);
// Note that forms are successfully copied
$doc->PagePushBack($src_page);
$doc->PagePushBack($src_page);
// Now we rename fields in order to make every field unique.
// You can use this technique for dynamic template filling where you have a 'master'
// form page that should be replicated, but with unique field names on every page.
RenameAllFields($doc, "employee.name.first");
RenameAllFields($doc, "employee.name.last");
RenameAllFields($doc, "employee.name.check1");
RenameAllFields($doc, "submit");
$doc->Save($output_path . "forms_test1_cloned.pdf", 0);
echo nl2br("Done.\n");