Edit PDF� - PHP Sample Code

Sample code for using Apryse 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, provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB. You can also build a GUI with interactive PDF editor widgets. Some of Apryse SDK's other functions for programmatically editing PDFs include the Cos/SDF low-level API, page manipulation, and more. Learn more about our Server SDK and PDF Editing & Manipulation Library.

1<?php
2#---------------------------------------------------------------------------------------
3# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4# Consult LICENSE.txt regarding license information.
5#---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10#---------------------------------------------------------------------------------------
11# The sample code shows how to edit the page display list and how to modify graphics state
12# attributes on existing Elements. In particular the sample program strips all images from
13# the page and changes text color to blue.
14#---------------------------------------------------------------------------------------
15
16# Relative path to the folder containing the test files.
17$input_path = getcwd()."/../../TestFiles/";
18$output_path = $input_path."Output/";
19$input_filename = "newsletter.pdf";
20$output_filename = "newsletter_edited.pdf";
21
22function ProcessElements($reader, $writer, $map) {
23 while (($element = $reader->Next()) != null) // Read page contents
24 {
25 switch ($element->GetType())
26 {
27 case Element::e_image:
28 case Element::e_inline_image:
29 // remove all images by skipping them
30 break;
31 case Element::e_path:
32 {
33 // Set all paths to red color.
34 $gs = $element->GetGState();
35 $gs->SetFillColorSpace(ColorSpace::CreateDeviceRGB());
36 $gs->SetFillColor(new ColorPt(1.0, 0.0, 0.0));
37 $writer->WriteElement($element);
38 break;
39 }
40 case Element::e_text:// Process text strings...
41 {
42 // Set all text to blue color.
43 $gs = $element->GetGState();
44 $gs->SetFillColorSpace(ColorSpace::CreateDeviceRGB());
45 $cp = new ColorPt(0.0, 0.0, 1.0);
46 $gs->SetFillColor($cp);
47 $writer->WriteElement($element);
48 break;
49 }
50 case Element::e_form:// Recursively process form XObjects
51 {
52 $o = $element->GetXObject();
53 $objNum = $o->GetObjNum();
54 $map[$objNum] = $o;
55 $writer->WriteElement($element);
56 break;
57 }
58 default:
59 $writer->WriteElement($element);
60 }
61 }
62}
63
64 PDFNet::Initialize($LicenseKey);
65 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
66
67
68 // Open the test file
69 echo nl2br("Opening the input file...\n");
70 $doc = new PDFDoc($input_path.$input_filename);
71 $doc->InitSecurityHandler();
72
73 $writer = new ElementWriter();
74 $reader = new ElementReader();
75
76 $itr = $doc->GetPageIterator();
77
78 while ($itr->HasNext())
79 {
80 $page = $itr->Current();
81 $reader->Begin($page);
82 $writer->Begin($page, ElementWriter::e_replacement, false);
83 $map1 = array();
84 ProcessElements($reader, $writer, $map1);
85 $writer->End();
86 $reader->End();
87
88 $map2 = array();
89 while (!(empty($map1) && empty($map2)))
90 {
91 foreach ($map1 as $k=>$v)
92 {
93 $obj = $v;
94 $writer->Begin($obj);
95 $reader->Begin($obj, $page->GetResourceDict());
96 ProcessElements($reader, $writer, $map2);
97 $reader->End();
98 $writer->End();
99
100 unset($map1[$k]);
101 }
102 if (empty($map1) && !empty($map2))
103 {
104 $map1 = $map1 + $map2;
105 $map2 = array();
106 }
107 }
108 $itr->Next();
109 }
110
111 $doc->Save($output_path.$output_filename, SDFDoc::e_remove_unused);
112 PDFNet::Terminate();
113 echo nl2br("Done. Result saved in ".$output_filename."...\n");
114?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales