Class PdfReader
java.lang.Object
com.codename1.pdf.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.
-
Method Summary
Modifier and TypeMethodDescriptionThe text of every page, pages separated by a form feed (\f).extractText(int index) The text of pageindex, in drawing order, with lines separated by\n.The author, or null.The application that created the original document, or null.The keywords, or null.intThe number of pages.doublegetPageHeight(int index) The height of pageindexin points as displayed, after rotation.intgetPageRotation(int index) The rotation of pageindexin clockwise degrees: 0, 90, 180 or 270.doublegetPageWidth(int index) The width of pageindexin points as displayed, after rotation.The application that wrote the PDF, or null.The subject, or null.getTitle()The document title, or null.booleanTrue when the file was encrypted.static PdfReaderopen(byte[] data) Opens a PDF held in memory.static PdfReaderOpens an encrypted PDF withpassword, the user or owner password.static PdfReaderopen(InputStream in) Readsinto its end and opens it.
-
Method Details
-
open
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
Opens an encrypted PDF withpassword, the user or owner password.- Throws:
IOException- if the data cannot be read or the password is wrong
-
open
Readsinto 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 pageindexin clockwise degrees: 0, 90, 180 or 270. -
getPageWidth
public double getPageWidth(int index) The width of pageindexin points as displayed, after rotation. -
getPageHeight
public double getPageHeight(int index) The height of pageindexin points as displayed, after rotation. -
extractText
The text of pageindex, in drawing order, with lines separated by\n. -
extractText
The text of every page, pages separated by a form feed (\f). -
getTitle
The document title, or null. -
getAuthor
The author, or null. -
getSubject
The subject, or null. -
getKeywords
The keywords, or null. -
getCreator
The application that created the original document, or null. -
getProducer
The application that wrote the PDF, or null.
-