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"); 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());
$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"); $element = $builder->CreateImage($img, new Matrix2D((double) $img->GetImageWidth(), 0.0, 0.0, (double) $img->GetImageHeight(), 10.0, 50.0)); $writer->WritePlacedElement($element); $writer->End(); // Save the page $doc->PagePushBack($page); // Add the page to the document page sequence // ---------------------------------------------------------- // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter. $page = $doc->PageCreate(new Rect(0.0, 0.0, 612.0, 794.0)); $writer->Begin($page); // begin writing to this page // Note: encoder hints can be used to select between different compression methods. // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression. $hint_set = new ObjSet(); $enc = $hint_set->CreateArray(); // Initilaize encoder 'hint' parameter $enc->PushBackName("JBIG2"); $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));
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"); //----------------------------------------------------------------------------------
$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...");
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"; }