Skip to content

Commit 2bd5471

Browse files
author
KB Bot
committed
Added new kb article get-textfragment-position-size-radpdfprocessing
1 parent 8666dfc commit 2bd5471

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: Getting Position and Size of TextFragment in PDF Documents
3+
description: Learn how to retrieve the (x,y) position and dimensions of a TextFragment within a PDF document using RadPdfProcessing.
4+
type: how-to
5+
page_title: How to Determine TextFragment Position and Size in PDFs with RadPdfProcessing
6+
slug: get-textfragment-position-size-radpdfprocessing
7+
tags: radpdfprocessing, textfragment, position, size, document processing
8+
res_type: kb
9+
ticketid: 1662399
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 2024.3.806| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
17+
18+
## Description
19+
20+
Determining the precise location and size of a [TextFragment]({%slug radpdfprocessing-model-textfragment%}) in a PDF document is essential for various document processing tasks. A `TextFragment`'s position is accessible through its `Position` property, which returns a `MatrixPosition`. This article outlines how to translate the `MatrixPosition` into (x,y) coordinates and how to calculate the width and height of a `TextFragment`.
21+
22+
This KB article also answers the following questions:
23+
- How can I find the exact location of a text in a PDF document?
24+
- What is the method to determine the dimensions of a text segment within a PDF file?
25+
- How to use the `MatrixPosition` for locating text in a PDF document?
26+
27+
## Solution
28+
29+
To obtain the (x,y) coordinates and the dimensions (width and height) of a `TextFragment`, follow these steps:
30+
31+
1. **Extract the (x,y) Coordinates:**
32+
33+
Each `TextFragment` has a `Position` property of type [MatrixPosition](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/concepts/position#matrixposition). The `MatrixPosition` object includes a `Matrix` that exposes `OffsetX` and `OffsetY` properties. These properties represent the fragment's offset from the top-left corner of the PDF page.
34+
35+
```csharp
36+
float offsetX = textFragment.Position.Matrix.OffsetX;
37+
float offsetY = textFragment.Position.Matrix.OffsetY;
38+
```
39+
40+
2. **Calculate the Size (Width and Height):**
41+
42+
To determine the width and height of a `TextFragment`, leverage the measuring functionality of the `Block` object. First, insert the `TextFragment` into a `Block`, and then use the `Measure` method to find its size.
43+
44+
```csharp
45+
private static Size GetFragmentSize(TextFragment textFragment)
46+
{
47+
Block block = new Block();
48+
block.Insert(textFragment);
49+
Size textFragmentSize = block.Measure();
50+
return textFragmentSize;
51+
}
52+
```
53+
54+
By following these steps, you can accurately locate a `TextFragment` within a PDF document and determine its size, enabling enhanced document processing and manipulation capabilities.
55+
56+
## Notes
57+
58+
- The `OffsetX` and `OffsetY` values pinpoint the location relative to the top-left corner of the PDF page.
59+
- The `Measure` method of the `Block` object provides the width and height of the contained `TextFragment`, facilitating precise layout and positioning operations.
60+
61+
## See Also
62+
63+
- [Position Concept in RadPdfProcessing](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/concepts/position)
64+
- [TextFragment in RadPdfProcessing](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/model/textfragment)
65+
- [Extracting Text Within a Specific Rectangle in PDF Documents](https://docs.telerik.com/devtools/document-processing/knowledge-base/extract-text-specific-rectangle-pdf-radpdfprocessing)

0 commit comments

Comments
 (0)