Skip to content
Ibanasca PDF Lab

PDF guide

What is inside a PDF file and what Ibanasca PDF Lab does to it in each operation. You do not need it to use the program, but it explains its choices and its limits.

What a PDF is

PDF (Portable Document Format) is a format for fixed-layout documents: a page looks the same on any screen, system or printer. Adobe published it in 1993 and, since 2008, it has been an open international standard:

  • ISO 32000-1:2008 is equivalent to PDF 1.7. Adobe offers a technically identical copy free of charge.
  • ISO 32000-2 is PDF 2.0 (2017, revised in 2020). The PDF Association offers it at no cost.

There are also subsets for specific uses: PDF/A for long-term archiving, PDF/UA for accessibility, PDF/X for print production. A PDF is not an “editable” document like a text file: it is a description of how to draw each page. That is why changing pages is easy and changing existing text is hard.

File structure

%PDF-1.7 1 0 obj << /Type /Catalog … >> 2 0 obj << /Type /Pages … >> 3 0 obj << /Type /Page … >> 4 0 obj << /Length 44 >> stream… … xref 0 5 … trailer << /Root 1 0 R >> %%EOF Header Body: numbered objects Cross-reference tablebyte offset of every object Trailerpoints to the catalog (/Root) and the xref 5 0 obj … xref … trailer %%EOF Incremental update (optional)new or changed objects
The four parts of a PDF and an incremental update appended at the end.
  1. Header: the first line, %PDF-1.7, gives the version.
  2. Body: a series of numbered indirect objects (3 0 obj … endobj) describing pages, fonts, images, etc.
  3. Cross-reference table (xref): tells at which byte each object starts, so the reader can jump straight to it without reading the whole file.
  4. Trailer: names the root object (/Root, the catalog) and where the xref is. The file ends with %%EOF.

A PDF can be modified without rewriting it: changed objects are appended with a new xref and a new trailer (incremental update). This is how successive signatures work. Since PDF 1.5 the xref can be a compressed stream and small objects can be grouped into object streams, which reduces size.

Objects

TypeExampleUse
Booleantrueoptions
Number612, -3.5sizes, coordinates
String(Hello), <48 65 6C 6C 6F>text, dates, binary data
Name/Pagekeys and fixed values
Array[0 0 595 842]lists
Dictionary<< /Type /Page /Rotate 90 >>most structures
Streamdictionary + stream … endstreampage content, images, fonts
Nullnullmissing value
Reference3 0 Rpoints to object 3, generation 0

Page tree

The catalog (/Type /Catalog) points to a tree of /Pages nodes whose leaves are the pages (/Type /Page). Each page has:

  • /MediaBox: the paper size.
  • /Resources: the fonts, images and other resources it uses.
  • /Contents: one or more streams with the drawing instructions.
  • /Rotate (optional): 0, 90, 180 or 270 degrees, clockwise.

Some attributes are inherited from the parent node. Reordering, deleting, duplicating or moving pages only changes this tree, without touching the content, which is why it is fast and lossless. Rotating changes /Rotate. Merging copies pages from another file together with all the objects they need.

Coordinates and sizes

xy origin (0, 0) text at (72, 700) (595, 842) A4 MediaBox: 595 × 842 pt 1 point = 1/72 inch ≈ 0.353 mm
The origin is at the bottom left and the y axis grows upwards.

The unit is the point: 1/72 inch, about 0.353 mm. To convert millimetres to points, multiply by 72/25.4 ≈ 2.835. Besides /MediaBox there may be /CropBox (visible area), /BleedBox, /TrimBox and /ArtBox (for print production).

SizeMillimetresPoints
A3297 × 420842 × 1191
A4210 × 297595 × 842
A5148 × 210420 × 595
Letter215.9 × 279.4 (8.5 × 11 in)612 × 792
Legal215.9 × 355.6 (8.5 × 14 in)612 × 1008

