如何在iTextSharp中将PDF转换为文本文件

时间:2012-10-18 11:52:06

标签: c# pdf itextsharp

我必须从PDF文件中检索文本。但是使用下面的代码我只得到空文本文件。

for (int i = 0; i < n; i++)
{
    pagenumber = i + 1;
    filename = pagenumber.ToString();
    while (filename.Length < digits) filename = "0" + filename;
    filename = "_" + filename;
    filename = splitFile + name + filename;
    // step 1: creation of a document-object
    document = new Document(reader.GetPageSizeWithRotation(pagenumber));
    // step 2: we create a writer that listens to the document
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filename + ".pdf", FileMode.Create));

    // step 3: we open the document
    document.Open();

    PdfContentByte cb = writer.DirectContent;
    PdfImportedPage page = writer.GetImportedPage(reader, pagenumber);
    int rotation = reader.GetPageRotation(pagenumber);
    if (rotation == 90 || rotation == 270)
    {
        cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pagenumber).Height);
    }
    else
    {
        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
    }
    // step 5: we close the document

    document.Close();
    PDFParser parser = new PDFParser();
    parser.ExtractText(filename + ".pdf", filename + ".txt");
}

我做错了什么以及如何从PDF中提取文字?

3 个答案:

答案 0 :(得分:9)

使用iTextSharp进行文本提取时,请获取该库的当前版本并使用

PdfTextExtractor.GetTextFromPage(reader, pageNumber);

请注意,某些5.3.x版本的文本提取代码中存在一个错误,同时已在主干中修复。因此,您可能想要检查主干版本。

答案 1 :(得分:4)

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

namespace Pdf2Text
{
    class Program
    {
        static void Main(string[] args)
        {
            if (!args.Any()) return;

            var file = args[0];
            var output = Path.ChangeExtension(file, ".txt");
            if (!File.Exists(file)) return;

            var bytes = File.ReadAllBytes(file);
            File.WriteAllText(output, ConvertToText(bytes), Encoding.UTF8);
        }

        private static string ConvertToText(byte[] bytes)
        {
            var sb = new StringBuilder();

            try
            {
                var reader = new PdfReader(bytes);
                var numberOfPages = reader.NumberOfPages;

                for (var currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
                {
                    sb.Append(PdfTextExtractor.GetTextFromPage(reader, currentPageIndex));
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }

            return sb.ToString();
        }
    }
}

答案 2 :(得分:-2)

//call Create_pdf() function when Done button pressed;

EditText et_Text=findViewById(R.id.EditText);

String projectname="MyPdf";

public  void Create_pdf(){
    Document doc =new Document();
    String outPath= Environment.getExternalStorageDirectory()+"/"+projectname+".pdf";
    try {
       PdfWriter.getInstance(doc, new FileOutputStream(outPath));
       doc.open();
       doc.add(new Paragraph(et_Text.getText().toString()));
       doc.close();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}