poppler clib

PDF reading and rendering. Extract text, render pages to PNG at any DPI, search content, enumerate links, annotations, and metadata.

ppm install poppler
Requires libpoppler-dev and libpoppler-glib-dev: sudo apt install libpoppler-dev libpoppler-glib-dev

Overview

Poppler is the de facto standard open-source PDF rendering library, used by Evince, Okular, LibreOffice, and Chrome on Linux. It can extract plain text from any PDF, render pages to images at arbitrary resolution, and inspect all document structure.

Use libharu to create PDFs. Use poppler to read them.

Quick Start — Text Extraction

uses poppler;

var doc := OpenPDF('/tmp/report.pdf');
Writeln('Pages: ' + IntToStr(GetPageCount(doc)));

{ Extract text from page 1 }
var text := GetPageText(doc, 1);
Writeln(text);

{ Extract all text at once }
var all := GetAllText(doc);

{ Metadata }
var meta := GetMeta(doc);
Writeln(meta.Title + ' by ' + meta.Author);

ClosePDF(doc);

Rendering Pages to Images

var doc := OpenPDF('/tmp/manual.pdf');

{ Render page 1 at 150 DPI, get PNG bytes }
var pngBytes := RenderPage(doc, 1, 150);

{ Save page 2 directly to a file }
RenderPageToFile(doc, 2, 150, '/tmp/page2.png');

{ Render ALL pages to numbered files }
var n := RenderAllPages(doc, 150, '/tmp/page_%03d.png');
Writeln(IntToStr(n) + ' pages rendered');

{ Raw RGB pixels for custom processing }
var w, h: Integer;
var rgb := RenderPageRGB(doc, 1, 72, w, h);
{ rgb contains w*h*3 bytes in R,G,B order }

ClosePDF(doc);

Text Search

var doc     := OpenPDF('/tmp/contracts.pdf');
var results := SearchText(doc, 'termination clause');

for r in results do
begin
  Writeln('Page ' + IntToStr(r.Page) + ': ' + r.Context);
end;

{ Case-insensitive }
var hits := SearchTextCI(doc, 'payment');

ClosePDF(doc);

Links & Annotations

var doc   := OpenPDF('/tmp/paper.pdf');
var links := GetLinks(doc, 1);  { page 1 }

for lnk in links do
begin
  if lnk.URI <> '' then
    Writeln('URL: ' + lnk.URI)
  else
    Writeln('Internal link to page ' + IntToStr(lnk.DestPage));
end;

var ann := GetAnnotations(doc, 2);
for a in ann do
  Writeln(a.AnnotType + ': ' + a.Content);

ClosePDF(doc);

Encrypted PDFs

var doc := OpenPDFPassword('/tmp/protected.pdf', 'secret');
if doc = 0 then
  Writeln('Wrong password or corrupt file')
else
begin
  Writeln(GetAllText(doc));
  ClosePDF(doc);
end;

API Reference

Open / Close

FunctionDescription
OpenPDF(Path)Open PDF file. Returns 0 on failure.
OpenPDFBytes(Data)Open PDF from in-memory bytes.
OpenPDFPassword(Path, Password)Open password-protected PDF.
ClosePDF(Doc)Close document and free resources.

Document Info

FunctionDescription
GetPageCount(Doc)Total number of pages.
GetMeta(Doc)Returns TPDFMeta: Title, Author, Subject, Keywords, Creator, Producer, PageCount, Encrypted, PDFVersion.
GetPageInfo(Doc, Page)Returns TPDFPageInfo: Width, Height (points), Rotation, HasText.
GetTOC(Doc)Table of contents as indented text with page numbers.

Text Extraction

FunctionDescription
GetPageText(Doc, Page)Extract text from page N (1-based).
GetTextRange(Doc, First, Last)Extract text from a range of pages.
GetAllText(Doc)Extract all text concatenated.
GetAllPagesText(Doc)Extract all text as array of strings (one per page).
GetTextRect(Doc, Page, X,Y,W,H)Extract text from a specific rectangle on a page.

Rendering

FunctionDescription
RenderPage(Doc, Page, DPI)Render page to PNG bytes. 72 DPI = original size.
RenderPageToFile(Doc, Page, DPI, Path)Render and save PNG to file.
RenderAllPages(Doc, DPI, Pattern)Render all pages to numbered PNG files.
RenderPageRGB(Doc, Page, DPI, W, H)Raw RGB bytes for custom processing.

Links, Annotations & Search

FunctionDescription
GetLinks(Doc, Page)Array of TPDFLink: URI or DestPage, bounding box.
GetAnnotations(Doc, Page)Array of TPDFAnnotation: type, content, author, bounding box.
SearchText(Doc, Query)Find all occurrences. Returns TPDFSearchResult array.
SearchTextRange(Doc, Query, First, Last)Search within a page range.
SearchTextCI(Doc, Query)Case-insensitive search.