7

我必须从 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 中提取文本?

4

3 回答 3

10

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

PdfTextExtractor.GetTextFromPage(reader, pageNumber);

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

于 2012-10-18T12:57:29.220 回答
5
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();
        }
    }
}
于 2014-05-12T21:37:44.007 回答
-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();
    }
} 
于 2018-10-19T16:31:25.113 回答