Daniel When not delving into obscure PDF or Java bugs, Daniel is exploring the new features in JavaFX.

Do you need to process or display PDF files?

Find out why you should be using IDRSolutions software

Make your own PDF file – Part 5: Path objects

1 min read

As shown in one of my Part 3: DIY Blank Page, a Pdf is drawn using a load of commands that sit in stream objects.  With these commands a Pdf viewer can figure out how to draw all the content that you see on a page.  I’m going to explore the graphics commands and create a Pdf in a text editor that draws a couple of lines on a page.

In a content stream you can create a combination of different graphics objects that a Pdf can understand.  The one we are going to muck about with in this article is the Path object.  A Path object is basically a list of points.  Each Path object has a starting point and new points are added to the list.  Each segment of the path can be a straight line, curve or rectangle.  The collection of points are treated as one Path object because the painting operation you apply is applied to all the segments of your path.  For example:

175 720 m 175 50 l h S

The previous line contains 3 commands.  The first one is m.  This means start a path at the coordinates given.  The l command means draw a line segment from the previous point in the trail to the coordinates 175, 50.  The h command closes off the path.  When you have set your path out you then have to give it some sort of paint command so that it will do something.  I used the command S which strokes the path, which but just means draws a line.

175 720 m 175 700 l 300 800 400 720 v h S

This adds another command, v, which draws a curved line. The first two coordinates are the control point which sets the bend of the line, the next two are where the line ends.

175 720 m 175 700 l 300 800 400 600 v 100 650 50 75 re h S

The re command draws a rectangle at coordinates 100 650 with a width and height of 50 and 75 respectively.  You may notice that when this gets draw the rectangle is not physically connected to the path, it is however part of the same path and the painting operation you apply at the end (S) is applied to the whole lot.

%PDF-2.0
1 0 obj <</Type /Catalog /Pages 2 0 R>>
endobj
2 0 obj <</Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 500 800]>>
endobj
3 0 obj<</Type /Page /Parent 2 0 R /Contents 4 0 R>>
endobj
4 0 obj
<</Length 61>>
stream
175 720 m 175 500 l 300 800 400 600 v 100 650 50 75 re h S
endstream
endobj
xref
0 5
0000000000 65535 f
0000000010 00000 n
0000000059 00000 n
0000000140 00000 n
0000000202 00000 n
trailer <</Size 5/Root 1 0 R>>
startxref
314
%%EOF

If you save the above as a file and open it in a Pdf viewer you should be able to see a few lines and a rectangle about the place.  Looks pretty boring at the moment so in the next article we’ll look at the different ways to fill paths in.

Next Time:  Graphics state



Our software libraries allow you to

Convert PDF files to HTML
Use PDF Forms in a web browser
Convert PDF Documents to an image
Work with PDF Documents in Java
Read and write HEIC and other Image formats in Java
Daniel When not delving into obscure PDF or Java bugs, Daniel is exploring the new features in JavaFX.

How to insert an image into a PDF

Recently, we released JPedal 2023.07 which contains the ability to insert images into PDF files. All you need is a copy of JPedal, a...
Jacob Collins
18 sec read