C# Pdf to Text with values in multiple line

余生颓废 提交于 2021-01-29 03:01:58

问题


Hi I have a pdf with content as following : -

Property Address: 123 Door         Form Type: Miscellaneous
                  ABC City
                  Pin - XXX

So when I use itextSharp to get the content, it is obtained as follows -

Property Address: 123 Door Form Type: Miscellaneous ABC City Pin - XXX

The data is mixed since it is in next line. Please suggest a possible way to get the content as required. Thanks

Property Address: 123 Door ABC City Pin - XXX Form Type: Miscellaneous

回答1:


The following code using iTextSharp helped in formatting the pdf -

PdfReader reader = new PdfReader(path);
int pagenumber = reader.NumberOfPages;
for (int page = 1; page <= pagenumber; page++)
{
    ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
    string tt = PdfTextExtractor.GetTextFromPage(reader, page , strategy);
    tt = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(tt)));
    File.AppendAllLines(outfile, tt, Encoding.UTF8);
}



回答2:


I'm Using Below helper class to convert PDF to Text file. this one is working clam for me. If any one need full working desktop application please refer this github repo https://github.com/Kithuldeniya/PDFReader

using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser;
using iText.Kernel.Pdf.Canvas.Parser.Listener;
using System;

namespace PDFReader.Helpers
{
    public static class PdfHelper
    {
        public static string ManipulatePdf(string filePath)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(filePath));

            //CustomFontFilter fontFilter = new CustomFontFilter(rect);
            FilteredEventListener listener = new FilteredEventListener();

            // Create a text extraction renderer
            LocationTextExtractionStrategy extractionStrategy = listener
                .AttachEventListener(new LocationTextExtractionStrategy());

            // Note: If you want to re-use the PdfCanvasProcessor, you must call PdfCanvasProcessor.reset()
            new PdfCanvasProcessor(listener).ProcessPageContent(pdfDoc.GetFirstPage());

            // Get the resultant text after applying the custom filter
            String actualText = extractionStrategy.GetResultantText();

            pdfDoc.Close();

            return actualText;

        }
    }
}


来源:https://stackoverflow.com/questions/42383123/c-sharp-pdf-to-text-with-values-in-multiple-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!