CX · Reference guidepdf
PlaygroundDownloadsAboutv0.18.0-pre.1

pdf

Text extraction from a PDF byte string, pure and with no I/O. The reader scans the file for its indirect objects (a later definition replacing an earlier one), reads object streams, walks the page tree from the catalog, decodes content streams through Flate, LZW, ASCIIHex, ASCII85 and RunLength, and decodes the text-showing operators through each font's ToUnicode CMap or its encoding (WinAnsi, MacRoman, Standard, a Differences array). text, pages, page-count and info; an encrypted document is refused, never decrypted (CXER7200-7202). And deterministic PDF generation in two pure stages (pdf.md 7): layout flows a [doc …] onto pages by the standard fonts' metrics, write renders the [pdf-layout …] as PDF 1.4 bytes — the same input, the same bytes (CXER7203-7204).

pdf:layout

[$pdf:layout] -> element — Lay a document ([doc [h1 …] [p …] [hr] [page-break]]) onto pages: word wrapping by the standard fonts' metrics, pagination, resolved positions — answering a [pdf-layout [page [text …]…]…] value, an ordinary CX document (pdf.md 7.1).

      [?lib 'cx-stdlib/pdf']
[$pdf:text [$pdf:write [$pdf:layout [doc [h2 "Lines"] [p "2 x widget = 20"] [hr] [p "Total 20"]]]]]
    
      'Lines\n2 x widget = 20\nTotal 20'
    

pdf:write

[$pdf:write] -> bytes — The bytes of a PDF 1.4 file for a [pdf-layout …] value — deterministic (no dates, no /ID: the same layout yields the same bytes), the twelve standard Latin fonts, printable ASCII text (pdf.md 7.2–7.4).

      [?lib 'cx-stdlib/pdf']
[$pdf:text [$pdf:write [pdf-layout [page width=200 height=100 [text x=10 y=50 font=helvetica size=12 "Hi (there)"] [line x1=10 y1=40 x2=190 y2=40 width=0.5] [rect x=10 y=10 w=50 h=20 fill=true gray=0.9]]]]]
    
      'Hi (there)'
    

pdf:text-width

[$pdf:text-width] -> decimal — A text run's width in points in one of the standard fonts at a size — the metrics layout wraps with (pdf.md 7.4).

      [?lib 'cx-stdlib/pdf']
[$pdf:text-width "Hello" "helvetica" 12]
    
      27.34
    

pdf:text

[$pdf:text] -> string — The text of every page of a PDF, in page-tree order, the pages joined by a newline.

      [?lib 'cx-stdlib/pdf']
[$pdf:text '%PDF-1.4 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Contents 4 0 R >> endobj 4 0 obj << >> stream BT (Hi) Tj ET endstream endobj trailer << /Root 1 0 R >>']
    
      Hi
    

pdf:pages

[$pdf:pages] -> [sequence string] — The text of each page of a PDF, one string per page, in page-tree order.

      [?lib 'cx-stdlib/pdf']
[$count [$pdf:pages '%PDF-1.4 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [3 0 R 5 0 R] /Count 2 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Contents 4 0 R >> endobj 4 0 obj << >> stream BT (one) Tj ET endstream endobj 5 0 obj << /Type /Page /Parent 2 0 R /Contents 6 0 R >> endobj 6 0 obj << >> stream BT (two) Tj ET endstream endobj trailer << /Root 1 0 R >>']]
    
      2
    

pdf:page-count

[$pdf:page-count] -> int — The number of pages of a PDF: the leaves of its page tree.

      [?lib 'cx-stdlib/pdf']
[$pdf:page-count '%PDF-1.4 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R >> endobj trailer << /Root 1 0 R >>']
    
      1
    

pdf:info

[$pdf:info] -> map — The document information dictionary (Title, Author, Producer, …) as a map of strings; an empty map when the document carries none.

      [?lib 'cx-stdlib/pdf']
[$pdf:info '%PDF-1.4 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R >> endobj 7 0 obj << /Title (Report) >> endobj trailer << /Root 1 0 R /Info 7 0 R >>']/Title
    
      Report