function SignPDF() { $infile = '../../TestFiles/doc_to_sign.pdf'; $outfile = '../../TestFiles/Output/signed_doc.pdf'; $certfile = '../../TestFiles/pdftron.pfx'; $imagefile = '../../TestFiles/signature.jpg'; $result = true; try { echo nl2br('Signing 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"); // Obtain the signature form field from the PDFDoc via Annotation. $sigField = $doc->GetField('Signature1'); $widgetAnnot = new Widget($sigField->GetSDFObj()); # 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.'); // Create a signature appearance $apWriter = new ElementWriter(); $apBuilder = new ElementBuilder(); $apWriter->Begin($doc->GetSDFDoc()); $sigImg = Image::Create($doc->GetSDFDoc(), $imagefile); $w = floatval($sigImg->GetImageWidth()); $h = floatval($sigImg->GetImageHeight()); $apElement = $apBuilder->CreateImage($sigImg, 0.0, 0.0, $w, $h); $apWriter->WritePlacedElement($apElement); $apObj = $apWriter->End(); $apObj->PutRect('BBox', 0.0, 0.0, $w, $h); $apObj->PutName('Subtype', 'Form'); $apObj->PutName('Type', 'XObject'); $apWriter->Begin($doc->GetSDFDoc()); $apElement = $apBuilder->CreateForm($apObj); $apWriter->WritePlacedElement($apElement); $apObj = $apWriter->End(); $apObj->PutRect('BBox', 0.0, 0.0, $w, $h); $apObj->PutName('Subtype', 'Form'); $apObj->PutName('Type', 'XObject'); $widgetAnnot->SetAppearance($apObj); $widgetAnnot->RefreshAppearance(); // 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 signing 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; }
function CreateImageTilingPattern($doc) { $writer = new ElementWriter(); $builder = new ElementBuilder(); // Create a new pattern content stream - a single bitmap object ---------- $writer->Begin($doc->GetSDFDoc()); global $input_path; $image = Image::Create($doc->GetSDFDoc(), $input_path . "dice.jpg"); $img_element = $builder->CreateImage($image, 0.0, 0.0, (double) $image->GetImageWidth(), (double) $image->GetImageHeight()); $writer->WritePlacedElement($img_element); $pattern_dict = $writer->End(); // Initialize pattern dictionary. For details on what each parameter represents please // refer to Table 4.22 (Section '4.6.2 Tiling Patterns') in PDF Reference Manual. $pattern_dict->PutName("Type", "Pattern"); $pattern_dict->PutNumber("PatternType", 1); // TilingType - Constant spacing. $pattern_dict->PutNumber("TilingType", 1); // This is a Type1 pattern - A colored tiling pattern. $pattern_dict->PutNumber("PaintType", 1); // Set bounding box $pattern_dict->PutRect("BBox", -253, 0, 253, 545); // Create and set the matrix $pattern_mtx = new Matrix2D(0.3, 0.0, 0.0, 0.3, 0.0, 0.0); $pattern_dict->PutMatrix("Matrix", $pattern_mtx); // Set the desired horizontal and vertical spacing between pattern cells, // measured in the pattern coordinate system. $pattern_dict->PutNumber("XStep", 300); $pattern_dict->PutNumber("YStep", 300); return $pattern_dict; // finished creating the Pattern resource }
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; }
// 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 // Add a GIF image to the output file //$img = Image::Create($doc->GetSDFDoc(), $input_path."pdfnet.gif");
$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); $sc_x = $mid_point / $src_page->GetPageWidth();
function Create3DAnnotation($doc, $annots) { // --------------------------------------------------------------------------------- // Create a 3D annotation based on U3D content. PDF 1.6 introduces the capability // for collections of three-dimensional objects, such as those used by CAD software, // to be embedded in PDF files. $link_3D = $doc->CreateIndirectDict(); $link_3D->PutName("Subtype", "3D"); // Annotation location on the page $link_3D_rect = new Rect(25.0, 180.0, 585.0, 643.0); $link_3D->PutRect("Rect", $link_3D_rect->x1, $link_3D_rect->y1, $link_3D_rect->x2, $link_3D_rect->y2); $annots->PushBack($link_3D); // The 3DA entry is an activation dictionary (see Table 9.34 in the PDF Reference Manual) // that determines how the state of the annotation and its associated artwork can change. $activation_dict_3D = $link_3D->PutDict("3DA"); // Set the annotation so that it is activated as soon as the page containing the // annotation is opened. Other options are: PV (page view) and XA (explicit) activation. $activation_dict_3D->PutName("A", "PO"); // Embed U3D Streams (3D Model/Artwork). global $input_path; $u3d_file = new MappedFile($input_path . "dice.u3d"); $u3d_reader = new FilterReader($u3d_file); // To embed 3D stream without compression, you can omit the second parameter in CreateIndirectStream. $u3d_data_dict = $doc->CreateIndirectStream($u3d_reader, new FlateEncode(new Filter())); $u3d_data_dict->PutName("Subtype", "U3D"); $link_3D->Put("3DD", $u3d_data_dict); // Set the initial view of the 3D artwork that should be used when the annotation is activated. $view3D_dict = $link_3D->PutDict("3DV"); $view3D_dict->PutString("IN", "Unnamed"); $view3D_dict->PutString("XN", "Default"); $view3D_dict->PutName("MS", "M"); $view3D_dict->PutNumber("CO", 27.5); // A 12-element 3D transformation matrix that specifies a position and orientation // of the camera in world coordinates. $tr3d = $view3D_dict->PutArray("C2W"); $tr3d->PushBackNumber(1); $tr3d->PushBackNumber(0); $tr3d->PushBackNumber(0); $tr3d->PushBackNumber(0); $tr3d->PushBackNumber(0); $tr3d->PushBackNumber(-1); $tr3d->PushBackNumber(0); $tr3d->PushBackNumber(1); $tr3d->PushBackNumber(0); $tr3d->PushBackNumber(0); $tr3d->PushBackNumber(-27.5); $tr3d->PushBackNumber(0); // Create annotation appearance stream, a thumbnail which is used during printing or // in PDF processors that do not understand 3D data. $ap_dict = $link_3D->PutDict("AP"); $builder = new ElementBuilder(); $writer = new ElementWriter(); $writer->Begin($doc->GetSDFDoc()); $thumb_pathname = $input_path . "dice.jpg"; $image = Image::Create($doc->GetSDFDoc(), $thumb_pathname); $writer->WritePlacedElement($builder->CreateImage($image, 0.0, 0.0, $link_3D_rect->Width(), $link_3D_rect->Height())); $normal_ap_stream = $writer->End(); $normal_ap_stream->PutName("Subtype", "Form"); $normal_ap_stream->PutRect("BBox", 0, 0, $link_3D_rect->Width(), $link_3D_rect->Height()); $ap_dict->Put("N", $normal_ap_stream); }
} } } PDFNet::Initialize(); echo nl2br("-------------------------------------------------\n"); // Open the test file echo nl2br("Opening the input file...\n"); $doc = new PDFDoc($input_path . $input_filename); $doc->InitSecurityHandler(); $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]); }
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"; }