Ragnarsson
Ragnarsson

Reputation: 1825

iTextSharp doesn't work with PDF generated by Fax machine?

I have a class to extract images from PDF file, using iTextSharp.

I tested with PDF generated by scan machine, it worked fine.

Then, I tested with PDF generated by fax machine, I got an IOException: .pdf not found as file or resource.

I don't have any clue why it didn't work with PDF from fax machine. Does iTextSharp not support PDF from fax machine or something?

Any thought appreciated. Thanks

EDIT

public List<Image> ExtractImagesFromFax(string sourcePdf)
    {
        var imgList = new List<Image>();

        try
        {                
            var pdfReader = new PdfReader(sourcePdf); //Error is here ...

            for (var i = 0; i <= pdfReader.XrefSize - 1; i++)
            {
                //code here
            }
            pdfReader.Close();                
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        return imgList;
    }

I tried to read PDF by iTextSharp.text.pdf.PdfReader, but I got IOException so I couldn't go further (only happened with PDF generated by fax machine).

PDF files:

Upvotes: 0

Views: 713

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77566

The IOException says that you're trying to open a file named ".pdf". One would expect the file to be named "somefile.pdf", but now you only have a dot and the extension pdf. Are you sure this is the name of the file you want to read? Are you sure your code is correct?

Note that the exception isn't really thrown by an iText class. PdfReader uses a C# class trying to open the file, and it's that C# class that tells you the file can't be found. This can mean two things:

  1. You didn't use the correct path.
  2. The application you wrote doesn't have sufficient permissions to access that file.

You can check this by writing some code that reads the file into a byte array without using iText. If that fails, you've found your problem.

Upvotes: 2

Related Questions