Some test text!

platform
search
< Linux samples

PHP PDF Editor (Programmatic)

Sample PHP code for using PDFTron SDK to programmatically edit an existing PDF document's page display list and the graphics state attributes on existing elements. In particular, this sample strips all images from the page and changes the text color to blue. You can also build a GUI with interactive PDF editor widgets. Some of PDFTron SDK's other functions for programmatically editing PDFs include the Cos/SDF low-level API, page manipulation, and more. Learn more about our PDF Editing & Manipulation Library.

Step 1: Get your free trial license key, or sign in

Start Trial
Sign in

Step 2: Add the code:

<?php
#---------------------------------------------------------------------------------------
# Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
# Consult LICENSE.txt regarding license information.
#---------------------------------------------------------------------------------------
include("../../../PDFNetC/Lib/PDFNetPHP.php");

#---------------------------------------------------------------------------------------
# The sample code shows how to edit the page display list and how to modify graphics state 
# attributes on existing Elements. In particular the sample program strips all images from 
# the page and changes text color to blue. 
#---------------------------------------------------------------------------------------

# Relative path to the folder containing the test files.
$input_path = getcwd()."/../../TestFiles/";
$output_path = $input_path."Output/";
$input_filename = "newsletter.pdf";
$output_filename = "newsletter_edited.pdf";

function ProcessElements($reader, $writer, $map) {
	while (($element = $reader->Next()) != null) 	// Read page contents
	{
		switch ($element->GetType())
		{		
		case Element::e_image: 
		case Element::e_inline_image: 
			// remove all images by skipping them			
			break;
		case Element::e_text:// Process text strings...
			{
				// 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:// Recursively process form XObjects
			{
				$o = $element->GetXObject();
				$objNum = $o->GetObjNum();
				$map[$objNum] = $o;
				$writer->WriteElement($element); 				
				break; 
			}
		default:
			$writer->WriteElement($element);
		}
	}
}

	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]);
			}
			if (empty($map1) && !empty($map2))
			{
				$map1 = $map1 + $map2;
				$map2 = array();
			}
		}
		$itr->Next();
	}

	$doc->Save($output_path.$output_filename, SDFDoc::e_remove_unused);		
	echo nl2br("Done. Result saved in ".$output_filename."...\n");
?>