You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取PDF中文本元素的坐标?DevExpress多文章PDF拆分需求

解决单页PDF多文章拆分的坐标获取问题

1. 通过文本搜索获取起止坐标

你可以利用DevExpress的PdfTextSearchResults对象,搜索文章的起止标识文本(比如代码里的"attachment"),直接拿到文本的边界矩形坐标,这是实现区域裁剪的核心依据。

修改后的示例代码:

using DevExpress.Pdf;
using System.Drawing;

namespace DivisorPDF
{
    public class PdfManipulator
    {
        public void SplitPdfByArticle(string sourcePdfPath, string outputPdfPath)
        {
            string startMarker = "attachment"; // 文章起始标识
            string endMarker = "文章结束标识"; // 自行替换为实际结束标识

            using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
            {
                processor.LoadDocument(sourcePdfPath);
                PdfPage sourcePage = processor.Document.Pages[0];

                // 搜索起始位置
                var startSearch = processor.FindText(startMarker, new PdfTextSearchParameters { WholeWords = true });
                // 搜索结束位置
                var endSearch = processor.FindText(endMarker, new PdfTextSearchParameters { WholeWords = true });

                if (startSearch.Status == PdfTextSearchStatus.Found && endSearch.Status == PdfTextSearchStatus.Found)
                {
                    RectangleF startBounds = startSearch.Rectangles[0].BoundingRectangle;
                    RectangleF endBounds = endSearch.Rectangles[0].BoundingRectangle;

                    // 创建新PDF并裁剪指定区域
                    using (var outputProcessor = new PdfDocumentProcessor())
                    {
                        outputProcessor.CreateEmptyDocument(outputPdfPath);
                        PdfPage newPage = outputProcessor.AddNewPage(sourcePage.Size);

                        using (PdfGraphics graphics = outputProcessor.CreateGraphics())
                        {
                            // 绘制原页面的目标区域到新页面(注意PDF坐标原点在左下角)
                            var cropArea = new RectangleF(0, startBounds.Top, sourcePage.Size.Width, endBounds.Bottom - startBounds.Top);
                            graphics.DrawPdfPage(sourcePage, new RectangleF(0, 0, cropArea.Width, cropArea.Height), cropArea);
                        }

                        outputProcessor.RenderNewPageWithGraphics(newPage);
                    }
                }
            }
        }
    }
}

2. 无明确标识时的文章分界判断

如果文章没有固定起止文本,可以提取带位置的文本块,通过Y坐标的间隔判断文章分界:

// 提取页面所有带位置的文本块
var extractionParams = new PdfTextExtractionParameters { ExtractWords = true };
IEnumerable<PdfWord> pageWords = processor.GetWords(0, extractionParams);

float lastY = -1;
float splitY = -1;
// 设定坐标间隔阈值(比如20pt,可根据实际调整)
float threshold = 20;

foreach (var word in pageWords)
{
    float currentY = word.Bounds.Top;
    if (lastY != -1 && Math.Abs(currentY - lastY) > threshold)
    {
        splitY = currentY;
        break;
    }
    lastY = currentY;
}

// 用splitY作为拆分坐标,裁剪上下两个区域生成独立PDF

3. 关键注意点

  • PDF坐标原点在页面左下角,Y轴向上递增,计算区域时要注意方向
  • 若页面存在旋转,需先通过sourcePage.Rotation获取角度,对坐标做对应转换
  • 裁剪后可调整绘制参数,确保内容无偏移或截断

内容的提问来源于stack exchange,提问作者abner santos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 17:50:13