# Extracting Text Within a Specific Rectangle in PDF Documents

## Environment

| Version    | Product          | Author                                                                          |
| ---------- | ---------------- | ------------------------------------------------------------------------------- |
| 2024.2.426 | RadPdfProcessing | [Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova) |

## Description

Learn how to extract the text from specific rectangular areas within PDF pages.

## Solution

To extract text from a specific rectangle or crop box within a PDF page, use the [TextFragment](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/textfragment) class together with its [MatrixPosition](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/position) property. The following example loads a PDF document, defines a rectangle for the target area, and iterates through the text fragments on each page. If the position of a text fragment falls within the specified rectangle, the code outputs the text.

```csharp
        static void Main(string[] args)
        {
            string originalFilePath = @"WinForms PdfViewer.pdf";
            PdfFormatProvider provider = new PdfFormatProvider();
            RadFixedDocument croppedDocument = provider.Import(File.ReadAllBytes(originalFilePath));
            Rect middleRectangle = new Rect(croppedDocument.Pages.First().Size.Width/2, croppedDocument.Pages.First().Size.Height / 3, croppedDocument.Pages.First().Size.Width, croppedDocument.Pages.First().Size.Height / 3);

            foreach (RadFixedPage currentPage in croppedDocument.Pages)
            {
                foreach (var contentElement in currentPage.Content)
                {
                    TextFragment textFragment = contentElement as TextFragment;

                    if (textFragment != null)
                    {
                        string currentText = (contentElement as TextFragment).Text;
                        if (currentText==" ")
                        {
                            continue;
                        }
                        MatrixPosition position = textFragment.Position as MatrixPosition;
                        if (middleRectangle.Contains(position.Matrix.OffsetX, position.Matrix.OffsetY))
                        {
                            Debug.Write(currentText);
                        }
                }
            }
        }
```

The following screenshot shows the cropped middle part of the page:

![Rectangle with text in PdfProcessing](https://www.telerik.com/document-processing-libraries/documentation/assets/82f1756c0376d1ca2a7610e7346dd403/radpdfprocessing-text-rectangle.png)

The Output console displays the detected text:

![Extracted text in PdfProcessing](https://www.telerik.com/document-processing-libraries/documentation/assets/05f1b476595a5f6133727e7b80219ce2/radpdfprocessing-extracted-text.png)

## See Also

* [RadPdfProcessing Documentation](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/overview)
* [TextFragment](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/textfragment)
* [MatrixPosition](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/position)
