# TextFragment

The `TextFragment` class in [RadPdfProcessing](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/overview) represents a single-line text object that you can add to a PDF page.

* [Creating a TextFragment](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/textfragment#creating-a-textfragment)
* [Modifying a TextFragment](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/textfragment#modifying-a-textfragment)

## Public API

| **Property**            | **Description**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **CharacterSpacing**    | The spacing between the characters in the text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| **WordSpacing**         | The spacing between the words in the text. (*Only space character (Unicode 0x32) is considered a word break in the RadPdfProcessing document model.*)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| **HorizontalScaling**   | The horizontal scaling that is applied to the characters.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| **Font**                | The font that is used to draw the text.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| **FontSize**            | The font size. The measurement unit used for font size is [Device Independent Pixels](https://www.telerik.com/document-processing-libraries/documentation/common-information/device-independent-pixels) (DIPs). You can convert it to points or other units with the [Unit](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Media.Unit.html) class.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| **RenderingMode**       | Enumeration representing the way the text is rendered. It can have one of the following values: <table><tr><th scope="row">**Fill**</th><td>Fill text (the default value).</td></tr><tr><th scope="row">**Stroke**</th><td>Stroke text.</td></tr><tr><th scope="row">**FillAndStroke**</th><td>Fill, then stroke text.</td></tr><tr><th scope="row">**None**</th><td>Neither fill nor stroke text (invisible).</td></tr><tr><th scope="row">**FillAndAddToClippingPath**</th><td>Fill text and add to path for clipping.</td></tr><tr><th scope="row">**StrokeAndAddToClippingPath**</th><td>Stroke text and add to path for clipping.</td></tr><tr><th scope="row">**FillStrokeAndAddToClippingPath**</th><td>Fill, then stroke text and add to path for clipping.</td></tr><tr><th scope="row">**AddToClippingPath**</th><td>Add text to path for clipping.</td></tr></table> |
| **TextRise**            | Specifies the distance, in unscaled text space units, to move the baseline up or down from its default location.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| **Fill**                | The color that is used to fill the text. The default value is Black.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| **Stroke**              | The color that is used to stroke text. The default value is Black.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| **StrokeThickness**     | The width of the stroke line.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| **StrokeLineCap**       | Specifies the shape used at the ends of open paths when they are stroked. It can have one of the following values:<table><tr><th scope="row">**Flat**</th><td>Flat line cap.</td></tr><tr><th scope="row">**Round**</th><td>Round line cap.</td></tr><tr><th scope="row">**Square**</th><td>Square line cap.</td></tr></table>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| **StrokeLineJoin**      | Specifies the shape to be used at the corners of paths that are stroked. Join styles are significant only at the points where consecutive segments of a path connect at an angle. Available options: <table><tr><th scope="row">**Bevel**</th><td>Produces a diagonal corner.</td></tr><tr><th scope="row">**Miter**</th><td>Produces a sharp corner. If the segments meet at too sharp an angle, a bevel join is used instead.</td></tr><tr><th scope="row">**Round**</th><td>Produces a smooth, circular arc between the lines.</td></tr></table>                                                                                                                                                                                                                                                                                                                             |
| **StrokeDashArray**     | The pattern of dashes and gaps used to stroke paths.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| **StrokeDashOffset**    | The distance from the start of a line to the beginning of a dash pattern.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| **AlphaConstant**       | Specifies the constant shape or constant opacity value to be used for nonstroking operations.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| **StrokeAlphaConstant** | Specifies the constant shape or constant opacity value to be used for stroking operations.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| **MiterLimit**          | The limit of the thickness of the join on a mitered corner.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| **Text**                | Represents the text that is drawn.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| **Position**            | The [Position](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/position) where the text element is drawn.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |

| **Method**                          | **Description**                               |
| ----------------------------------- | --------------------------------------------- |
| **Clone** (*Introduced in Q2 2025*) | Creates a deep copy of this document element. |

> note To use a font that is not part of the [standard fonts](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/fonts#standard-fonts), register it with the [RegisterFont()](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/fonts#registering-a-font) method of the `FontsRepository` static class.

> important In **.NET Standard/.NET (Target OS: None)** environments, fonts beyond the [14 standard ones](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/fonts#standard-fonts) require a [FontsProvider implementation](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/pdfprocessing-implement-fontsprovider) to be resolved correctly.

### Creating a TextFragment

`TextFragment` is a content element that you can add to the `Content` collection of an `IContainerElement` such as [RadFixedPage](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/radfixedpage). There are several approaches you can use:

* Create a `TextFragment` and add it to a page container.
* Use one of the factory methods of the `ContentElementCollection` to create a new text fragment and insert it into the respective container.

Both methods return the actual `TextFragment` instance so you can modify it.

**Create TextFragments and Add Them to a Page**

```C#
RadFixedDocument document = new RadFixedDocument();
RadFixedPage page = new RadFixedPage();
document.Pages.Add(page);
FixedContentEditor editor = new FixedContentEditor(page);

SimplePosition simplePosition = new SimplePosition();
simplePosition.Translate(20, 20);
TextFragment textFragment = page.Content.AddTextFragment("Document Processing Libraries");
textFragment.CharacterSpacing = 5;
textFragment.WordSpacing = 15;
textFragment.Position = simplePosition;

SimplePosition simplePosition2 = new SimplePosition();
simplePosition2.Translate(20, 120);
TextFragment textFragment2 = new TextFragment("Document Processing Libraries");
textFragment2.CharacterSpacing = 10;
textFragment2.WordSpacing = 20;
textFragment2.Position = simplePosition2;
page.Content.Add(textFragment2);
```

**Inserted TextFragments**

![RadPdfProcessing Inserted TextFragments](https://www.telerik.com/document-processing-libraries/documentation/assets/49faf798ed6be7b3cb08fad9ff6c2d47/radpdfprocessing-model-textfragment.png)

> tip `TextFragment` represents a single line of text. To make your text flow in a document, verify that all fragments you add can fit in a line, or use [FixedContentEditor](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/editing/fixedcontenteditor).

> The `\r` and `\n` characters do not have the usual meaning of *"go to next line"* when inserted into a PDF document. You cannot insert text that contains these characters and produce multiline text. Instead, split the text and insert it line by line. An alternative approach is to use the [RadFixedDocumentEditor](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/editing/radfixeddocumenteditor) which allows you to create a document in a flow-like manner.

### Modifying a TextFragment

Modify a `TextFragment` element with the properties listed in the [Public API](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/textfragment#public-api) section.

**Modify TextFragment Properties**

```C#
RadFixedDocument document = new RadFixedDocument();
RadFixedPage page = new RadFixedPage();
document.Pages.Add(page);
FixedContentEditor editor = new FixedContentEditor(page);

SimplePosition simplePosition = new SimplePosition();
simplePosition.Translate(20, 20);
TextFragment textFragment = page.Content.AddTextFragment("Document");
textFragment.RenderingMode = RenderingMode.Stroke;
textFragment.Stroke = new RgbColor(255, 0, 255);
textFragment.CharacterSpacing = 5;
textFragment.WordSpacing = 15;

// Read the font file 
byte[] fontData = File.ReadAllBytes(@"C:\Windows\Fonts\arial.ttf");
FontFamily fontFamily = new FontFamily("Arial");

// Register the font 
FontsRepository.RegisterFont(fontFamily, FontStyles.Normal, FontWeights.Normal, fontData);
FontBase font;
bool success = FontsRepository.TryCreateFont(fontFamily, FontStyles.Normal, FontWeights.Normal, out font);
textFragment.Font = font;
textFragment.FontSize = Unit.PointToDip(12);
textFragment.Position = simplePosition;

textFragment = page.Content.AddTextFragment("Processing");
simplePosition = new SimplePosition();
simplePosition.Translate(20, 50);
textFragment.Position = simplePosition;
textFragment.CharacterSpacing = 5;
textFragment.WordSpacing = 15;
textFragment.Font = FontsRepository.Courier;
textFragment.FontSize = Unit.PointToDip(12);
textFragment.RenderingMode = RenderingMode.Stroke;
textFragment.Stroke = new RgbColor(0, 0, 255);
textFragment.StrokeThickness = 1;

simplePosition = new SimplePosition();
textFragment = page.Content.AddTextFragment("Libraries");
simplePosition.Translate(20, 80);
textFragment.Position = simplePosition;
textFragment.Font = FontsRepository.TimesItalic;
textFragment.FontSize = Unit.PointToDip(16);
textFragment.RenderingMode = RenderingMode.Stroke;
textFragment.StrokeLineCap = Telerik.Documents.Fixed.Model.Graphics.LineCap.Flat;
textFragment.StrokeLineJoin = Telerik.Documents.Fixed.Model.Graphics.LineJoin.Round;
textFragment.StrokeDashArray = new double[] { 1 };
textFragment.StrokeDashOffset = 2;
textFragment.AlphaConstant = 0.5;
textFragment.MiterLimit = 2;
```

**Modified TextFragments**

![RadPdfProcessing Modified TextFragments](https://www.telerik.com/document-processing-libraries/documentation/assets/5c5aedae44fdf727ead2a0c7e7f8c35d/radpdfprocessing-model-modified-textfragments.png)

## See Also

* [RadFixedPage](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/radfixedpage)
* [FixedContentEditor](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/editing/fixedcontenteditor)
* [Position](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/position)
* [Block](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/editing/block)
* [Extracting Text Within a Specific Rectangle in PDF Documents](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/extract-text-specific-rectangle-pdf-radpdfprocessing)
* [Getting Position and Size of TextFragment in PDF Documents](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/get-textfragment-position-size-radpdfprocessing)
* [Replacing Specific Text in PDF Spanning Multiple TextFragments in Telerik PdfProcessing](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/replace-specific-text-spanning-textfragments-pdfprocessing)
