To extract text from PDF in Java you need a PDF library, because the JDK has no PDF support. JPedal, the pure Java PDF library from IDRsolutions, extracts plain text page by page, individual words with their coordinates, the document outline, and, from tagged PDFs, structured text as ePUB, HTML, JSON, Markdown, XML or YAML. Apache PDFBox is the free alternative: it handles plain text well but leaves you to assemble structured output yourself.
TL;DR
Which method you need depends on the PDF. Plain and word-level extraction work on any text-based PDF. Structured text extraction only works on tagged PDFs, the ones that carry an internal structure tree describing headings, paragraphs, lists and tables. If a PDF is not tagged (most scanned files and a lot of older documents), there is no structure to pull out and you get an empty result.
Why structure matters in PDF text extraction
A PDF was never designed as an editable text document. Internally it holds draw commands for text, images and shapes, positioned by coordinates. There are no paragraph markers, no reading order, often not even spaces between words. We have come across files where every letter "a" on the page is drawn first, then every letter "b", and so on.
Later, Adobe added a way to embed the missing information: marked content, also called tagging. A tagged PDF wraps its content in a structure tree, similar to HTML, where a run of text is labelled h1, p, table and so on. When that tree is present, extraction is accurate and the reading order is the one the author intended. When it is absent, there is nothing to extract structurally, and any tool will either return empty output or fall back to working out the layout from coordinates.
For developers working in sectors that require high compliance (like government or legal), it is also worth referencing PDF/UA (ISO 14289). This is the international standard for "Universal Accessibility", ensuring that the structured text you extract is accessible to all users and assistive technologies.
So before you debug your code, check whether the file is actually tagged. A tool returning nothing is usually correct, not broken.
Extract text from PDF in Java: examples with JPedal
All four JPedal examples below follow the same pattern:
- Download the JPedal trial jar
- Create a File handle, byte array or file path pointing to the PDF file
- Include a password if the file is password protected
- Open the PDF file
- Extract the text (per page, or for the whole document)
- Close the PDF file
JPedal is pure Java with no native binaries and no third-party dependencies, and it needs Java 17 as a minimum. The JPedal text extraction docs list every extraction method and output format.
Plain (unstructured) text
ExtractTextInRectangle extracts all the text on a page, or inside a rectangle you specify. It works on any text-based PDF, tagged or not, and tries to put the content in the order it appears on the page.
ExtractTextInRectangle extract = new ExtractTextInRectangle("C:/pdfs/mypdf.pdf");
extract.setOutputFormat(ExtractTextInRectangle.OUTPUT_FORMAT.TXT); // or OUTPUT_FORMAT.XML
//extract.setPassword("password");
if (extract.openPDFFile()) {
int pageCount = extract.getPageCount();
for (int page = 1; page <= pageCount; page++) {
String text = extract.getTextOnPage(page);
// or a region: x1, y1 (lower left), x2, y2 (upper right)
// String text = extract.getTextOnPage(page, x1, y1, x2, y2);
}
}
extract.closePDFfile();
Rectangle coordinates use the PDF page origin, which is bottom left (the opposite of Java). If a page contains text in more than one orientation, only the text in the most common orientation is extracted. See extract unstructured text for details.
Below is an example of the original PDF vs the extracted unstructured text:

Words with coordinates
ExtractTextAsWordlist returns each word on the page, which is useful for search, highlighting or building your own layout analysis.
ExtractTextAsWordlist extract = new ExtractTextAsWordlist("C:/pdfs/mypdf.pdf");
//extract.setPassword("password");
if (extract.openPDFFile()) {
int pageCount = extract.getPageCount();
for (int page = 1; page <= pageCount; page++) {
List<String> wordList = extract.getWordsOnPage(page);
}
}
extract.closePDFfile();
When written to file, each line holds the word followed by its x1, y1, x2, y2 coordinates (left, bottom, right and top, with the origin at the bottom left of the page). See extract words on a page.
Below is an example of the original PDF vs the extracted word list:

Document outline
Many PDFs include an optional document outline: the table of contents shown in the bookmarks panel of a PDF viewer. ExtractOutline returns it as an XML Document.
ExtractOutline extract = new ExtractOutline("C:/pdfs/mypdf.pdf");
//extract.setPassword("password");
if (extract.openPDFFile()) {
Document pdfOutline = extract.getPDFTextOutline();
}
extract.closePDFfile();
If the PDF has no outline, there is nothing to return. See extract the document outline.

