New to Telerik Document Processing? Download free 30-day trial

Positioning Centered and Right-Aligned Text on the Same Line in PDF

Environment

Version Product Author
2025.3.806 RadPdfProcessing 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 ><

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 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:

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

In this article