How to read File using PDFReader

I have a method public void readFile(File inputFile, File outputFile), I want to read the File using PDFReader, but the reader reads only String values, I cant hardcode the string value because it is given by the user while exporting the file.

I used FileUtils.readFileToString(inputFile) but I get exception on the line PdfReader reader = new PdfReader(input);

Kindly suggest a way to achieve this.

Code Snippet:

public void readFile(File inputFile, File outputFile){
String input = FileUtils.readFileToString(inputFile);
PdfReader reader = new PdfReader(input);

//some more code

You’re reading the whole file to a string and passing to PdfReader which is completely wrong. You need to pass path of the file or File inputFile itself.

You can try

PdfReader reader = new PdfReader(inputFile.getCanonicalPath());//or inputFile.getName()


PdfReader reader = new PdfReader(inputFile);

The PdfReader class has different constructors. When you create an instance using a String, you are supposed to pass a file path (e.g. myFolder/my.pdf). What you are doing is completely wrong! You are reading a binary file into a String. That String is not a file path!

You can easily create a PdfReader instance using a FileInputStream.

It is unclear which version of iText you are using. Please make sure that you are using iText 7. In iText 7, PdfReader has the following constructors:

PdfReader(InputStream is)
PdfReader(InputStream is, ReaderProperties properties)
PdfReader(IRandomAccessSource byteSource, ReaderProperties properties)
PdfReader(String filename)
PdfReader(String filename, ReaderProperties properties)

If for some reason you need to use iText 5, then this is the list of constructors you can use:

PdfReader(byte[] pdfIn)
PdfReader(byte[] pdfIn, byte[] ownerPassword)
PdfReader(byte[] pdfIn, Certificate certificate, ExternalDecryptionProcess externalDecryptionProcess)
PdfReader(InputStream is)
PdfReader(InputStream is, byte[] ownerPassword)
PdfReader(InputStream inputStream, Certificate certificate, ExternalDecryptionProcess externalDecryptionProcess)
PdfReader(PdfReader reader)
PdfReader(RandomAccessFileOrArray raf, byte[] ownerPassword)
PdfReader(RandomAccessFileOrArray raf, byte[] ownerPassword, boolean partial)
PdfReader(String filename)
PdfReader(String filename, byte[] ownerPassword)
PdfReader(String filename, byte[] ownerPassword, boolean partial)
PdfReader(String filename, Certificate certificate, ExternalDecryptionProcess externalDecryptionProcess)
PdfReader(String filename, Certificate certificate, Key certificateKey, String certificateKeyProvider)
PdfReader(URL url)
PdfReader(URL url, byte[] ownerPassword)

As you can see, iText 5 is much messier than iText 7. We have completely rewritten iText from scratch to get rid of plenty of legacy classes and methods that were no longer relevant. The API of iText 7 is much more slick now.

Please, read much more carefully through PdfReader constructors. Best way for your example is to wrap File into InputStream.

new PdfReader(new FileInputStream(inputFile))

thanks for the update, I am using Itext5, below is the code snippet:

public void readFile(File inputFile, File outputFile){

//i am reading the inputFile here

PdfReader reader = new PdfReader(inputFile);

//doing some logic to manipulate the pdf and then saving the pdf to outputFile

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));

//Then calling the readFile method in main class

private File showDialogBox(){

if(getSelectedFile() != null){
File inputFile = jfc.getSelectedFile();
//JFileChooser in java swing
//The selected File name will be given by the user before exporting it.
readFile(inputFile, outputFile);

Leave a Reply

Your email address will not be published. Required fields are marked *