Basics of PDF graphics and how to edit

11/9/2015 By Frank 0 comments

A question that I often hear is: "how can I change the graphics of my PDF such as replacing text with some other text or replace a logo with another logo?" In general this is not a good idea. PDF is not designed for editing; it is designed as an end format much like ink on paper. Nevertheless there may be circumstances - such as when you don't have access to the source format - when editing a PDF is a requirement. This article explains the basics of PDF graphics and how graphics can be edited if you really have to.

PDF Graphics

A PDF document contains various types of information such as metadata (author, title, etc.), form fields, navigational data such as bookmarks, annotations such as comments, and last but not least graphics. Graphics can roughly be divided into three categories: curves, text and images. The graphics on a PDF page are described by a sequence of operators. Operators can be divided into 3 groups:

1. Draw operators that draw curves, text and images

2. Graphics state operators that do things such as selecting a font, selecting a color or transforming the coordinate system (more about that later)

3. Marked-content operators that associate high-level information with graphics but do not affect the appearance. I ignore this group for the purpose of this article.

Each operator takes zero or more operands. Here is a simple example that draws a straight red line:

1 150 250 m % set the current point to (150, 250) 2 150 350 l % append a straight line to (150, 350) 3 1 0 0 RG % set the stroke color to red 4 S % stroke the line

The operator follows the operands that are used by the operator. On the first line, operator 'm' uses operands 150 and 250.

Here is an example that involves text:

1 /F1 24 Tf % set font to F1 and font size to 24 2 100 100 Td % move text position to (100, 100) 3 (Hello World) Tj % draw the text 'Hello World'

On the first line, operator Tf takes operands /F1 and 24. Operand /F1 is the name of a font. Without going into the full details, it suffices to say that /F1 can be resolved to an actual font either inside or outside the PDF document.

And finally, here is a one-liner that draws an image:

1 /I1 Do % draw image

Similar to selection a font, /I1 is resolved to an actual image inside the PDF document.

Graphics state

As said, operators can be divided into draw operators and graphics state operators. When the operators are processed from top to bottom a graphics state is maintained. The graphics state operators change the graphics state and the result of draw operators are affected by the graphics state. In the first sample we saw that the RG operator changed the stroke color to red and the S operator draws a line using the current stroke color.

Other graphics state operators set the line width, dash pattern, fill color, font size etc. Finally, there are two special operators that, respectively, save (q) and restore (Q) the graphics state. Simply put: the restore operator changes the graphics state back to the state at the previous save operator. They appear pair-wise and can be nested.

Coordinate System

A crucial part of the PDF imaging model is the coordinate system. The coordinate system determines where on the page a given coordinate such as (150, 250) is located and what the extend of a size is. PDF defines different coordinate systems. The most important two are user space and device space.

Device space

The device space is determined by the output device such as a printer or display on which a PDF page is ultimately rendered. Let's say that we want to render a PDF page to a Windows bitmap at 300 DPI, then from a Windows development perspective, the device space has its origin at the top-left corner, the x axis points to the right, the y axis points downwards, and the length of a unit (a pixel) is 1/300 inch.

User space

As opposed to the device space, the user space is device independent. For every page, it is initialized such that its origin lies at the bottom-left corner, the x axis points to the right, the y axis point upwards and the length of a unit is 1/72 inch or 1 point. The coordinates in the above PDF operator examples are in user space.

Mapping user space to device space

How these coordinates are mapped or transformed to coordinates in the device space is defined by the current transformation matrix or CTM. Let's see how this would look in code:

