Skip to content

Commit 6aafb78

Browse files
github-actions[bot]KB Botdessyordanova
authored
Added new kb article aligning-centered-right-margin-text-pdf-telerik-document-processing (#659)
* Added new kb article aligning-centered-right-margin-text-pdf-telerik-document-processing * polished --------- Co-authored-by: KB Bot <[email protected]> Co-authored-by: Desislava Yordanova <[email protected]>
1 parent e7f89e9 commit 6aafb78

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: Positioning Centered and Right-Aligned Text on the Same Line in PDF
3+
description: Learn how to position centered text and right-aligned text on the same line in a PDF using Telerik Document Processing (RadPdfProcessing).
4+
type: how-to
5+
page_title: Aligning Centered and Right-Margin Text in PDF Using Telerik Document Processing
6+
meta_title: Aligning Centered and Right-Margin Text in PDF Using Telerik Document Processing
7+
slug: aligning-centered-right-margin-text-pdf-telerik-document-processing
8+
tags: pdf, processing,document, position, text, center, right, align, block, measure
9+
res_type: kb
10+
ticketid: 1701532
11+
---
12+
<style>
13+
img[alt$="><"] {
14+
border: 1px solid lightgrey;
15+
}
16+
</style>
17+
18+
## Environment
19+
20+
| Version | Product | Author |
21+
| ---- | ---- | ---- |
22+
| 2025.3.806| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
23+
24+
## Description
25+
26+
Learn how to generate a PDF document with centered text and right-aligned text on the same line. The centered text varies in length, as does the text in the right margin. The goal is to manually position each text block as there is no built-in feature for this layout.
27+
28+
This knowledge base article also shows how to:
29+
* Align text to the center and right margin on the same line in a PDF
30+
* Calculate positions for text blocks in RadPdfProcessing
31+
* Measure text width and adjust its position in the PDF
32+
33+
![Positioning Centered and Right-Aligned Text ><](images/aligning-centered-right-margin-text-pdf-telerik-document-processing.png)
34+
35+
## Solution
36+
37+
To position centered and right-aligned text on the same line, follow these steps:
38+
39+
1. Measure Text Widths: Use the `Block.Measure()` method to determine the width of both the centered text and the right-margin text. Refer to [Measuring Block Size]({%slug radpdfprocessing-editing-block%}#measuring-block-size) for details.
40+
41+
2. Calculate Positions:
42+
- For centered text, calculate the X position by subtracting the text width from the page width and dividing by two.
43+
- For right-margin text, set the X position close to the right edge by subtracting the text width and any desired margin.
44+
45+
3. Draw Blocks Separately:
46+
- Use `FixedContentEditor.Position.Translate(x, y)` to move to the calculated positions.
47+
- Draw each block using individual `Block` objects.
48+
49+
Here is an example:
50+
51+
```csharp
52+
RadFixedDocument document = new RadFixedDocument();
53+
RadFixedPage page = document.Pages.AddPage();
54+
FixedContentEditor editor = new FixedContentEditor(page);
55+
56+
string centeredText = "This is Centered text";
57+
string rightMarginText = "Right";
58+
59+
Block centerBlock = new Block();
60+
centerBlock.InsertText(centeredText);
61+
Telerik.Documents.Primitives.Size centerSize = centerBlock.Measure();
62+
63+
Block rightBlock = new Block();
64+
rightBlock.InsertText(rightMarginText);
65+
Telerik.Documents.Primitives.Size rightSize = rightBlock.Measure();
66+
67+
double pageWidth = page.Size.Width;
68+
double yPosition = 100; // Example Y position
69+
70+
// Centered text
71+
double centerX = (pageWidth - centerSize.Width) / 2;
72+
editor.Position.Translate(centerX, yPosition);
73+
editor.DrawBlock(centerBlock);
74+
75+
// Right margin text
76+
double rightX = pageWidth - rightSize.Width - 20; // 20 for right margin
77+
editor.Position.Translate(rightX, yPosition);
78+
editor.DrawBlock(rightBlock);
79+
80+
// Save the document
81+
var pdfFormatProvider = new PdfFormatProvider();
82+
string outputFilePath = "StyledDocument.pdf";
83+
using (var output = File.Create(outputFilePath))
84+
{
85+
pdfFormatProvider.Export(document, output, TimeSpan.FromHours(10));
86+
}
87+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
88+
```
89+
90+
## See Also
91+
92+
- [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%})
93+
- [Block Element]({%slug radpdfprocessing-editing-block%})
5.49 KB
Loading

libraries/radpdfprocessing/editing/block.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,4 @@ The code in __Example 9__ splits a block in two. The first will contains text "H
223223
* [How to Measure Text in WordsProcessing .NET Standard]({%slug wordsprocessing-measure-text-netstandard%})
224224
* [How to Generate a PDF Document from Images with FixedContentEditor]({%slug pdf-from-images-with-fixedcontenteditor%})
225225
* [How to Change Text Color Using PdfProcessing]({%slug pdfprocessing-text-color%})
226+
* [Positioning Centered and Right-Aligned Text on the Same Line in PDF]({%slug aligning-centered-right-margin-text-pdf-telerik-document-processing%})

libraries/radpdfprocessing/editing/fixedcontenteditor.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,4 @@ __FixedContentEditor__ has some properties and methods that affect how it will b
238238
* [Resizing Large Images to Fit in the PDF Page]({%slug resize-images-radpdfprocessing%})
239239
* [Inserting HTML Content into PDF TableCell with RadPdfProcessing]({%slug insert-html-content-into-pdf-tablecell-radpdfprocessing%})
240240
* [Drawing Rectangles with Text and Image Contant with RadPdfProcessing]({%slug draw-rectangles-text-images-radpdfprocessing%})
241+
* [Positioning Centered and Right-Aligned Text on the Same Line in PDF]({%slug aligning-centered-right-margin-text-pdf-telerik-document-processing%})

0 commit comments

Comments
 (0)