function CreateButtonAppearance($doc, $button_down) { // Create a button appearance stream ------------------------------------ $build = new ElementBuilder(); $writer = new ElementWriter(); $writer->Begin($doc->GetSDFDoc()); // Draw background $element = $build->CreateRect(0, 0, 101, 37); $element->SetPathFill(true); $element->SetPathStroke(false); $element->GetGState()->SetFillColorSpace(ColorSpace::CreateDeviceGray()); $element->GetGState()->SetFillColor(new ColorPt(0.75, 0.0, 0.0)); $writer->WriteElement($element); // Draw 'Submit' text $writer->WriteElement($build->CreateTextBegin()); $text = "Submit"; $element = $build->CreateTextRun($text, Font::Create($doc->GetSDFDoc(), Font::e_helvetica_bold), 12.0); $element->GetGState()->SetFillColor(new ColorPt(0.0, 0.0, 0.0)); if ($button_down) { $element->SetTextMatrix(1.0, 0.0, 0.0, 1.0, 33.0, 10.0); } else { $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; }
function AddCoverPage($doc) { // Here we dynamically generate cover page (please see ElementBuilder // sample for more extensive coverage of PDF creation API). $page = $doc->PageCreate(new Rect(0.0, 0.0, 200.0, 200.0)); $builder = new ElementBuilder(); $writer = new ElementWriter(); $writer->Begin($page); $font = Font::Create($doc->GetSDFDoc(), Font::e_helvetica); $writer->WriteElement($builder->CreateTextBegin($font, 12.0)); $element = $builder->CreateTextRun("My PDF Collection"); $element->SetTextMatrix(1.0, 0.0, 0.0, 1.0, 50.0, 96.0); $element->GetGState()->SetFillColorSpace(ColorSpace::CreateDeviceRGB()); $element->GetGState()->SetFillColor(new ColorPt(1.0, 0.0, 0.0)); $writer->WriteElement($element); $writer->WriteElement($builder->CreateTextEnd()); $writer->End(); $doc->PagePushBack($page); // Alternatively we could import a PDF page from a template PDF document // (for an example please see PDFPage sample project). // ... }
function CreateTestAnnots($doc) { $ew = new ElementWriter(); $eb = new ElementBuilder(); $first_page = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0)); $doc->PagePushBack($first_page); $ew->Begin($first_page, ElementWriter::e_overlay, false); // begin writing to this page $ew->End(); // save changes to the current page // // Test of a free text annotation. // $txtannot = FreeText::Create($doc->GetSDFDoc(), new Rect(10.0, 400.0, 160.0, 570.0)); $txtannot->SetContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." . "\n\nAha!\n\nAnd there was much rejoicing!"); $txtannot->SetBorderStyle(new BorderStyle(BorderStyle::e_solid, 1.0, 10.0, 20.0), true); $txtannot->SetQuaddingFormat(0); $first_page->AnnotPushBack($txtannot); $txtannot->RefreshAppearance(); $txtannot = FreeText::Create($doc->GetSDFDoc(), new Rect(100.0, 100.0, 350.0, 500.0)); $txtannot->SetContentRect(new Rect(200.0, 200.0, 350.0, 500.0)); $txtannot->SetContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." . "\n\nAha!\n\nAnd there was much rejoicing!"); $txtannot->SetCalloutLinePoints(new Point(200.0, 300.0), new Point(150.0, 290.0), new Point(110.0, 110.0)); $txtannot->SetBorderStyle(new BorderStyle(BorderStyle::e_solid, 1.0, 10.0, 20.0), true); $txtannot->SetEndingStyle(LineAnnot::e_ClosedArrow); $txtannot->SetColor(new ColorPt(0.0, 1.0, 0.0)); $txtannot->SetQuaddingFormat(1); $first_page->AnnotPushBack($txtannot); $txtannot->RefreshAppearance(); $txtannot = FreeText::Create($doc->GetSDFDoc(), new Rect(400.0, 10.0, 550.0, 400.0)); $txtannot->SetContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." . "\n\nAha!\n\nAnd there was much rejoicing!"); $txtannot->SetBorderStyle(new BorderStyle(BorderStyle::e_solid, 1.0, 10.0, 20.0), true); $txtannot->SetColor(new ColorPt(0.0, 0.0, 1.0)); $txtannot->SetOpacity(0.2); $txtannot->SetQuaddingFormat(2); $first_page->AnnotPushBack($txtannot); $txtannot->RefreshAppearance(); $page = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0)); $doc->PagePushBack($page); $ew->Begin($page, ElementWriter::e_overlay, false); // begin writing to this page $eb->Reset(); // Reset the GState to default $ew->End(); // save changes to the current page //Create a Line annotation... $line = LineAnnot::Create($doc->GetSDFDoc(), new Rect(250.0, 250.0, 400.0, 400.0)); $line->SetStartPoint(new Point(350.0, 270.0)); $line->SetEndPoint(new Point(260.0, 370.0)); $line->SetStartStyle(LineAnnot::e_Square); $line->SetEndStyle(LineAnnot::e_Circle); $line->SetColor(new ColorPt(0.3, 0.5, 0.0), 3); $line->SetContents("Dashed Captioned"); $line->SetShowCaption(true); $line->SetCaptionPosition(LineAnnot::e_Top); $line->SetBorderStyle(new BorderStyle(BorderStyle::e_dashed, 2.0, 0.0, 0.0, array(2.0, 2.0))); $line->RefreshAppearance(); $page->AnnotPushBack($line); $line = LineAnnot::Create($doc->GetSDFDoc(), new Rect(347.0, 377.0, 600.0, 600.0)); $line->SetStartPoint(new Point(385.0, 410.0)); $line->SetEndPoint(new Point(540.0, 555.0)); $line->SetStartStyle(LineAnnot::e_Circle); $line->SetEndStyle(LineAnnot::e_OpenArrow); $line->SetColor(new ColorPt(1.0, 0.0, 0.0), 3); $line->SetInteriorColor(new ColorPt(0.0, 1.0, 0.0), 3); $line->SetContents("Inline Caption"); $line->SetShowCaption(true); $line->SetCaptionPosition(LineAnnot::e_Inline); $line->SetLeaderLineExtensionLength(-4.0); $line->SetLeaderLineLength(-12.0); $line->SetLeaderLineOffset(2.0); $line->RefreshAppearance(); $page->AnnotPushBack($line); $line = LineAnnot::Create($doc->GetSDFDoc(), new Rect(10.0, 400.0, 200.0, 600.0)); $line->SetStartPoint(new Point(25.0, 426.0)); $line->SetEndPoint(new Point(180.0, 555.0)); $line->SetStartStyle(LineAnnot::e_Circle); $line->SetEndStyle(LineAnnot::e_Square); $line->SetColor(new ColorPt(0.0, 0.0, 1.0), 3); $line->SetInteriorColor(new ColorPt(1.0, 0.0, 0.0), 3); $line->SetContents("Offset Caption"); $line->SetShowCaption(true); $line->SetCaptionPosition(LineAnnot::e_Top); $line->SetTextHOffset(-60); $line->SetTextVOffset(10); $line->RefreshAppearance(); $page->AnnotPushBack($line); $line = LineAnnot::Create($doc->GetSDFDoc(), new Rect(200.0, 10.0, 400.0, 70.0)); $line->SetStartPoint(new Point(220.0, 25.0)); $line->SetEndPoint(new Point(370.0, 60.0)); $line->SetStartStyle(LineAnnot::e_Butt); $line->SetEndStyle(LineAnnot::e_OpenArrow); $line->SetColor(new ColorPt(0.0, 0.0, 1.0), 3); $line->SetContents("Regular Caption"); $line->SetShowCaption(true); $line->SetCaptionPosition(LineAnnot::e_Top); $line->RefreshAppearance(); $page->AnnotPushBack($line); $line = LineAnnot::Create($doc->GetSDFDoc(), new Rect(200.0, 70.0, 400.0, 130.0)); $line->SetStartPoint(new Point(220.0, 111.0)); $line->SetEndPoint(new Point(370.0, 78.0)); $line->SetStartStyle(LineAnnot::e_Circle); $line->SetEndStyle(LineAnnot::e_Diamond); $line->SetContents("Circle to Diamond"); $line->SetColor(new ColorPt(0.0, 0.0, 1.0), 3); $line->SetInteriorColor(new ColorPt(0.0, 1.0, 0.0), 3); $line->SetShowCaption(true); $line->SetCaptionPosition(LineAnnot::e_Top); $line->RefreshAppearance(); $page->AnnotPushBack($line); $line = LineAnnot::Create($doc->GetSDFDoc(), new Rect(10.0, 100.0, 160.0, 200.0)); $line->SetStartPoint(new Point(15.0, 110.0)); $line->SetEndPoint(new Point(150.0, 190.0)); $line->SetStartStyle(LineAnnot::e_Slash); $line->SetEndStyle(LineAnnot::e_ClosedArrow); $line->SetContents("Slash to CArrow"); $line->SetColor(new ColorPt(1.0, 0.0, 0.0), 3); $line->SetInteriorColor(new ColorPt(0.0, 1.0, 1.0), 3); $line->SetShowCaption(true); $line->SetCaptionPosition(LineAnnot::e_Top); $line->RefreshAppearance(); $page->AnnotPushBack($line); $line = LineAnnot::Create($doc->GetSDFDoc(), new Rect(270.0, 270.0, 570.0, 433.0)); $line->SetStartPoint(new Point(300.0, 400.0)); $line->SetEndPoint(new Point(550.0, 300.0)); $line->SetStartStyle(LineAnnot::e_RClosedArrow); $line->SetEndStyle(LineAnnot::e_ROpenArrow); $line->SetContents("ROpen & RClosed arrows"); $line->SetColor(new ColorPt(0.0, 0.0, 1.0), 3); $line->SetInteriorColor(new ColorPt(0.0, 1.0, 0.0), 3); $line->SetShowCaption(true); $line->SetCaptionPosition(LineAnnot::e_Top); $line->RefreshAppearance(); $page->AnnotPushBack($line); $line = LineAnnot::Create($doc->GetSDFDoc(), new Rect(195.0, 395.0, 205.0, 505.0)); $line->SetStartPoint(new Point(200.0, 400.0)); $line->SetEndPoint(new Point(200.0, 500.0)); $line->RefreshAppearance(); $page->AnnotPushBack($line); $line = LineAnnot::Create($doc->GetSDFDoc(), new Rect(55.0, 299.0, 150.0, 301.0)); $line->SetStartPoint(new Point(55.0, 300.0)); $line->SetEndPoint(new Point(155.0, 300.0)); $line->SetStartStyle(LineAnnot::e_Circle); $line->SetEndStyle(LineAnnot::e_Circle); $line->SetContents("Caption that's longer than its line."); $line->SetColor(new ColorPt(1.0, 0.0, 1.0), 3); $line->SetInteriorColor(new ColorPt(0.0, 1.0, 0.0), 3); $line->SetShowCaption(true); $line->SetCaptionPosition(LineAnnot::e_Top); $line->RefreshAppearance(); $page->AnnotPushBack($line); $line = LineAnnot::Create($doc->GetSDFDoc(), new Rect(300.0, 200.0, 390.0, 234.0)); $line->SetStartPoint(new Point(310.0, 210.0)); $line->SetEndPoint(new Point(380.0, 220.0)); $line->SetColor(new ColorPt(0.0, 0.0, 0.0), 3); $line->RefreshAppearance(); $page->AnnotPushBack($line); $page3 = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0)); $ew->Begin($page3); // begin writing to the page $ew->End(); // save changes to the current page $doc->PagePushBack($page3); $circle = Circle::Create($doc->GetSDFDoc(), new Rect(300.0, 300.0, 390.0, 350.0)); $circle->SetColor(new ColorPt(0.0, 0.0, 0.0), 3); $circle->RefreshAppearance(); $page3->AnnotPushBack($circle); $circle = Circle::Create($doc->GetSDFDoc(), new Rect(100.0, 100.0, 200.0, 200.0)); $circle->SetColor(new ColorPt(0.0, 1.0, 0.0), 3); $circle->SetInteriorColor(new ColorPt(0.0, 0.0, 1.0), 3); $circle->SetBorderStyle(new BorderStyle(BorderStyle::e_dashed, 3.0, 0.0, 0.0, array(2.0, 4.0))); $circle->SetPadding(2.0); $circle->RefreshAppearance(); $page3->AnnotPushBack($circle); $sq = Square::Create($doc->GetSDFDoc(), new Rect(10.0, 200.0, 80.0, 300.0)); $sq->SetColor(new ColorPt(0.0, 0.0, 0.0), 3); $sq->RefreshAppearance(); $page3->AnnotPushBack($sq); $sq = Square::Create($doc->GetSDFDoc(), new Rect(500.0, 200.0, 580.0, 300.0)); $sq->SetColor(new ColorPt(1.0, 0.0, 0.0), 3); $sq->SetInteriorColor(new ColorPt(0.0, 1.0, 1.0), 3); $sq->SetBorderStyle(new BorderStyle(BorderStyle::e_dashed, 6.0, 0.0, 0.0, array(4.0, 2.0))); $sq->SetPadding(4.0); $sq->RefreshAppearance(); $page3->AnnotPushBack($sq); $poly = Polygon::Create($doc->GetSDFDoc(), new Rect(5.0, 500.0, 125.0, 590.0)); $poly->SetColor(new ColorPt(1.0, 0.0, 0.0), 3); $poly->SetInteriorColor(new ColorPt(1.0, 1.0, 0.0), 3); $poly->SetVertex(0, new Point(12.0, 510.0)); $poly->SetVertex(1, new Point(100.0, 510.0)); $poly->SetVertex(2, new Point(100.0, 555.0)); $poly->SetVertex(3, new Point(35.0, 544.0)); $poly->SetBorderStyle(new BorderStyle(BorderStyle::e_solid, 4.0, 0.0, 0.0)); $poly->SetPadding(4.0); $poly->RefreshAppearance(); $page3->AnnotPushBack($poly); $poly = PolyLine::Create($doc->GetSDFDoc(), new Rect(400.0, 10.0, 500.0, 90.0)); $poly->SetColor(new ColorPt(1.0, 0.0, 0.0), 3); $poly->SetInteriorColor(new ColorPt(0.0, 1.0, 0.0), 3); $poly->SetVertex(0, new Point(405.0, 20.0)); $poly->SetVertex(1, new Point(440.0, 40.0)); $poly->SetVertex(2, new Point(410.0, 60.0)); $poly->SetVertex(3, new Point(470.0, 80.0)); $poly->SetBorderStyle(new BorderStyle(BorderStyle::e_solid, 2.0, 0.0, 0.0)); $poly->SetPadding(4.0); $poly->SetStartStyle(LineAnnot::e_RClosedArrow); $poly->SetEndStyle(LineAnnot::e_ClosedArrow); $poly->RefreshAppearance(); $page3->AnnotPushBack($poly); $lk = Link::Create($doc->GetSDFDoc(), new Rect(5.0, 5.0, 55.0, 24.0)); //$lk->SetColor( new ColorPt(0.0,1.0,0.0), 3.0 ); $lk->RefreshAppearance(); $page3->AnnotPushBack($lk); $page4 = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0)); $ew->Begin($page4); // begin writing to the page $ew->End(); // save changes to the current page $doc->PagePushBack($page4); $ew->Begin($page4); $font = Font::Create($doc->GetSDFDoc(), Font::e_helvetica); $element = $eb->CreateTextBegin($font, 16.0); $element->SetPathFill(true); $ew->WriteElement($element); $element = $eb->CreateTextRun("Some random text on the page", $font, 16.0); $element->SetTextMatrix(1.0, 0.0, 0.0, 1.0, 100.0, 500.0); $ew->WriteElement($element); $ew->WriteElement($eb->CreateTextEnd()); $ew->End(); $hl = HighlightAnnot::Create($doc->GetSDFDoc(), new Rect(100.0, 490.0, 150.0, 515.0)); $hl->SetColor(new ColorPt(0.0, 1.0, 0.0), 3); $hl->RefreshAppearance(); $page4->AnnotPushBack($hl); $sq = Squiggly::Create($doc->GetSDFDoc(), new Rect(100.0, 450.0, 250.0, 600.0)); //$sq->SetColor( new ColorPt(1.0,0.0,0.0), 3 ); $sq->SetQuadPoint(0, new QuadPoint(new Point(122.0, 455.0), new Point(240.0, 545.0), new Point(230.0, 595.0), new Point(101.0, 500.0))); $sq->RefreshAppearance(); $page4->AnnotPushBack($sq); $cr = Caret::Create($doc->GetSDFDoc(), new Rect(100.0, 40.0, 129.0, 69.0)); $cr->SetColor(new ColorPt(0.0, 0.0, 1.0), 3); $cr->SetSymbol("P"); $cr->RefreshAppearance(); $page4->AnnotPushBack($cr); $page5 = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0)); $ew->Begin($page5); // begin writing to the page $ew->End(); // save changes to the current page $doc->PagePushBack($page5); global $input_path; $fs = FileSpec::Create($doc->GetSDFDoc(), $input_path . "butterfly.png", false); $page6 = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0)); $ew->Begin($page6); // begin writing to the page $ew->End(); // save changes to the current page $doc->PagePushBack($page6); for ($ipage = 0; $ipage < 2; ++$ipage) { for ($iann = 0; $iann < 100; $iann++) { if (!($iann > FileAttachment::e_Tag)) { $fa = FileAttachment::Create($doc->GetSDFDoc(), new Rect(50.0 + 50.0 * (double) $iann, 100.0, 70.0 + 50.0 * (double) $iann, 120.0), $fs, $iann); if ($ipage) { $fa->SetColor(new ColorPt(1.0, 1.0, 0.0)); } $fa->RefreshAppearance(); if ($ipage == 0) { $page5->AnnotPushBack($fa); } else { $page6->AnnotPushBack($fa); } } if ($iann > Text::e_Note) { break; } $txt = Text::Create($doc->GetSDFDoc(), new Rect(10.0 + (double) $iann * 50.0, 200.0, 30.0 + (double) $iann * 50.0, 220.0)); $txt->SetIcon($iann); $txt->SetContents($txt->GetIconName()); if ($ipage) { $txt->SetColor(new ColorPt(1.0, 1.0, 0.0)); } $txt->RefreshAppearance(); if ($ipage == 0) { $page5->AnnotPushBack($txt); } else { $page6->AnnotPushBack($txt); } } } $txt = Text::Create($doc->GetSDFDoc(), new Rect(10.0, 20.0, 30.0, 40.0)); $txt->SetIcon("UserIcon"); $txt->SetContents("User defined icon, unrecognized by appearance generator"); $txt->SetColor(new ColorPt(0.0, 1.0, 0.0)); $txt->RefreshAppearance(); $page6->AnnotPushBack($txt); $ink = Ink::Create($doc->GetSDFDoc(), new Rect(100.0, 400.0, 200.0, 550.0)); $ink->SetColor(new ColorPt(0.0, 0.0, 1.0)); $ink->SetPoint(1, 3, new Point(220.0, 505.0)); $ink->SetPoint(1, 0, new Point(100.0, 490.0)); $ink->SetPoint(0, 1, new Point(120.0, 410.0)); $ink->SetPoint(0, 0, new Point(100.0, 400.0)); $ink->SetPoint(1, 2, new Point(180.0, 490.0)); $ink->SetPoint(1, 1, new Point(140.0, 440.0)); $ink->SetBorderStyle(new BorderStyle(BorderStyle::e_solid, 3.0, 0.0, 0.0)); $ink->RefreshAppearance(); $page6->AnnotPushBack($ink); $page7 = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0)); $ew->Begin($page7); // begin writing to the page $ew->End(); // save changes to the current page $doc->PagePushBack($page7); $snd = Sound::Create($doc->GetSDFDoc(), new Rect(100.0, 500.0, 120.0, 520.0)); $snd->SetColor(new ColorPt(1.0, 1.0, 0.0)); $snd->SetIcon(Sound::e_Speaker); $snd->RefreshAppearance(); $page7->AnnotPushBack($snd); $snd = Sound::Create($doc->GetSDFDoc(), new Rect(200.0, 500.0, 220.0, 520.0)); $snd->SetColor(new ColorPt(1.0, 1.0, 0.0)); $snd->SetIcon(Sound::e_Mic); $snd->RefreshAppearance(); $page7->AnnotPushBack($snd); $page8 = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0)); $ew->Begin($page8); // begin writing to the page $ew->End(); // save changes to the current page $doc->PagePushBack($page8); for ($ipage = 0; $ipage < 2; ++$ipage) { $px = 5; $py = 520; for ($istamp = RubberStamp::e_Approved; $istamp <= RubberStamp::e_Draft; $istamp = $istamp + 1) { $st = RubberStamp::Create($doc->GetSDFDoc(), new Rect(1.0, 1.0, 100.0, 100.0)); $st->SetIcon($istamp); $st->SetContents($st->GetIconName()); $st->SetRect(new Rect((double) $px, (double) $py, (double) $px + 100.0, (double) $py + 25.0)); $py -= 100; if ($py < 0) { $py = 520; $px += 200; } if ($ipage == 0) { //$page7->AnnotPushBack( $st ); } else { $page8->AnnotPushBack($st); $st->RefreshAppearance(); } } } $st = RubberStamp::Create($doc->GetSDFDoc(), new Rect(400.0, 5.0, 550.0, 45.0)); $st->SetIcon("UserStamp"); $st->SetContents("User defined stamp"); $page8->AnnotPushBack($st); $st->RefreshAppearance(); }
function CreateGroup3($doc, $layer) { $writer = new ElementWriter(); $writer->Begin($doc->GetSDFDoc()); // Create a path object in the shape of a heart. $builder = new ElementBuilder(); // Begin writing a block of text $element = $builder->CreateTextBegin(Font::Create($doc->GetSDFDoc(), Font::e_times_roman), 120.0); $writer->WriteElement($element); $element = $builder->CreateTextRun("A text layer!"); // Rotate text 45 degrees, than translate 180 pts horizontally and 100 pts vertically. $transform = Matrix2D::RotationMatrix(-45 * (3.1415 / 180.0)); $transform->Concat(1, 0, 0, 1, 180, 100); $element->SetTextMatrix($transform); $writer->WriteElement($element); $writer->WriteElement($builder->CreateTextEnd()); $grp_obj = $writer->End(); // Indicate that this form (content group) belongs to the given layer (OCG). $grp_obj->PutName("Subtype", "Form"); $grp_obj->Put("OC", $layer); $grp_obj->PutRect("BBox", 0, 0, 1000, 1000); // Set the clip box for the content. return $grp_obj; }
$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");
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"; }