Write PDF text file line by line

0

I need to extract text from pdf files (line by line) but it does not write to me in the text file;

class Program
{
    static void Main(string[] args)
    {

        PdfReader reader = new PdfReader(@"c:\temp\textoPDF.pdf");
        int intPageNum = reader.NumberOfPages;
        string[] words;
        string line;
        string text;

        for (int i = 1; i <= intPageNum; i++)
        {
            text = PdfTextExtractor.GetTextFromPage(reader, i, new LocationTextExtractionStrategy());

            words = text.Split('\n');

                for (int j = 0, len = words.Length; j < len; j++)
                {
                    line = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(words[j]));
                    File.WriteAllText(@"c:\temp\", line);
                }

        }
    }
}

I get error in File.WriteAllText Unable to find a part of the path 'c: \ temp \'. ' Why does the error come out?

    
asked by ger 12.11.2018 в 19:58
source

1 answer

0

You have to define the name of a file

File.WriteAllText(@"c:\temp\archivo.txt", line);

You will see that I indicate the archivo.txt in the route

Also the WriteAllText() is supposed to be used when you have all the full text, not within a for

But you should use something like this

List<string> lineas = new List<string>();

for (int i = 1; i <= intPageNum; i++)
{
    text = PdfTextExtractor.GetTextFromPage(reader, i, new LocationTextExtractionStrategy());

    words = text.Split('\n');

    for (int j = 0, len = words.Length; j < len; j++)
    {
        string line = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(words[j]));
        lineas.Add(line);
    }

}

File.WriteAllLines(@"c:\temp\archivo.txt", lineas);
    
answered by 12.11.2018 / 20:45
source