阅读PDF内容与iTextSharp的在VB.NET或C#DLL [英] Reading PDF content with itextsharp dll in VB.NET or C#

查看:107
本文介绍了阅读PDF内容与iTextSharp的在VB.NET或C#DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样可以阅读PDF格式的内容与该Pdfreader类iTextSharp的。我的PDF可能包括文本的纯文本或图像。


解决方案

 使用iTextSharp.text.pdf;
使用iTextSharp.text.pdf.parser;
使用System.IO;公共字符串ReadPdfFile(字符串文件名)
{
    StringBuilder的文本=新的StringBuilder();    如果(File.Exists(文件名))
    {
        PdfReader pdfReader =新PdfReader(文件名);        对于(INT页= 1;页< = pdfReader.NumberOfPages;网页++)
        {
            ITextExtractionStrategy策略=新SimpleTextExtractionStrategy();
            字符串currentText = PdfTextExtractor.GetTextFromPage(pdfReader,页面策略);            currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default,Encoding.UTF8,Encoding.Default.GetBytes(currentText)));
            text.Append(currentText);
        }
        pdfReader.Close();
    }
    返回text.ToString();
}

How can I read PDF content with the itextsharp with the Pdfreader class. My PDF may include Plain text or Images of the text.

解决方案

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

public string ReadPdfFile(string fileName)
{
    StringBuilder text = new StringBuilder();

    if (File.Exists(fileName))
    {
        PdfReader pdfReader = new PdfReader(fileName);

        for (int page = 1; page <= pdfReader.NumberOfPages; page++)
        {
            ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
            string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);

            currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
            text.Append(currentText);
        }
        pdfReader.Close();
    }
    return text.ToString();
}

这篇关于阅读PDF内容与iTextSharp的在VB.NET或C#DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