C# 使用itextsharp 读取pdf中文字坐标

本文介绍如何使用C#结合iTextSharp库读取并获取PDF文档中的文字坐标信息,详细讲解PdfHelper类的运用。
摘要由CSDN通过智能技术生成
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestIText
{
    class Program
    {
        static void Main(string[] args)
        {
           PdfReader readerTemp = new PdfReader(@"D:\.pdf");

            PdfHelper.LocationTextExtractionStrategyEx pz = new PdfHelper.LocationTextExtractionStrategyEx();

            iTextSharp.text.pdf.parser.PdfReaderContentParser p = new iTextSharp.text.pdf.parser.PdfReaderContentParser(readerTemp);
            p.ProcessContent<PdfHelper.LocationTextExtractionStrategyEx>(1, pz);

            Console.WriteLine(pz.GetResultantText());
            Console.ReadLine();


        }
    }
}

PdfHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using iTextSharp.text.pdf.parser;

namespace PdfHelper
{
    /// <summary>
    /// Taken from http://www.java-frameworks.com/java/itext/com/itextpdf/text/pdf/parser/LocationTextExtractionStrategy.java.html
    /// </summary>
    class LocationTextExtractionStrategyEx : LocationTextExtractionStrategy
    {
        private List<TextChunk> m_locationResult = new List<TextChunk>();
        private List<TextInfo> m_TextLocationInfo = new List<TextInfo>();
        public List<TextChunk> LocationResult
        {
            get { return m_locationResult; }
        }
        public List<TextInfo> TextLocationInfo
        {
            get { return m_TextLocationInfo; }
        }

        /// <summary>
        /// Creates a new LocationTextExtracationStrategyEx
        /// </summary>
        public LocationTextExtractionStrategyEx()
        {
        }

        /// <summary>
        /// Returns the result so far
        /// </summary>
        /// <returns>a String with the resulting text</returns>
        public override String GetResultantText()
        {
            m_locationResult.Sort();

            StringBuilder sb = new StringBuilder();
            TextChunk lastChunk = null;
            TextInfo lastTextInfo = null;
            foreach (TextChunk chunk in m_locationResult)
            {
                if (lastChunk == null)
                {
                    sb.Append(chunk.Text);
                    lastTextInfo = n
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用iTextSharp读取PDF文档可以实现对PDF文档的展示和处理。iTextSharp是一款开源的PDF文档处理库,可以在.NET平台上使用。 首先,我们需要将iTextSharp库添加到项目。可以通过NuGet包管理器来安装iTextSharp。 要使用iTextSharp读取PDF文档,我们需要先创建一个PdfReader对象,将要读取PDF文件路径作为参数传入。例如: PdfReader reader = new PdfReader("文档路径"); 然后,我们可以通过调用PdfReader对象的GetPageN()方法来获取PDF文档的页数,以便展示文档的总页数。例如: int totalPages = reader.NumberOfPages; 接下来,我们可以使用PdfReader对象的GetPageContent()方法来获取PDF文档的内容。该方法返回一个字节数组,表示当前页面的内容。我们可以将这个字节数组转换成字符串,以实现对PDF文档内容的展示。例如: byte[] pageContent = reader.GetPageContent(pageNumber); string contentText = Encoding.Default.GetString(pageContent); 最后,记得在使用PdfReader对象后,调用其Close()方法释放资源。例如: reader.Close(); 通过上述步骤,我们可以使用iTextSharp读取PDF文档并展示其内容。根据实际需求,我们还可以通过其他方法实现对PDF文档的处理,例如提取文本、插入图片或表格等。 需要注意的是,iTextSharp库是基于AGPL协议发布的,如果使用iTextSharp开发商业应用程序,则需要购买商业许可证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值