10

PDFドキュメントからデータを抽出する分析を実行する必要があります。

を使用してiTextSharp、このPdfTextExtractor.GetTextFromPageメソッドを使用して PDF ドキュメントからコンテンツを抽出したところ、長い 1 行で返されました。

配列に格納できるように、テキストを行ごとに取得する方法はありますか? より柔軟な行ごとにデータを分析できるようにします。

以下は私が使用したコードです:

       string urlFileName1 = "pdf_link";
        PdfReader reader = new PdfReader(urlFileName1);
        string text = string.Empty;
        for (int page = 1; page <= reader.NumberOfPages; page++)
        {
            text += PdfTextExtractor.GetTextFromPage(reader, page);
        }
        reader.Close();
        candidate3.Text = text.ToString();
4

6 に答える 6

3

これが古い投稿に投稿されていることは知っていますが、これを理解するために多くの時間を費やしたので、これをグーグルで検索しようとする将来の人々のためにこれを共有します:

using System;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

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

        string filePath = @"Your said path\the file name.pdf";
        string outPath = @"the output said path\the text file name.txt";
        int pagesToScan = 2;

        string strText = string.Empty;
        try
        {
            PdfReader reader = new PdfReader(filePath);

            for (int page = 1; page <= pagesToScan; page ++) //(int page = 1; page <= reader.NumberOfPages; page++) <- for scanning all the pages in A PDF
            {
                ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
                strText = PdfTextExtractor.GetTextFromPage(reader, page, its);

                strText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(strText)));
                //creating the string array and storing the PDF line by line
                string[] lines = strText.Split('\n');
                foreach (string line in lines)
                {
                    //Creating and appending to a text file
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(outPath, true))
                    {
                        file.WriteLine(line);
                    }
                }
            }

            reader.Close();
        }
        catch (Exception ex)
        {
            Console.Write(ex);
        }
    }
}
}

設定したパスからプログラムを PDF で読み込んで、テキスト ファイルに出力するだけでしたが、それは何にでも操作できます。これは、Snziv Gupta の反応に基づいています。

于 2018-08-16T13:25:59.650 に答える