// Replace the Prefix in the decimal labels (from example 1). $label = $doc->GetPageLabel(4); if ($label->IsValid()) { $label->SetPrefix("A"); $label->SetStart(1); } // Add a new label $new_label = PageLabel::Create($doc->GetSDFDoc(), PageLabel::e_decimal, "B", 1); $doc->SetPageLabel(10, $new_label); // starting from page 10. $doc->Save($output_path . "newsletter_with_pagelabels_modified.pdf", SDFDoc::e_linearized); echo nl2br("Done. Result saved in newsletter_with_pagelabels_modified.pdf...\n"); $page_num = $doc->GetPageCount(); for ($i = 1; $i <= $page_num; ++$i) { echo "Page number: " . $i; $label = $doc->GetPageLabel($i); if ($label->IsValid()) { echo nl2br(" Label: " . $label->GetLabelTitle($i) . "\n"); } else { echo nl2br(" No Label.\n"); } } $doc->Close(); //----------------------------------------------------------- // Example 4: Delete all page labels in an existing PDF document. //----------------------------------------------------------- $doc = new PDFDoc($output_path . "newsletter_with_pagelabels.pdf"); $doc->GetRoot()->Erase("PageLabels"); $doc->Save($output_path . "newsletter_with_pagelabels_removed.pdf", SDFDoc::e_linearized); echo nl2br("Done. Result saved in newsletter_with_pagelabels_removed.pdf...\n"); // ...
$enc->PushBackName("Lossy"); $img = Image::Create($doc->GetSDFDoc(), $input_path . "multipage.tif"); $element = $builder->CreateImage($img, new Matrix2D(612.0, 0.0, 0.0, 794.0, 0.0, 0.0)); $writer->WritePlacedElement($element); $writer->End(); // Save the page $doc->PagePushBack($page); // Add the page to the document page sequence // ---------------------------------------------------------- // Add a JPEG2000 (JP2) image to the output file // Create a new page $page = $doc->PageCreate(); $writer->Begin($page); // Begin writing to the page // Embed the image $img = Image::Create($doc->GetSDFDoc(), $input_path . "palm.jp2"); // Position the image on the page $element = $builder->CreateImage($img, new Matrix2D((double) $img->GetImageWidth(), 0.0, 0.0, (double) $img->GetImageHeight(), 96.0, 80.0)); $writer->WritePlacedElement($element); // Write 'JPEG2000 Sample' text string under the image $writer->WriteElement($builder->CreateTextBegin(Font::Create($doc->GetSDFDoc(), Font::e_times_roman), 32.0)); $element = $builder->CreateTextRun("JPEG2000 Sample"); $element->SetTextMatrix(1.0, 0.0, 0.0, 1.0, 190.0, 30.0); $writer->WriteElement($element); $writer->WriteElement($builder->CreateTextEnd()); $writer->End(); // Finish writing to the page $doc->PagePushBack($page); $doc->Save($output_path . "addimage.pdf", SDFDoc::e_linearized); $doc->Close(); echo nl2br("Done. Result saved in addimage.pdf...\n");
$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"); echo nl2br("Sample 6 - Preserving shared resources using ImportPages...\n"); echo nl2br("Opening the input pdf...\n"); $in_doc = new PDFDoc($input_path . "newsletter.pdf"); $in_doc->InitSecurityHandler(); $new_doc = new PDFDoc(); $copy_pages = new VectorPage(); for ($itr = $in_doc->GetPageIterator(); $itr->HasNext(); $itr->Next()) { $copy_pages->push($itr->Current()); } $imported_pages = $new_doc->ImportPages($copy_pages); for ($i = 0; $i < $imported_pages->size(); ++$i) { $new_doc->PagePushFront($imported_pages->get($i)); // Order pages in reverse order. // Use PagePushBack() if you would like to preserve the same order. } $new_doc->Save($output_path . "newsletter_import_pages.pdf", 0); echo nl2br("Done. Result saved in newsletter_import_pages.pdf...\n\n" . "Note that the output file size is less than half the size\n" . "of the file produced using individual page copy operations\n" . "between two documents\n");
if (!$doc->InitSecurityHandler()) { $success = false; echo "The password is: test\n"; for ($count = 0; $count < 3; $count++) { echo "A password required to open the document.\n" . "Please enter the password:"******"The password is correct.\n"; break; } else { if ($count < 3) { echo "The password is incorrect, please try again\n"; } } } if (!$success) { echo "Document authentication error....\n"; PDFNet::Terminate(); return; } $hdlr = $doc->GetSecurityHandler(); echo "Document Open Password: "******"\n"; echo "Permissions Password: "******"\n"; echo "Permissions: " . "\n\tHas 'owner' permissions: " . $hdlr->GetPermission(SecurityHandler::e_owner) . "\n\tOpen and decrypt the document: " . $hdlr->GetPermission(SecurityHandler::e_doc_open) . "\n\tAllow content extraction: " . $hdlr->GetPermission(SecurityHandler::e_extract_content) . "\n\tAllow full document editing: " . $hdlr->GetPermission(SecurityHandler::e_doc_modify) . "\n\tAllow printing: " . $hdlr->GetPermission(SecurityHandler::e_print) . "\n\tAllow high resolution printing: " . $hdlr->GetPermission(SecurityHandler::e_print_high) . "\n\tAllow annotation editing: " . $hdlr->GetPermission(SecurityHandler::e_mod_annot) . "\n\tAllow form fill: " . $hdlr->GetPermission(SecurityHandler::e_fill_forms) . "\n\tAllow content extraction for accessibility: " . $hdlr->GetPermission(SecurityHandler::e_access_support) . "\n\tAllow document assembly: " . $hdlr->GetPermission(SecurityHandler::e_assemble_doc) . "\n"; } // remove all security on the document $doc->RemoveSecurity(); $doc->Save($output_path . "not_secured.pdf", 0); $doc->Close(); echo "Test Completed.\n";
<?php //--------------------------------------------------------------------------------------- // Copyright (c) 2001-2014 by PDFTron Systems Inc. All Rights Reserved. // Consult LICENSE.txt regarding license information. //--------------------------------------------------------------------------------------- include "../../../PDFNetC/Lib/PDFNetPHP.php"; PDFNet::Initialize(); // Relative path to the folder containing the test files. $input_path = getcwd() . "/../../TestFiles/"; $output_path = $input_path . "Output/"; // Test - Adjust the position of content within the page. echo nl2br("_______________________________________________\n"); echo nl2br("Opening the input pdf...\n"); $input_doc = new PDFDoc($input_path . "tiger.pdf"); $input_doc->InitSecurityHandler(); $pg_itr1 = $input_doc->GetPageIterator(); $media_box = new Rect($pg_itr1->Current()->GetMediaBox()); $media_box->x1 -= 200; $media_box->x2 -= 200; $media_box->Update(); $input_doc->Save($output_path . "tiger_shift.pdf", 0); $input_doc->Close(); echo nl2br("Done. Result saved in tiger_shift...\n");
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");
$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) $element->GetGState()->SetTransform($scale, 0.0, 0.0, $scale, $mid_point, 0.0); $writer->WritePlacedElement($element); } $writer->End(); $new_doc->PagePushBack($new_page); } $new_doc->Save($output_path, SDFDoc::e_linearized); echo nl2br("Done. Result saved in newsletter_booklet.pdf...");
$doc = new PDFDoc($input_path . "BusinessCardTemplate.pdf"); $doc->InitSecurityHandler(); // first, replace the image on the first page $replacer = new ContentReplacer(); $page = $doc->GetPage(1); $img = Image::Create($doc->GetSDFDoc(), $input_path . "peppers.jpg"); $replacer->AddImage($page->GetMediaBox(), $img->GetSDFObj()); // next, replace the text place holders on the second page $replacer->AddString("NAME", "John Smith"); $replacer->AddString("QUALIFICATIONS", "Philosophy Doctor"); $replacer->AddString("JOB_TITLE", "Software Developer"); $replacer->AddString("ADDRESS_LINE1", "#100 123 Software Rd"); $replacer->AddString("ADDRESS_LINE2", "Vancouver, BC"); $replacer->AddString("PHONE_OFFICE", "604-730-8989"); $replacer->AddString("PHONE_MOBILE", "604-765-4321"); $replacer->AddString("EMAIL", "*****@*****.**"); $replacer->AddString("WEBSITE_URL", "http://www.pdftron.com"); // finally, apply $replacer->Process($page); $doc->Save($output_path . "BusinessCard.pdf", 0); //-------------------------------------------------------------------------------- // Example 2) Replace text in a region with new text $doc = new PDFDoc($input_path . "newsletter.pdf"); $doc->InitSecurityHandler(); $replacer = new ContentReplacer(); $page = $doc->GetPage(1); $target_region = $page->GetMediaBox(); $replacer->AddText($target_region, "hello hello hello hello hello hello hello hello hello hello"); $replacer->Process($page); $doc->Save($output_path . "ContentReplaced.pdf", 0);
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; }
$builder->MoveTo(459.223, 505.646); $builder->CurveTo(459.223, 415.841, 389.85, 343.04, 304.273, 343.04); $builder->CurveTo(218.697, 343.04, 149.324, 415.841, 149.324, 505.646); $builder->CurveTo(149.324, 595.45, 218.697, 668.25, 304.273, 668.25); $builder->CurveTo(389.85, 668.25, 459.223, 595.45, 459.223, 505.646); $element = $builder->PathEnd(); $element->SetPathFill(true); $gstate = $element->GetGState(); $gstate->SetFillColorSpace(ColorSpace::CreateDeviceRGB()); $gstate->SetFillColor(new ColorPt(0.0, 0.0, 1.0)); $gstate->SetBlendMode(GState::e_bl_normal); $gstate->SetFillOpacity(0.5); $writer->WriteElement($element); // Translate relative to the Blue Circle $gstate->SetTransform(1.0, 0.0, 0.0, 1.0, 113.0, -185.0); $gstate->SetFillColor(new ColorPt(0.0, 1.0, 0.0)); // Green Circle $gstate->SetFillOpacity(0.5); $writer->WriteElement($element); // Translate relative to the Green Circle $gstate->SetTransform(1.0, 0.0, 0.0, 1.0, -220.0, 0.0); $gstate->SetFillColor(new ColorPt(1.0, 0.0, 0.0)); // Red Circle $gstate->SetFillOpacity(0.5); $writer->WriteElement($element); $writer->End(); // save changes to the current page $doc->PagePushBack($page); // End page ------------------------------------ $doc->Save($output_path . "element_builder.pdf", SDFDoc::e_remove_unused); echo "Done. Result saved in element_builder.pdf...\n";
$writer = new ElementWriter(); $reader = new ElementReader(); $itr = $doc->GetPageIterator(); while ($itr->HasNext()) { $page = $itr->Current(); $reader->Begin($page); $writer->Begin($page, ElementWriter::e_replacement, false); $map1 = array(); ProcessElements($reader, $writer, $map1); $writer->End(); $reader->End(); $map2 = array(); while (!(empty($map1) && empty($map2))) { foreach ($map1 as $k => $v) { $obj = $v; $writer->Begin($obj); $reader->Begin($obj, $page->GetResourceDict()); ProcessElements($reader, $writer, $map2); $reader->End(); $writer->End(); unset($map1[$k]); } if (empty($map1) && !empty($map2)) { $map1 = $map1 + $map2; $map2 = array(); } } $itr->Next(); } $doc->Save($output_path . $output_filename, SDFDoc::e_remove_unused); echo nl2br("Done. Result saved in " . $output_filename . "...\n");
// Sample: // Flatten all form fields in a document. // Note that this sample is intended to show that it is possible to flatten // individual fields. PDFNet provides a utility function PDFDoc.FlattenAnnotations() // that will automatically flatten all fields. //---------------------------------------------------------------------------------- $doc = new PDFDoc($output_path . "forms_test1.pdf"); $doc->InitSecurityHandler(); // Traverse all pages if (true) { $doc->FlattenAnnotations(); } else { for ($pitr = $doc->GetPageIterator(); $pitr->HasNext(); $pitr->Next()) { $page = $pitr->Current(); $annots = $page->GetAnnots(); if ($annots) { // Look for all widget annotations (in reverse order) for ($i = $annots->Size() - 1; $i >= 0; --$i) { if (!strcmp(annots . GetAt(i) . Get("Subtype") . Value() . GetName(), "Widget")) { $field($annots->GetAt(i)); $field->Flatten($page); // Another way of making a read only field is by modifying // field's e_read_only flag: // field.SetFlag(Field::e_read_only, true); } } } } } $doc->Save($output_path . "forms_test1_flattened.pdf", 0); echo nl2br("Done.\n");
function main() { global $input_path, $output_path; PDFNet::Initialize(); $doc = new PDFDoc(); $builder = new ElementBuilder(); $writer = new ElementWriter(); // Start a new page ------------------------------------ $page = $doc->PageCreate(new Rect(0.0, 0.0, 612.0, 794.0)); $writer->Begin($page); // begin writing to this page // Embed and subset the font $font_program = $input_path . "ARIALUNI.TTF"; if (!file_exists($font_program)) { if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $font_program = "C:/Windows/Fonts/ARIALUNI.TTF"; echo nl2br("Note: Using ARIALUNI.TTF from C:/Windows/Fonts directory.\n"); } else { echo nl2br("Error: Cannot find ARIALUNI.TTF.\n"); exit(1); } } $fnt = Font::CreateCIDTrueTypeFont($doc->GetSDFDoc(), $font_program, true, true); $element = $builder->CreateTextBegin($fnt, 1.0); $element->SetTextMatrix(10.0, 0.0, 0.0, 10.0, 50.0, 600.0); $element->GetGState()->SetLeading(2); // Set the spacing between lines $writer->WriteElement($element); // Hello World! $hello = array('H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'); $writer->WriteElement($builder->CreateUnicodeTextRun($hello, count($hello))); $writer->WriteElement($builder->CreateTextNewLine()); // Latin $latin = array('a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 0x45, 0x46, 0xc0, 0xc1, 0xc2, 0x143, 0x144, 0x145, 0x152, '1', '2'); $writer->WriteElement($builder->CreateUnicodeTextRun($latin, count($latin))); $writer->WriteElement($builder->CreateTextNewLine()); // Greek $greek = array(0x39e, 0x39f, 0x3a0, 0x3a1, 0x3a3, 0x3a6, 0x3a8, 0x3a9); $writer->WriteElement($builder->CreateUnicodeTextRun($greek, count($greek))); $writer->WriteElement($builder->CreateTextNewLine()); // Cyrillic $cyrillic = array(0x409, 0x40a, 0x40b, 0x40c, 0x40e, 0x40f, 0x410, 0x411, 0x412, 0x413, 0x414, 0x415, 0x416, 0x417, 0x418, 0x419); $writer->WriteElement($builder->CreateUnicodeTextRun($cyrillic, count($cyrillic))); $writer->WriteElement($builder->CreateTextNewLine()); // Hebrew $hebrew = array(0x5d0, 0x5d1, 0x5d3, 0x5d3, 0x5d4, 0x5d5, 0x5d6, 0x5d7, 0x5d8, 0x5d9, 0x5da, 0x5db, 0x5dc, 0x5dd, 0x5de, 0x5df, 0x5e0, 0x5e1); $writer->WriteElement($builder->CreateUnicodeTextRun($hebrew, count($hebrew))); $writer->WriteElement($builder->CreateTextNewLine()); // Arabic $arabic = array(0x624, 0x625, 0x626, 0x627, 0x628, 0x629, 0x62a, 0x62b, 0x62c, 0x62d, 0x62e, 0x62f, 0x630, 0x631, 0x632, 0x633, 0x634, 0x635); $writer->WriteElement($builder->CreateUnicodeTextRun($arabic, count($arabic))); $writer->WriteElement($builder->CreateTextNewLine()); // Thai $thai = array(0xe01, 0xe02, 0xe03, 0xe04, 0xe05, 0xe06, 0xe07, 0xe08, 0xe09, 0xe0a, 0xe0b, 0xe0c, 0xe0d, 0xe0e, 0xe0f, 0xe10, 0xe11, 0xe12); $writer->WriteElement($builder->CreateUnicodeTextRun($thai, count($thai))); $writer->WriteElement($builder->CreateTextNewLine()); // Hiragana - Japanese $hiragana = array(0x3041, 0x3042, 0x3043, 0x3044, 0x3045, 0x3046, 0x3047, 0x3048, 0x3049, 0x304a, 0x304b, 0x304c, 0x304d, 0x304e, 0x304f, 0x3051, 0x3051, 0x3052); $writer->WriteElement($builder->CreateUnicodeTextRun($hiragana, count($hiragana))); $writer->WriteElement($builder->CreateTextNewLine()); // CJK Unified Ideographs $cjk_uni = array(0x5841, 0x5842, 0x5843, 0x5844, 0x5845, 0x5846, 0x5847, 0x5848, 0x5849, 0x584a, 0x584b, 0x584c, 0x584d, 0x584e, 0x584f, 0x5850, 0x5851, 0x5852); $writer->WriteElement($builder->CreateUnicodeTextRun($cjk_uni, count($cjk_uni))); $writer->WriteElement($builder->CreateTextNewLine()); // Simplified Chinese $chinese_simplified = array(0x4e16, 0x754c, 0x60a8, 0x597d); $writer->WriteElement($builder->CreateUnicodeTextRun($chinese_simplified, count($chinese_simplified))); $writer->WriteElement($builder->CreateTextNewLine()); // Finish the block of text $writer->WriteElement($builder->CreateTextEnd()); $writer->End(); // save changes to the current page $doc->PagePushBack($page); $doc->Save($output_path . "unicodewrite.pdf", SDFDoc::e_remove_unused | SDFDoc::e_hex_strings); echo "Done. Result saved in unicodewrite.pdf...\n"; }
$itr = $obj->Find("Filter"); if ($itr->HasNext() && $itr->Value()->IsName() && $itr->Value()->GetName() == "JBIG2Decode") { continue; } $filter = $obj->GetDecodedStream(); $reader = new FilterReader($filter); $hint_set = new ObjSet(); // A hint to image encoder to use JBIG2 compression $hint = $hint_set->CreateArray(); $hint->PushBackName("JBIG2"); $hint->PushBackName("Lossless"); $new_image = Image::Create($cos_doc, $reader, $input_image->GetImageWidth(), $input_image->GetImageHeight(), 1, ColorSpace::CreateDeviceGray(), $hint); $new_img_obj = $new_image->GetSDFObj(); $itr = $obj->Find("Decode"); if ($itr->HasNext()) { $new_img_obj->Put("Decode", $itr->Value()); } $itr = $obj->Find("ImageMask"); if ($itr->HasNext()) { $new_img_obj->Put("ImageMask", $itr->Value()); } $itr = $obj->Find("Mask"); if ($itr->HasNext()) { $new_img_obj->Put("Mask", $itr->Value()); } $cos_doc->Swap($i, $new_img_obj->GetObjNum()); } } $pdf_doc->Save("../../TestFiles/Output/US061222892_JBIG2.pdf", SDFDoc::e_remove_unused); $pdf_doc->Close(); echo "Done.";