Class PdfReader

java.lang.Object
com.codename1.pdf.PdfReader

public final class PdfReader extends Object

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.

  • 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.
      Throws:
      IOException
    • 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.