Text Position Within Shape

I’m trying to position text within a rectangle and am unsuccessful. I’ve included an example program below. Please let me know what I’m doing wrong. Thank you for your time.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Rectangle = Aspose.Pdf.Drawing.Rectangle;

namespace ConsoleApp2
    class Program
        static void Main(string[] args)
            var doc = new Document();
            var page = doc.Pages.Add();
            var canvas = new Graph(100, 400);

            var textRect = new Aspose.Pdf.Drawing.Rectangle(30, 75, 150, 50);
            textRect.GraphInfo.Color = Aspose.Pdf.Color.BlanchedAlmond;
            textRect.GraphInfo.DashArray = new int[] { 1 };

            var tf = new TextFragment("Test It!")
                Position = new Position(30, 30),
                BaselinePosition = new Position(70, 70),
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Bottom

            tf.TextState.Font = FontRepository.FindFont("Whitney Book");
            tf.TextState.FontSize = 12;
            tf.TextState.BackgroundColor = Aspose.Pdf.Color.FloralWhite;
            tf.TextState.ForegroundColor = Aspose.Pdf.Color.DarkBlue;
            tf.TextState.FontStyle = FontStyles.Regular;
            tf.TextState.StrikeOut = false;
            tf.TextState.Underline = false;
            tf.TextState.DrawTextRectangleBorder = true;
            textRect.Text = tf;



Thank you for contacting support.

You may add a FloatingBox on a PDF page and then add text in it as per your requirements. Please try the code snippet suggested below, in your environment and then share your kind feedback with us.

    Document doc = new Document();
    Aspose.Pdf.Page page = doc.Pages.Add();
    page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

    const double width = 222;
    const double height = 333;

    var box = new FloatingBox((float)width, (float)height)
        Left = 0f,
        Top = 0f,
        ZIndex = 345,
        Margin = new MarginInfo(0, 0, 0, 0),
        Padding = new MarginInfo(0, 0, 0, 0),
        IsInLineParagraph = false,
        IsInNewPage = false,
        IsNeedRepeating = false,
        Border = new BorderInfo(BorderSide.None),
        HorizontalAlignment = HorizontalAlignment.Left,
        BackgroundColor = Aspose.Pdf.Color.Tomato,
        VerticalAlignment = VerticalAlignment.Top

    //Add the Text into paragraphs collection of FloatingBox
    box.Paragraphs.Add(new TextFragment("Sample Text added in FloatingBox") { HorizontalAlignment = HorizontalAlignment.Center, Margin = new MarginInfo(36, 0, 36, 0) });
    doc.Save(path, SaveFormat.Pdf);

We hope this will be helpful. Please feel free to contact us if you need any further assistance.


Thanks for your response, but I need the ZIndex functionality of the canvas object. Am I to understand that the text positioning for shapes doesn’t work?



We are afraid text positioning may not be supported for text shapes, therefore an enhancement ticket with ID PDFNET-45413 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

Moreover, FloatingBox class also exposes ZIndex property which may be used as per your requirements.

We are sorry for the inconvenience.