In this post we’ll see a Java program to read PDF document using OpenPDF library.
To know more about OpenPDF library and PDF examples check this post- Generating PDF in Java Using OpenPDF Tutorial
Reading PDF using OpenPDF
For reading PDF using OpenPDF you need to perform the following steps.
- Create a PDFReader instance with the path to PDF that has to be read.
- Wrap PDFReader instance in an instance of PdfTextExtractor.
- Get PDF content using pdfTextExtractor.getTextFromPage() method.
import java.io.IOException;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.parser.PdfTextExtractor;
public class ReadPDF {
// PDF to be read
public static final String READ_PDF = "F://knpcode//result//OpenPDF//Content.pdf";
public static void main(String[] args) {
PdfReader pdfreader = null;
try {
pdfreader = new PdfReader(READ_PDF);
// get pages in PDF
int pages = pdfreader.getNumberOfPages();
PdfTextExtractor pdfTextExtractor = new PdfTextExtractor(pdfreader);
// Iterate through pages to read content
for(int i = 1; i <= pages; i++) {
// Extract content of each page
String contentOfPage = pdfTextExtractor.getTextFromPage(i, true);
System.out.println(contentOfPage);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(pdfreader != null) {
pdfreader.close();
}
}
}
}
That's all for the topic Read PDF in Java Using OpenPDF. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Password Protected PDF Using OpenPDF in Java
- Read PDF in Java Using iText
- How to Convert float to int in Java
- Java Program to Convert Date to LocalDate, LocalDateTime
- SynchronousQueue in Java With Examples
- Comparable Vs Comparator in Java
- Java BigDecimal Class With Examples
- Spring Boot With JSP Example
No comments:
Post a Comment