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

本文介绍如何在C#中利用iTextSharp库读取PDF文档中的文字及其坐标信息,通过示例代码展示程序调用及PdfHelper帮助类的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

程序调用:

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:\_Number position.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_locationResu
C#中,iTextSharp是一个流行的开源库,用于处理PDF文档,包括读取和操作内容。如果你想要使用iTextSharp获取PDF中文字坐标,你需要首先安装iTextSharp库,然后通过PdfReader、PdfStamper或PdfCopy等类来访问PDF页面和文本信息。 下面是一个简单的步骤示例: 1. 安装iTextSharp NuGet包:在Visual Studio中,右键点击项目 -> 管理NuGet包 -> 搜索 "iTextSharp" 并安装。 2. 加入必要的命名空间: ```csharp using iTextSharp.text; using iTextSharp.text.pdf; ``` 3. 使用PdfReader实例打开PDF文件,并遍历每一页: ```csharp string filePath = "your_pdf_file_path"; PdfReader reader = new PdfReader(filePath); foreach (int page in reader.GetPages()) { // 获取当前页的内容 ImageRenderedForm form = reader.GetPageImage(page); using (var imageStream = new MemoryStream(form.ToImage())) { // 使用iTextSharp工具解析PDF,查找文本并获取其位置 var parser = new SimpleTextExtractionStrategy(); List<ITextElement> elements = ElementHelper.GetTextElementsFromImage(imageStream, parser); foreach (ITextElement element in elements) { if (element is ITextLineSegment line) { // line.ElementBox是最小的包围矩形,包含该文本的边界 RectangleF bbox = line.ElementBox; float x = bbox.Left; float y = bbox.Top; // 这里的y值通常是负数,需要根据实际需求转换 Console.WriteLine($"Text at ({x}, {y}): {element.ToString()}"); } } } } reader.Close(); // 关闭读者 ``` 在这个例子中,`ElementBox`属性包含了文本所在的坐标。请注意,由于PDF坐标系统通常是从左上角开始计数,而iTextSharp返回的是从下往上的Y轴,所以实际应用中可能需要调整Y坐标的正负。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值