Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public final class PdfReader

  1. Object
  2. PdfReader

Reads an existing PDF: its pages, their sizes, its metadata and its text. Runs headless, so it works on a server as well as on a device.

PdfDocument doc = new PdfDocument();
doc.addPage(PdfPageSize.A4).getCanvas().drawText("Hello", 72, 72);
byte[] bytes = doc.toByteArray();
PdfReader r = PdfReader.open(bytes);
for (int i = 0; i < r.getPageCount(); i++) {
    String text = r.extractText(i);
}

Files with a broken cross-reference table are repaired as they are read. Encrypted files that use the standard security handler (RC4, AES-128 or AES-256) open with the user or owner password, and those encrypted with an empty user password open without one.

Methods

public static PdfReader open(byte[] data) throws IOExceptionOpens a PDF held in memory.
public static PdfReader open(byte[] data, String password) throws IOExceptionOpens an encrypted PDF with password, the user or owner password.
public static PdfReader open(InputStream in) throws IOExceptionReads in to its end and opens it.
public int getPageCount()The number of pages.
public boolean isEncrypted()True when the file was encrypted.
public int getPageRotation(int index)The rotation of page index in clockwise degrees: 0, 90, 180 or 270.
public double getPageWidth(int index)The width of page index in points as displayed, after rotation.
public double getPageHeight(int index)The height of page index in points as displayed, after rotation.
public String extractText(int index)The text of page index, in drawing order, with lines separated by \n.
public String extractText()The text of every page, pages separated by a form feed (\f).
public String getTitle()The document title, or null.
public String getAuthor()The author, or null.
public String getSubject()The subject, or null.
public String getKeywords()The keywords, or null.
public String getCreator()The application that created the original document, or null.
public String getProducer()The application that wrote the PDF, or null.

Inherited methods

Method details

open

public static PdfReader open(byte[] data) throws IOException
Opens a PDF held in memory.

Throws

IOException
if the data is not a PDF, is damaged beyond repair, or is encrypted with a password other than the empty one

open

public static PdfReader open(byte[] data, String password) throws IOException
Opens an encrypted PDF with password, the user or owner password.

Throws

IOException
if the data cannot be read or the password is wrong

open

public static PdfReader open(InputStream in) throws IOException
Reads in to its end and opens it. The stream is not closed.

getPageCount

public int getPageCount()
The number of pages.

isEncrypted

public boolean isEncrypted()
True when the file was encrypted.

getPageRotation

public int getPageRotation(int index)
The rotation of page index in clockwise degrees: 0, 90, 180 or 270.

getPageWidth

public double getPageWidth(int index)
The width of page index in points as displayed, after rotation.

getPageHeight

public double getPageHeight(int index)
The height of page index in points as displayed, after rotation.

extractText

public String extractText(int index)
The text of page index, in drawing order, with lines separated by \n.

extractText

public String extractText()
The text of every page, pages separated by a form feed (\f).

getTitle

public String getTitle()
The document title, or null.

getAuthor

public String getAuthor()
The author, or null.

getSubject

public String getSubject()
The subject, or null.

getKeywords

public String getKeywords()
The keywords, or null.

getCreator

public String getCreator()
The application that created the original document, or null.

getProducer

public String getProducer()
The application that wrote the PDF, or null.