diff --git a/knowledge-base/aligning-centered-right-margin-text-pdf-telerik-document-processing.md b/knowledge-base/aligning-centered-right-margin-text-pdf-telerik-document-processing.md new file mode 100644 index 00000000..ade5c2ff --- /dev/null +++ b/knowledge-base/aligning-centered-right-margin-text-pdf-telerik-document-processing.md @@ -0,0 +1,93 @@ +--- +title: Positioning Centered and Right-Aligned Text on the Same Line in PDF +description: Learn how to position centered text and right-aligned text on the same line in a PDF using Telerik Document Processing (RadPdfProcessing). +type: how-to +page_title: Aligning Centered and Right-Margin Text in PDF Using Telerik Document Processing +meta_title: Aligning Centered and Right-Margin Text in PDF Using Telerik Document Processing +slug: aligning-centered-right-margin-text-pdf-telerik-document-processing +tags: pdf, processing,document, position, text, center, right, align, block, measure +res_type: kb +ticketid: 1701532 +--- + + +## Environment + +| Version | Product | Author | +| ---- | ---- | ---- | +| 2025.3.806| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| + +## Description + +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. + +This knowledge base article also shows how to: +* Align text to the center and right margin on the same line in a PDF +* Calculate positions for text blocks in RadPdfProcessing +* Measure text width and adjust its position in the PDF + +![Positioning Centered and Right-Aligned Text ><](images/aligning-centered-right-margin-text-pdf-telerik-document-processing.png) + +## Solution + +To position centered and right-aligned text on the same line, follow these steps: + +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. + +2. Calculate Positions: + - For centered text, calculate the X position by subtracting the text width from the page width and dividing by two. + - For right-margin text, set the X position close to the right edge by subtracting the text width and any desired margin. + +3. Draw Blocks Separately: + - Use `FixedContentEditor.Position.Translate(x, y)` to move to the calculated positions. + - Draw each block using individual `Block` objects. + +Here is an example: + +```csharp +RadFixedDocument document = new RadFixedDocument(); +RadFixedPage page = document.Pages.AddPage(); +FixedContentEditor editor = new FixedContentEditor(page); + +string centeredText = "This is Centered text"; +string rightMarginText = "Right"; + +Block centerBlock = new Block(); +centerBlock.InsertText(centeredText); +Telerik.Documents.Primitives.Size centerSize = centerBlock.Measure(); + +Block rightBlock = new Block(); +rightBlock.InsertText(rightMarginText); +Telerik.Documents.Primitives.Size rightSize = rightBlock.Measure(); + +double pageWidth = page.Size.Width; +double yPosition = 100; // Example Y position + +// Centered text +double centerX = (pageWidth - centerSize.Width) / 2; +editor.Position.Translate(centerX, yPosition); +editor.DrawBlock(centerBlock); + +// Right margin text +double rightX = pageWidth - rightSize.Width - 20; // 20 for right margin +editor.Position.Translate(rightX, yPosition); +editor.DrawBlock(rightBlock); + +// Save the document +var pdfFormatProvider = new PdfFormatProvider(); +string outputFilePath = "StyledDocument.pdf"; +using (var output = File.Create(outputFilePath)) +{ + pdfFormatProvider.Export(document, output, TimeSpan.FromHours(10)); +} +Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); +``` + +## See Also + +- [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%}) +- [Block Element]({%slug radpdfprocessing-editing-block%}) diff --git a/knowledge-base/images/aligning-centered-right-margin-text-pdf-telerik-document-processing.png b/knowledge-base/images/aligning-centered-right-margin-text-pdf-telerik-document-processing.png new file mode 100644 index 00000000..fd6b1956 Binary files /dev/null and b/knowledge-base/images/aligning-centered-right-margin-text-pdf-telerik-document-processing.png differ diff --git a/libraries/radpdfprocessing/editing/block.md b/libraries/radpdfprocessing/editing/block.md index 5b599fe9..4a673da6 100644 --- a/libraries/radpdfprocessing/editing/block.md +++ b/libraries/radpdfprocessing/editing/block.md @@ -223,3 +223,4 @@ The code in __Example 9__ splits a block in two. The first will contains text "H * [How to Measure Text in WordsProcessing .NET Standard]({%slug wordsprocessing-measure-text-netstandard%}) * [How to Generate a PDF Document from Images with FixedContentEditor]({%slug pdf-from-images-with-fixedcontenteditor%}) * [How to Change Text Color Using PdfProcessing]({%slug pdfprocessing-text-color%}) + * [Positioning Centered and Right-Aligned Text on the Same Line in PDF]({%slug aligning-centered-right-margin-text-pdf-telerik-document-processing%}) diff --git a/libraries/radpdfprocessing/editing/fixedcontenteditor.md b/libraries/radpdfprocessing/editing/fixedcontenteditor.md index 9e739980..2bb211eb 100644 --- a/libraries/radpdfprocessing/editing/fixedcontenteditor.md +++ b/libraries/radpdfprocessing/editing/fixedcontenteditor.md @@ -238,3 +238,4 @@ __FixedContentEditor__ has some properties and methods that affect how it will b * [Resizing Large Images to Fit in the PDF Page]({%slug resize-images-radpdfprocessing%}) * [Inserting HTML Content into PDF TableCell with RadPdfProcessing]({%slug insert-html-content-into-pdf-tablecell-radpdfprocessing%}) * [Drawing Rectangles with Text and Image Contant with RadPdfProcessing]({%slug draw-rectangles-text-images-radpdfprocessing%}) +* [Positioning Centered and Right-Aligned Text on the Same Line in PDF]({%slug aligning-centered-right-margin-text-pdf-telerik-document-processing%})