} //----------------------------------------------------------- // Example 3: Modify page labels from an existing PDF document. //----------------------------------------------------------- $doc = new PDFDoc($output_path . "newsletter_with_pagelabels.pdf"); $doc->InitSecurityHandler(); // Remove the alphabetic labels from example 1. $doc->RemovePageLabel(7); // 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();
} //--------------------------------------------------------------------------------------- PDFNet::Initialize(); // Create a PDF Package. $doc = new PDFDoc(); AddPackage($doc, $input_path . "numbered.pdf", "My File 1"); AddPackage($doc, $input_path . "newsletter.pdf", "My Newsletter..."); AddPackage($doc, $input_path . "peppers.jpg", "An image"); AddCoverPage($doc); $doc->Save($output_path . "package.pdf", SDFDoc::e_linearized); $doc->Close(); echo nl2br("Done.\n"); // Extract parts from a PDF Package. $doc = new PDFDoc($output_path . "package.pdf"); $doc->InitSecurityHandler(); $files = NameTree::Find($doc->GetSDFDoc(), "EmbeddedFiles"); if ($files->IsValid()) { // Traverse the list of embedded files. $i = $files->GetIterator(); for ($counter = 0; $i->HasNext(); $i->Next(), ++$counter) { $entry_name = $i->Key()->GetAsPDFText(); echo nl2br("Part: " . $entry_name . "\n"); $file_spec = new FileSpec($i->Value()); $stm = new Filter($file_spec->GetFileData()); if ($stm) { $stm->WriteToFile($output_path . "extract_" . $counter, false); } } } $doc->Close(); echo nl2br("Done.\n");
$reader = new ElementReader(); // Read every page for ($itr = $doc->GetPageIterator(); $itr->HasNext(); $itr->Next()) { $reader->Begin($itr->Current()); ImageExtract($reader); $reader->End(); } $doc->Close(); echo nl2br("Done...\n"); echo nl2br("----------------------------------------------------------------\n"); // Example 2: // Extract images by scanning the low-level document. $doc = new PDFDoc($input_path . "newsletter.pdf"); $doc->InitSecurityHandler(); $image_counter = 0; $cos_doc = $doc->GetSDFDoc(); $num_objs = $cos_doc->XRefSize(); for ($i = 1; $i < $num_objs; ++$i) { $obj = $cos_doc->GetObj($i); if ($obj != null && !$obj->IsFree() && $obj->IsStream()) { // Process only images $itr = $obj->Find("Type"); if (!$itr->HasNext() || !($itr->Value()->GetName() == "XObject")) { continue; } $itr = $obj->Find("Subtype"); if (!$itr->HasNext() || !($itr->Value()->GetName() == "Image")) { continue; } $image = new Image($obj); echo nl2br("\n-. Image: " . ++$image_counter);
$s->SetPosition(10.0, 10.0); $img = Image::Create($doc->GetSDFDoc(), $input_path . "peppers.jpg"); $ps = new PageSet(1, $doc->GetPageCount(), PageSet::e_all); $s->StampImage($doc, $img, $ps); $doc->Save($output_path . $input_filename . ".ex7.pdf", SDFDoc::e_linearized); $doc->Close(); //-------------------------------------------------------------------------------- // Example 8) Add Text stamp to first 2 pages, and image stamp to first page. // Because text stamp is set as background, the image is top of the text // stamp. Text stamp on the first page is not visible. $doc = new PDFDoc($input_path . $input_filename); $doc->InitSecurityHandler(); $s = new Stamper(Stamper::e_relative_scale, 0.07000000000000001, -0.1); $s->SetAlignment(Stamper::e_horizontal_right, Stamper::e_vertical_bottom); $s->SetAlignment(Stamper::e_horizontal_center, Stamper::e_vertical_top); $s->SetFont(Font::Create($doc->GetSDFDoc(), Font::e_courier, true)); $red = new ColorPt(1.0, 0.0, 0.0, 0.0); $s->SetFontColor($red); //set color to red $s->SetTextAlignment(Stamper::e_align_right); $s->SetAsBackground(true); //set text stamp as background $ps = new PageSet(1, 2); $s->StampText($doc, "This is a title!", $ps); $img = Image::Create($doc->GetSDFDoc(), $input_path . "peppers.jpg"); $s->SetAsBackground(false); // set image stamp as foreground $first_page_ps = new PageSet(1); $s->StampImage($doc, $img, $first_page_ps); $doc->Save($output_path . $input_filename . ".ex8.pdf", SDFDoc::e_linearized); $doc->Close();
// Relative path to the folder containing the test files. $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
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; }
$domain = $function->PutArray("Domain"); $domain->PushBackNumber(0); $domain->PushBackNumber(1); $function->PutNumber("FunctionType", 2); $function->PutNumber("N", 1); return $pattern_dict; } //--------------------------------------------------------------------------------------- PDFNet::Initialize(); $doc = new PDFDoc(); $writer = new ElementWriter(); $builder = new ElementBuilder(); // The following sample illustrates how to create and use tiling patterns $page = $doc->PageCreate(); $writer->Begin($page); $element = $builder->CreateTextBegin(Font::Create($doc->GetSDFDoc(), Font::e_times_bold), 1.0); $writer->WriteElement($element); // Begin the text block $data = "G"; $element = $builder->CreateTextRun($data); $element->SetTextMatrix(720.0, 0.0, 0.0, 720.0, 20.0, 240.0); $gs = $element->GetGState(); $gs->SetTextRenderMode(GState::e_fill_stroke_text); $gs->SetLineWidth(4); // Set the fill color space to the Pattern color space. $gs->SetFillColorSpace(ColorSpace::CreatePattern()); $gs->SetFillColor(new PatternColor(CreateTilingPattern($doc))); $writer->WriteElement($element); $writer->WriteElement($builder->CreateTextEnd()); // Finish the text block $writer->End();
//----------------------------------------------------------------------------------------- // The sample code illustrates how to use the ContentReplacer class to make using // 'template' pdf documents easier. //----------------------------------------------------------------------------------------- PDFNet::Initialize(); // Relative path to the folder containing the test files. $input_path = getcwd() . "/../../TestFiles/"; $output_path = $input_path . "Output/"; //-------------------------------------------------------------------------------- // Example 1) Update a business card template with personalized info $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); //--------------------------------------------------------------------------------
# This sample illustrates how to edit existing text strings. # 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);
} PDFNet::Initialize(); //---------------------------------------------------------------------------------- // Example 1: Programatically create new Form Fields and Widget Annotations. //---------------------------------------------------------------------------------- $doc = new PDFDoc(); $blank_page = $doc->PageCreate(); // Create a blank new page and add some form fields. // Create new fields. $emp_first_name = $doc->FieldCreate("employee.name.first", Field::e_text, "John", ""); $emp_last_name = $doc->FieldCreate("employee.name.last", Field::e_text, "Doe", ""); $emp_last_check1 = $doc->FieldCreate("employee.name.check1", Field::e_check, "Yes", ""); $submit = $doc->FieldCreate("submit", Field::e_button); // Create page annotations for the above fields. // Create text annotations $annot1 = Widget::Create($doc->GetSDFDoc(), new Rect(50.0, 550.0, 350.0, 600.0), $emp_first_name); $annot2 = Widget::Create($doc->GetSDFDoc(), new Rect(50.0, 450.0, 350.0, 500.0), $emp_last_name); // Create a check-box annotation $annot3 = Widget::Create($doc->GetSDFDoc(), new Rect(64.0, 356.0, 120.0, 410.0), $emp_last_check1); // Set the annotation appearance for the "Yes" state... $annot3->SetAppearance(CreateCheckmarkAppearance($doc, Annot::e_normal, "Yes")); // Create button annotation $annot4 = Widget::Create($doc->GetSDFDoc(), new Rect(64.0, 284.0, 163.0, 320.0), $submit); // Set the annotation appearances for the down and up state... $annot4->SetAppearance(CreateButtonAppearance($doc, false), Annot::e_normal); $annot4->SetAppearance(CreateButtonAppearance($doc, true), Annot::e_down); // Create 'SubmitForm' action. The action will be linked to the button. $url = FileSpec::CreateURL($doc->GetSDFDoc(), "http://www.pdftron.com"); $button_action = Action::CreateSubmitForm($url); // Associate the above action with 'Down' event in annotations action dictionary. $annot_action = $annot4->GetSDFObj()->PutDict("AA");
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"; }
// Consult LICENSE.txt regarding license information. //--------------------------------------------------------------------------------------- include "../../../PDFNetC/Lib/PDFNetPHP.php"; // Relative path to the folder containing the test files. $input_path = getcwd() . "/../../TestFiles/"; $output_path = $input_path . "Output/"; // This sample project illustrates how to recompress bi-tonal images in an // existing PDF document using JBIG2 compression. The sample is not intended // to be a generic PDF optimization tool. // // You can download the entire document using the following link: // http://www.pdftron.com/net/samplecode/data/US061222892.pdf PDFNet::Initialize(); $pdf_doc = new PDFDoc("../../TestFiles/US061222892-a.pdf"); $pdf_doc->InitSecurityHandler(); $cos_doc = $pdf_doc->GetSDFDoc(); $num_objs = $cos_doc->XRefSize(); for ($i = 1; $i < $num_objs; ++$i) { $obj = $cos_doc->GetObj($i); if ($obj && !$obj->IsFree() && $obj->IsStream()) { // Process only images $itr = $obj->Find("Subtype"); if (!$itr->HasNext() || $itr->Value()->GetName() != "Image") { continue; } $input_image = new Image($obj); // Process only gray-scale images if ($input_image->GetComponentNum() != 1) { continue; } $bpc = $input_image->GetBitsPerComponent();