Example #1
0
// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
//
// Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
// be present in the system path.
//-----------------------------------------------------------------------------------
PDFNet::Initialize();
// 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
// Create a list of pages to import from one PDF document to another.
$import_pages = new VectorPage();
for ($itr = $in_doc->GetPageIterator(); $itr->HasNext(); $itr->Next()) {
    $import_pages->push($itr->Current());
}
$new_doc = new PDFDoc();
$imported_pages = $new_doc->ImportPages($import_pages);
// Paper dimension for A3 format in points. Because one inch has
// 72 points, 11.69 inch 72 = 841.69 points
$media_box = new Rect(0.0, 0.0, 1190.88, 841.6900000000001);
$mid_point = $media_box->Width() / 2;
$builder = new ElementBuilder();
$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);
$file_reader = new FilterReader($file);
$mem = $file_reader->Read($file_sz);
$test = array();
for ($i = 0; $i < strlen($mem); $i++) {
    $test[] = ord($mem[$i]);
}
$doc = new PDFDoc($mem, $file_sz);
$doc->InitSecurityHandler();
$num_pages = $doc->GetPageCount();
$writer = new ElementWriter();
$reader = new ElementReader();
// Create a duplicate of every page but copy only path objects
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
Example #4
0
    $C1->PushBackNumber(0);
    $C1->PushBackNumber(0);
    $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());
        $element->SetTextMatrix(1.0, 0.0, 0.0, 1.0, 30.0, 13.0);
    }
    $writer->WriteElement($element);
    $writer->WriteElement($build->CreateTextEnd());
    $stm = $writer->End();
    // Set the bounding box
    $stm->PutRect("BBox", 0, 0, 101, 37);
    $stm->PutName("Subtype", "Form");
    return $stm;
}
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
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";
}