title | description | type | page_title | slug | tags | res_type | ticketid |
---|---|---|---|---|---|---|---|
Getting Position and Size of TextFragment in PDF Documents |
Learn how to retrieve the (x,y) position and dimensions of a TextFragment within a PDF document using RadPdfProcessing. |
how-to |
How to Determine TextFragment Position and Size in PDFs with RadPdfProcessing |
get-textfragment-position-size-radpdfprocessing |
radpdfprocessing, textfragment, position, size, document processing |
kb |
1662399 |
Version | Product | Author |
---|---|---|
2024.3.806 | RadPdfProcessing | Desislava Yordanova |
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
.
This KB article also answers the following questions:
- How can I find the exact location of a text in a PDF document?
- What is the method to determine the dimensions of a text segment within a PDF file?
- How to use the
MatrixPosition
for locating text in a PDF document?
To obtain the (x,y) coordinates and the dimensions (width and height) of a TextFragment
, follow these steps:
-
Extract the (x,y) Coordinates:
Each
TextFragment
has aPosition
property of type MatrixPosition. TheMatrixPosition
object includes aMatrix
that exposesOffsetX
andOffsetY
properties. These properties represent the fragment's offset from the top-left corner of the PDF page.float offsetX = textFragment.Position.Matrix.OffsetX; float offsetY = textFragment.Position.Matrix.OffsetY;
-
Calculate the Size (Width and Height):
To determine the width and height of a
TextFragment
, leverage the measuring functionality of theBlock
object. First, insert theTextFragment
into aBlock
, and then use theMeasure
method to find its size.private static Size GetFragmentSize(TextFragment textFragment) { Block block = new Block(); block.Insert(textFragment); Size textFragmentSize = block.Measure(); return textFragmentSize; }
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.
- The
OffsetX
andOffsetY
values pinpoint the location relative to the top-left corner of the PDF page. - The
Measure
method of theBlock
object provides the width and height of the containedTextFragment
, facilitating precise layout and positioning operations.
- [Position Concept in RadPdfProcessing]({%slug radpdfprocessing-concepts-position%})
- [TextFragment in RadPdfProcessing]({%slug radpdfprocessing-model-textfragment%})
- [Extracting Text Within a Specific Rectangle in PDF Documents]({%slug extract-text-specific-rectangle-pdf-radpdfprocessing%})