function ProcessElements($reader, $writer, $map) { while (($element = $reader->Next()) != null) { switch ($element->GetType()) { case Element::e_image: case Element::e_inline_image: // remove all images by skipping them break; case Element::e_text: // Set all text to blue color. $gs = $element->GetGState(); $gs->SetFillColorSpace(ColorSpace::CreateDeviceRGB()); $cp = new ColorPt(0.0, 0.0, 1.0); $gs->SetFillColor($cp); $writer->WriteElement($element); break; case Element::e_form: $o = $element->GetXObject(); $objNum = $o->GetObjNum(); $map[$objNum] = $o; $writer->WriteElement($element); break; default: $writer->WriteElement($element); } } }
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 CreateTilingPattern($doc) { $writer = new ElementWriter(); $builder = new ElementBuilder(); // Create a new pattern content stream - a heart. ------------ $writer->Begin($doc->GetSDFDoc()); $builder->PathBegin(); $builder->MoveTo(0, 0); $builder->CurveTo(500, 500, 125, 625, 0, 500); $builder->CurveTo(-125, 625, -500, 500, 0, 0); $heart = $builder->PathEnd(); $heart->SetPathFill(true); // Set heart color to red. $heart->GetGState()->SetFillColorSpace(ColorSpace::CreateDeviceRGB()); $heart->GetGState()->SetFillColor(new ColorPt(1.0, 0.0, 0.0)); $writer->WriteElement($heart); $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.04, 0.0, 0.0, 0.04, 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", 1000); $pattern_dict->PutNumber("YStep", 1000); return $pattern_dict; // finished creating the Pattern resource }
function CreateGroup2($doc, $layer) { $writer = new ElementWriter(); $writer->Begin($doc->GetSDFDoc()); // Create a path object in the shape of a heart. $builder = new ElementBuilder(); $builder->PathBegin(); // start constructing the path $builder->MoveTo(306, 396); $builder->CurveTo(681, 771, 399.75, 864.75, 306, 771); $builder->CurveTo(212.25, 864.75, -69, 771, 306, 396); $builder->ClosePath(); $element = $builder->PathEnd(); // the path geometry is now specified. // Set the path FILL color space and color. $element->SetPathFill(true); $gstate = $element->GetGState(); $gstate->SetFillColorSpace(ColorSpace::CreateDeviceCMYK()); $gstate->SetFillColor(new ColorPt(1.0, 0.0, 0.0, 0.0)); // cyan // Set the path STROKE color space and color. $element->SetPathStroke(true); $gstate->SetStrokeColorSpace(ColorSpace::CreateDeviceRGB()); $gstate->SetStrokeColor(new ColorPt(1.0, 0.0, 0.0)); // red $gstate->SetLineWidth(20); $gstate->SetTransform(0.5, 0.0, 0.0, 0.5, 280.0, 300.0); $writer->WriteElement($element); $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; }
$gstate->SetFillColor(new ColorPt(0.0, 1.0, 0.0, 0.0)); $gstate->SetFillOpacity(0.5); $writer->WriteElement($element); $writer->WriteElement($builder->CreateTextEnd()); // Draw three overlapping transparent circles. $builder->PathBegin(); // start constructing the path $builder->MoveTo(459.223, 505.646); $builder->CurveTo(459.223, 415.841, 389.85, 343.04, 304.273, 343.04); $builder->CurveTo(218.697, 343.04, 149.324, 415.841, 149.324, 505.646); $builder->CurveTo(149.324, 595.45, 218.697, 668.25, 304.273, 668.25); $builder->CurveTo(389.85, 668.25, 459.223, 595.45, 459.223, 505.646); $element = $builder->PathEnd(); $element->SetPathFill(true); $gstate = $element->GetGState(); $gstate->SetFillColorSpace(ColorSpace::CreateDeviceRGB()); $gstate->SetFillColor(new ColorPt(0.0, 0.0, 1.0)); $gstate->SetBlendMode(GState::e_bl_normal); $gstate->SetFillOpacity(0.5); $writer->WriteElement($element); // Translate relative to the Blue Circle $gstate->SetTransform(1.0, 0.0, 0.0, 1.0, 113.0, -185.0); $gstate->SetFillColor(new ColorPt(0.0, 1.0, 0.0)); // Green Circle $gstate->SetFillOpacity(0.5); $writer->WriteElement($element); // Translate relative to the Green Circle $gstate->SetTransform(1.0, 0.0, 0.0, 1.0, -220.0, 0.0); $gstate->SetFillColor(new ColorPt(1.0, 0.0, 0.0)); // Red Circle $gstate->SetFillOpacity(0.5);
// Recompress only 1 BPC images continue; } // Skip images that are already compressed using JBIG2 $itr = $obj->Find("Filter"); if ($itr->HasNext() && $itr->Value()->IsName() && $itr->Value()->GetName() == "JBIG2Decode") { continue; } $filter = $obj->GetDecodedStream(); $reader = new FilterReader($filter); $hint_set = new ObjSet(); // A hint to image encoder to use JBIG2 compression $hint = $hint_set->CreateArray(); $hint->PushBackName("JBIG2"); $hint->PushBackName("Lossless"); $new_image = Image::Create($cos_doc, $reader, $input_image->GetImageWidth(), $input_image->GetImageHeight(), 1, ColorSpace::CreateDeviceGray(), $hint); $new_img_obj = $new_image->GetSDFObj(); $itr = $obj->Find("Decode"); if ($itr->HasNext()) { $new_img_obj->Put("Decode", $itr->Value()); } $itr = $obj->Find("ImageMask"); if ($itr->HasNext()) { $new_img_obj->Put("ImageMask", $itr->Value()); } $itr = $obj->Find("Mask"); if ($itr->HasNext()) { $new_img_obj->Put("Mask", $itr->Value()); } $cos_doc->Swap($i, $new_img_obj->GetObjNum()); }