Andreas777
Andreas777

Reputation: 377

Get text from PDF stored in LocalFolder using iTextSharp

I am trying to get the text from a PDF stored in localStorage in a Windows Phone 8.1 application,but I always get an FileNotFoundException.

To explain the whole story, I get a PDF from an online source, I store it to a folder with name same as the username (The username is an email address, but I tried also without the @ sign) of the user and then I want to get some text from the PDF file. I use iTextSharp and follow the examples, but cannot succeed. When I send the PDF to the Launcher is opening succesfully with another app like Acrobat Reader.

My function is like below. I first send an PDF Object, which has an attribute called Path and it is stored to folder specific to the username of the user. Then I get the pdf as a StorageFile Item. When I create the PDFReader calling the constructor I get a FileNotFoundException. Does anybody knows or can guess what can be the problem? Is iTextSharp compatible with Windows Phone 8.1?

internal async Task<bool> OpenPdfFromDownloadedCollections(PDF pdfToOpen, string username)
    {
        try
        {
            StorageFolder folder = ApplicationData.Current.LocalFolder;
            var pdfFolder = await folder.GetFolderAsync(username + "PDFs");

            var pdf = await pdfFolder.GetFileAsync(Object.Path);

            StringBuilder text = new StringBuilder();
            using (PdfReader reader = new PdfReader(pdf.Path))
            {
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    string thePage = PdfTextExtractor.GetTextFromPage(reader, i, its);
                    string[] theLines = thePage.Split('\n');
                    foreach (var theLine in theLines)
                    {
                        text.AppendLine(theLine);
                    }
                }
            }
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

Upvotes: 0

Views: 727

Answers (1)

soumya sambit Kunda
soumya sambit Kunda

Reputation: 508

var pdf = await pdfFolder.GetFileAsync(Object.Path);

In this line of code you should only pass the file name but you are giving the whole Path as parameter. As pdfFolder currently represents the path.

Upvotes: 0

Related Questions