1 // width and height of a Letter page 2 float width = 612; // 612 points = 8.5 inches 3 float height = 792; // 792 points = 11 inches 4 5 // output device is a 600 dpi bitmap 6 float dpi = 600; 7 Bitmap bitmap = new Bitmap((int)(width * dpi / 72), (int)(height * dpi / 72)); 8 9 // 4 page corners in user pace 10 PointF[] points = new PointF[] { 11 new PointF(0, 0), // bottom-left corner 12 new PointF(0, height), // top-left corner 13 new PointF(width, height), // top-right corner 14 new PointF(width, 0) // bottom-right corner 15 }; 16 Console.WriteLine( 17 string.Join("; ", points.Select(p => string.Format("({0}, {1})", p.X, p.Y)))); 18 19 // calculate the coordinates of the corners in device space 20 Matrix ctm = new Matrix(); 21 // flip vertical axis 22 ctm.Scale(1, -1); 23 ctm.Translate(0, -bitmap.Height); 24 // resolution 25 ctm.Scale(dpi / 72f, dpi / 72); 26 ctm.TransformPoints(points); 27 Console.WriteLine( 28 string.Join("; ", points.Select(p => string.Format("({0}, {1})", (int)p.X, (int)p.Y))));

Changing the user space

The CTM is part of the graphics state and it can be changed using the cm operator. The cm take six operands that represent a transformation matrix. Changing the CTM will affect subsequent draw operators as you will see in the following example.

We have a page that measures 200 pt by 200 pt. The following image shows the empty page with the user space coordinate system laid on top of it:


We draw a red square measuring 50 by 50 and a smaller blue square measuring 25 by 25 inside the red square like this:


Next we transform the user space by translating it by (50, 75). Note that this is done before the figure is drawn.


Finally, the user space is rotated 30 degrees like this:


So instead of transforming the squares, we transform the user space and then draw the squares inside that user space. Depending on where you are coming from, this may feel counter-intuitive.


From a development point of view, a sequence of operators is not a convenient format. E.g. you can not easily navigate to an image on the page and retrieve its position. Its properties depend on the accumulation of all previous operators so you would have to process all of them first. The same is true for text and curves.

Changing a graphic, such as moving a single image or rotating a piece of text would be even harder because you would have to insert operators in such a way that they would only affect the targeted graphic.

PDFKit.NET allows you to extract all graphics on a page as a collection of shape objects. Internally it will do all the hard work of interpreting the operators, creating shape objects from draw operators and assigning properties that reflect the current graphics state. After extracting the shapes, you can remove shapes, insert new shapes and change their respective properties. When done, you can write the shapes back to a PDF page. This will in turn generate the required sequence of operators and operands.

Example: Replace a logo

To demonstrate the use of shapes to edit graphics, we are going to replace a logo. See below the images of the original PDF and the PDF after replacing the logo:


Here is all the code:

1 static void Main(string[] args) 2 { 3 using (FileStream fileIn = new FileStream( 4 "indesign_shortcuts.pdf", FileMode.Open, FileAccess.Read)) 5 { 6 Document pdfIn = new Document(fileIn); 7 Document pdfOut = new Document(); 8 9 foreach (Page page in pdfIn.Pages) 10 { 11 ShapeCollection shapes = page.CreateShapes(); 12 replaceLogo(shapes); 13 14 // add modified shapes to the new document 15 Page newPage = new Page(page.Width, page.Height); 16 newPage.Overlay.Add(shapes); 17 pdfOut.Pages.Add(newPage); 18 } 19 20 using (FileStream fileOut = new FileStream( 21 "out.pdf", FileMode.Create, FileAccess.Write)) 22 { 23 pdfOut.Write(fileOut); 24 } 25 } 26 } 27 static void replaceLogo(ShapeCollection shapes) 28 { 29 for (int i = 0; i < shapes.Count; i++) 30 { 31 Shape shape = shapes[i]; 32 33 if (shape is ShapeCollection) 34 { 35 // recurse 36 replaceLogo(shape as ShapeCollection); 37 } 38 else if (shape is ImageShape) 39 { 40 ImageShape oldLogo = shape as ImageShape; 41 shapes.RemoveAt(i); 42 43 ImageShape newLogo = new ImageShape("new-logo.png"); 44 newLogo.Transform = oldLogo.Transform; 45 newLogo.Width = oldLogo.Width; 46 newLogo.Height = oldLogo.Height; 47 48 shapes.Insert(i, newLogo); 49 } 50 } 51 }