|
| 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 | + |
| 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%}) |
0 commit comments