When you click on the screen, Ibanasca converts that pixel position to page coordinates, taking zoom and rotation into account. If the page has /Rotate, text and images are written with a rotation matrix so they appear upright as you see them.

Content streams

What you see on a page is the result of running a small program of operators in postfix notation (operands first, then the operator):

q                          % save graphics state
BT                         % begin text block
/F1 12 Tf                  % font F1 at 12 points
0 0 0 rg                   % black fill color
72 700 Td                  % move to (72, 700)
(Hello, world) Tj          % show the string
ET                         % end text
150 0 0 80 72 500 cm       % image scale and position
/Im1 Do                    % draw image Im1
Q                          % restore graphics state

Other common operators: re (rectangle), m/l/c (paths), f (fill), S (stroke), Tm (text matrix), TL (leading). When adding text, an image or a drawn signature, Ibanasca does not modify the existing content: it appends a new stream to /Contents, wrapped in q and Q so the previous graphics state is not altered.

Fonts and text

PDF defines 14 standard fonts that every reader must know without them being embedded: Helvetica, Times and Courier (each in regular, bold, italic and bold italic), Symbol and ZapfDingbats. Ibanasca uses Helvetica, Times and Courier in regular and bold, so added text barely increases the file size.

These fonts use WinAnsi encoding (similar to Windows-1252): one byte per character, covering Western European languages. Writing other scripts would require embedding a TrueType or OpenType font with its glyphs and a ToUnicode map, which Ibanasca does not do in this version.

Editing existing text is hard because a PDF does not store paragraphs or lines: it stores pieces of text placed at exact positions, often with subset fonts (only the characters used). That is why Ibanasca only adds text.

Images

An image is an /XObject stream with /Subtype /Image, its width, height, color space (/DeviceRGB, /DeviceGray…) and bits per component. Transparency is stored separately, in a grayscale soft mask (/SMask).

When inserting a PNG, Ibanasca decompresses it, separates the colors from the alpha channel (or from the tRNS transparent color), creates the image and its /SMask and compresses them with Flate. For the visual signature, white and very light grays become transparent with a smooth transition so no halo is left.

Compression

Streams are compressed with filters. The most common:

  • /FlateDecode: deflate (zlib), lossless, the same algorithm as ZIP and PNG.
  • /DCTDecode: JPEG, lossy, for photographs.
  • /JBIG2Decode and /CCITTFaxDecode: black-and-white images, typical of scans.
  • /JPXDecode: JPEG 2000.

Ibanasca never recompresses existing images, so its operations do not reduce quality.

Passwords and encryption

A PDF can be encrypted with two passwords: the user password, needed to open it, and the owner password, which controls permissions such as printing or copying. Modern versions use 128- or 256-bit AES; older ones use RC4.

Ibanasca can open and view documents with a user password and change their pages, but it cannot merge or sign encrypted documents.

Digital signatures

Visual and digital signatures

A visual signature is a drawing or an image: it shows a handwritten mark, but anyone could copy or move it. A digital signature is a cryptographic computation that proves two things: who signed (the certificate holder) and integrity (the document has not changed since). It can appear as a box or be invisible.

The drawn signature

When you sign with the mouse, Ibanasca does not store a picture of the signature: it stores the strokes. Each stroke is a list of points that is thinned (points too close to each other are removed) and smoothed with cubic Bézier curves through the midpoints. In the PDF it becomes a path with the operators m (move), c (curve) and S (stroke), with round caps and joins (1 J 1 j) and the chosen width and color:

q 1 J 1 j 0.122 0.227 0.576 RG 1.6 w
12.4 30.1 m 14.0 33.2 17.5 35.8 21.3 34.9 c … S
Q

That is why the drawn signature looks sharp at any zoom and barely increases the file size. It is still a visual signature: proving authorship needs the digital signature, which can use that same drawing as its appearance.

