PDF guide
What is inside a PDF file and what Ibanasca PDF Mobile does to it in each operation. You do not need it to use the app, 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
- Header: the first line,
%PDF-1.7, gives the version. - Body: a series of numbered indirect objects (
3 0 obj … endobj) describing pages, fonts, images, etc. - Cross-reference table (xref): tells at which byte each object starts, so the reader can jump straight to it without reading the whole file.
- 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
| Type | Example | Use |
|---|---|---|
| Boolean | true | options |
| Number | 612, -3.5 | sizes, coordinates |
| String | (Hello), <48 65 6C 6C 6F> | text, dates, binary data |
| Name | /Page | keys and fixed values |
| Array | [0 0 595 842] | lists |
| Dictionary | << /Type /Page /Rotate 90 >> | most structures |
| Stream | dictionary + stream … endstream | page content, images, fonts |
| Null | null | missing value |
| Reference | 3 0 R | points 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
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).
| Size | Millimetres | Points |
|---|---|---|
| A3 | 297 × 420 | 842 × 1191 |
| A4 | 210 × 297 | 595 × 842 |
| A5 | 148 × 210 | 420 × 595 |
| Letter | 215.9 × 279.4 (8.5 × 11 in) | 612 × 792 |
| Legal | 215.9 × 355.6 (8.5 × 14 in) | 612 × 1008 |
When you tap the screen, the app 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, the app 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./JBIG2Decodeand/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 your finger, the app 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
/ByteRange gives the two signed ranges; the gap between them holds the signature.- A signature field is added with a
/Sigdictionary that reserves a/Contentsgap filled with zeros. /ByteRangestates which bytes are signed: the whole file except that gap.- The SHA-256 of those bytes is computed and signed with the private key; the result is a CMS block.
- 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:
| Level | Adds | In the app |
|---|---|---|
| B-B | Basic signature with the signer’s certificate and the signing-certificate-v2 attribute. | Yes |
| B-T | A timestamp from an authority (TSA) proving when the signature existed. | No |
| B-LT | Validation data (certificates, CRL, OCSP) inside the document. | No |
| B-LTA | Archive timestamps for long-term preservation. | No |
In B-B the signing time comes from your phone’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 phone’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 the app
- One core for two platforms. The PDF logic is the C++ core of Ibanasca PDF Lab for desktop, included as a git submodule. The Android interface (Kotlin, system classes only, no extra libraries) calls it through JNI. A new core feature appears in the app menus by itself.
- 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). All calls to the core go through a single worker thread so the interface does not freeze.
- PDFium, the PDF engine of the Chromium browser, renders the pages. Only visible pages are rendered, at screen resolution, and a few are cached.
- QPDF makes all structural changes: pages, merging, splitting, text, images, drawn signature and the preparation of the digital signature. It keeps content as is and writes valid files. It is built for Android with its own cryptography, without OpenSSL.
- Digital signature. The core computes the SHA-256 and builds the CMS block with its PAdES attributes. Only the last step, signing with the private key, is done by Android: with
KeyStore(“PKCS12”) for a .p12 file, or withKeyChainfor an installed certificate. The password is only used at that moment and is not stored. - Files without permissions. Opening, saving, choosing a folder and sharing use Android’s Storage Access Framework: the user chooses each file and the app only gets access to that file. To share, the app hands a temporary read-only copy through its own content provider.
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
- ISO 32000-1:2008 (PDF 1.7) and ISO 32000-2:2020 (PDF 2.0) — PDF Association, with a link to Adobe’s free copy of PDF 1.7.
- ISO 32000-2:2020, no-cost copy — PDF Association.
- ETSI EN 319 142-1 V1.2.1: PAdES building blocks and baseline signatures — ETSI, 2024.
- RFC 5652: Cryptographic Message Syntax (CMS)
- RFC 5035: ESS signing-certificate-v2 attribute
- RFC 5280: X.509 certificates and CRLs
- RFC 7292: PKCS#12
- FIPS 180-4: Secure Hash Standard (SHA-256) — NIST.
- RFC 1950 and RFC 1951: zlib and deflate formats.
- PNG specification — W3C.
Electronic signature laws (examples)
- Regulation (EU) 910/2014 (eIDAS): electronic identification and trust services.
- Law 527 of 1999 (Colombia): data messages, electronic commerce and digital signatures (in Spanish).
Libraries used
- PDFium · pdfium-binaries · QPDF · libjpeg-turbo · zlib
Android
- Access documents and other files from shared storage — Storage Access Framework, Android Developers.
- Sharing files securely — Android Developers.
- KeyChain and KeyStore — Android API reference.
- JNI tips — Android NDK.
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.