Structured text from tagged PDFs
ExtractStructuredText reads the structure tree of a tagged PDF and returns it in one call.
ExtractStructuredTextProperties properties = new ExtractStructuredTextProperties();
properties.setFileOutputMode(OutputModes.XML);
//properties.setFileOutputMode(OutputModes.HTML);
ExtractStructuredText extract = new ExtractStructuredText("C:/pdfs/mypdf.pdf", properties);
//extract.setPassword("password");
if (extract.openPDFFile()) {
Document anyStructuredText = extract.getStructuredTextContent();
}
extract.closePDFfile();
JPedal can extract structured text as ePUB, HTML, JSON, Markdown, XML and YAML. The full details are in the JPedal structured text extraction docs, and you can find out more about the JPedal Java PDF library on its product page.
OutputModes.MARKDOWN and OutputModes.JSON are the ones worth knowing if the output is headed into an AI pipeline. Both preserve the heading and table hierarchy in a shape that drops straight into a RAG workflow. There are dedicated walkthroughs for PDF to Markdown and PDF to JSON if that is your target.
Extracting text with open source (Apache PDFBox)
Apache PDFBox is the standard free option. It is Apache 2.0 licensed, so it is safe to use in commercial software without the copyleft strings that come with some alternatives. The trade-off is that PDFBox gives you the building blocks, not a finished structured document.
Add the dependency:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>3.0.8</version>
</dependency>
If you only need the words in roughly the right order and can live without a real hierarchy, PDFTextStripper is the quick answer. It works on any text-based PDF, tagged or not. This is plain text extraction, the PDFBox equivalent of ExtractTextInRectangle, not structure:
// Apache PDFBox 3.0.8
try (PDDocument doc = Loader.loadPDF(new File("C:/pdfs/mypdf.pdf"))) {
PDFTextStripper stripper = new PDFTextStripper();
stripper.setSortByPosition(true); // best-effort reading order
String text = stripper.getText(doc);
System.out.println(text);
}
To preserve the actual logical structure, you read the structure tree yourself. This is the part that JPedal does in one call. In PDFBox you walk PDStructureTreeRoot and print the tag of each element:
try (PDDocument doc = Loader.loadPDF(new File("C:/pdfs/mypdf.pdf"))) {
PDStructureTreeRoot root = doc.getDocumentCatalog().getStructureTreeRoot();
if (root == null) {
System.out.println("No structure tree: this PDF is not tagged.");
return;
}
printTags(root, 0);
}
// Recurse through the logical structure, printing the tag hierarchy (H1, P, Table, ...)
static void printTags(PDStructureNode node, int depth) {
for (Object kid : node.getKids()) {
if (kid instanceof PDStructureElement) {
PDStructureElement element = (PDStructureElement) kid;
System.out.println(" ".repeat(depth) + element.getStructureType());
printTags(element, depth + 1);
}
}
}
That prints the skeleton: the order and nesting of headings, paragraphs and tables. It does not print the text inside each tag. For that you pair the walk with PDFMarkedContentExtractor, which gives you the marked content on each page keyed by its MCID (getMarkedContents()), then you match each structure element's MCID references back to that content. It works, but you are writing and maintaining the assembly logic that a dedicated structured extractor already ships.
iText can also read the structure tree, with the same manual assembly effort, and its licensing is AGPL or commercial rather than permissive. If open source with no copyleft is the requirement, PDFBox is the practical choice.
JPedal vs PDFBox vs iText
| Library | License | One-call structured output | Output formats | Setup effort |
|---|---|---|---|---|
| Apache PDFBox | Apache 2.0 | No | Manual assembly | High |
| iText | AGPL or commercial | No | Manual assembly | High |
| JPedal | Commercial | Yes | ePUB, HTML, JSON, Markdown, XML, YAML | Low |
Which PDF text extraction library to use?
If the job is a one-off script, the PDFs are simple, and you have time to write structure-tree handling, PDFBox costs nothing and does the work. If you are shipping structured extraction as a feature, feeding tagged PDFs into a RAG or documentation pipeline, or you need consistent JSON or Markdown out of the box, the assembly code you would write around PDFBox is the thing JPedal already is.
FAQs
Q: Can I extract structured text if the PDF wasn't created with "Marked Content"?
A: No. If the PDF was not originally generated with the necessary structural metadata, the file only contains raw "draw" commands. You can still extract plain text or a word list, but to get structure you would need a content grouping or layout engine to infer it, rather than extracting it directly.
Q: Does text extraction work on scanned documents?
A: Only if the scan already has a text layer. A plain scanned page is an image, so it has no text and no structural metadata. To get text from a scan, you would first need to run Optical Character Recognition (OCR) to generate the text layer and then apply structure to those results.
Q: How does structured extraction handle multi-column layouts?
A: Because structured text uses "Marked Content", it follows the reading order defined by the creator. Basic text extraction might read across the page and mix sentences from two different columns; structured extraction follows the intended flow of the article or report.