The ingredients

  • Digest (hash): a fixed-length fingerprint of the document. Ibanasca uses SHA-256: changing a single bit of the file completely changes the fingerprint.
  • Key pair: the private key signs and only the holder has it; the public key, included in the certificate, lets anyone verify the signature.
  • X.509 certificate: an electronic document in which a certification authority states that the public key belongs to a person or organization, for a validity period.
  • PKCS#12 (.p12/.pfx): a password-protected file containing the certificate and the private key.
  • CMS (PKCS#7): the structure that holds the signature, the certificate and the signed attributes.

How a PDF is signed

signed bytes <3082…0000> signed bytes /ByteRange [0 a b c] 0a bb+c /Contents gap: CMS signature in hex SHA-256 of the signed bytes signed with the certificate private key (.p12)
/ByteRange gives the two signed ranges; the gap between them holds the signature.
  1. A signature field is added with a /Sig dictionary that reserves a /Contents gap filled with zeros.
  2. /ByteRange states which bytes are signed: the whole file except that gap.
  3. The SHA-256 of those bytes is computed and signed with the private key; the result is a CMS block.
  4. The block, in hexadecimal, is written into the gap. The file size does not change, so the digest remains valid.

To verify it, the reader recomputes the digest of the same bytes, compares it with the signed one using the certificate’s public key, and checks the certificate chain up to a trusted authority.

PAdES

PAdES (ETSI EN 319 142) is the European profile for PDF signatures. It uses the ETSI.CAdES.detached sub-filter and defines levels:

LevelAddsIn Ibanasca
B-BBasic signature with the signer’s certificate and the signing-certificate-v2 attribute.Yes
B-TA timestamp from an authority (TSA) proving when the signature existed.No
B-LTValidation data (certificates, CRL, OCSP) inside the document.No
B-LTAArchive timestamps for long-term preservation.No

In B-B the signing time comes from your computer’s clock and is stored in the /M entry of the signature dictionary; PAdES does not allow it as an attribute inside the CMS. Because it depends on the computer’s clock, it does not prove the date to third parties: that needs a timestamp (level B-T).

Legal validity

A technically correct signature does not by itself decide its legal value: that depends on each country’s law and on the type of certificate. Examples: Regulation (EU) 910/2014 (eIDAS) in the European Union, or Law 527 of 1999 in Colombia. Ask whoever requests your signature which certificate and format they accept.

Inside Ibanasca

  • In-memory document. When opened, the whole PDF is loaded into memory. Each operation produces a new version of the bytes; the previous one goes to the undo stack (up to 20).
  • PDFium, the PDF engine of the Chromium browser, renders pages and thumbnails. Only visible pages are rendered, and a few are cached.
  • QPDF makes all structural changes: pages, merging, splitting, text, images and signature preparation. It keeps content as is and writes valid files.
  • Digital signature. The core computes the SHA-256 and builds the CMS block with its PAdES attributes itself (since version 1.1). GnuTLS only reads the .p12 file and signs with the private key; the password is used at that moment and is not stored. This way the same core works on Android, where the key is handled by the system.
  • Safe writing. The file is written under a temporary name and then renamed; the original is never overwritten.

Glossary

AcroForm
The document’s interactive form; signatures are a kind of field.
Certification authority (CA)
Organization that issues and vouches for certificates.
CMS / PKCS#7
Standard format for signed data (RFC 5652).
Content stream
Instructions that draw a page.
Glyph
The concrete drawing of a character in a font.
Hash
Fixed-length cryptographic digest.
Point (pt)
1/72 inch.
SMask
Transparency mask of an image.
TSA
Time-stamping authority.
xref
Table with the position of each object in the file.

References

Standards

Electronic signature laws (examples)

Libraries used

Books

  • John Whitington, PDF Explained, O'Reilly, 2011. A clear introduction to the internal structure.
  • Leonard Rosenthol, Developing with PDF, O'Reilly, 2013. Content, fonts, images and annotations.
Back to top ↑