|
| 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]({%slug radpdfprocessing-concepts-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 | + |
| 23 | + |
| 24 | +This KB article also answers the following questions: |
| 25 | +- How can I find the exact location of a text in a PDF document? |
| 26 | +- What is the method to determine the dimensions of a text segment within a PDF file? |
| 27 | +- How to use the `MatrixPosition` for locating text in a PDF document? |
| 28 | + |
| 29 | +## Solution |
| 30 | + |
| 31 | +To obtain the (x,y) coordinates and the dimensions (width and height) of a `TextFragment`, follow these steps: |
| 32 | + |
| 33 | +1. **Extract the (x,y) Coordinates:** |
| 34 | + |
| 35 | + 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. |
| 36 | + |
| 37 | + ```csharp |
| 38 | + float offsetX = textFragment.Position.Matrix.OffsetX; |
| 39 | + float offsetY = textFragment.Position.Matrix.OffsetY; |
| 40 | + ``` |
| 41 | + |
| 42 | +2. **Calculate the Size (Width and Height):** |
| 43 | + |
| 44 | + 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. |
| 45 | + |
| 46 | + ```csharp |
| 47 | + private static Size GetFragmentSize(TextFragment textFragment) |
| 48 | + { |
| 49 | + Block block = new Block(); |
| 50 | + block.Insert(textFragment); |
| 51 | + Size textFragmentSize = block.Measure(); |
| 52 | + return textFragmentSize; |
| 53 | + } |
| 54 | + ``` |
| 55 | + |
| 56 | +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. |
| 57 | + |
| 58 | +## Notes |
| 59 | + |
| 60 | +- The `OffsetX` and `OffsetY` values pinpoint the location relative to the top-left corner of the PDF page. |
| 61 | +- The `Measure` method of the `Block` object provides the width and height of the contained `TextFragment`, facilitating precise layout and positioning operations. |
| 62 | + |
| 63 | +## See Also |
| 64 | + |
| 65 | +- [Position Concept in RadPdfProcessing]({%slug radpdfprocessing-concepts-position%}) |
| 66 | +- [TextFragment in RadPdfProcessing]({%slug radpdfprocessing-model-textfragment%}) |
| 67 | +- [Extracting Text Within a Specific Rectangle in PDF Documents]({%slug extract-text-specific-rectangle-pdf-radpdfprocessing%}) |
0 commit comments