Background color on paragraph does not fill bounding rectangle

I am trying to add a new text area to an existing Pdf document and give it a background color on the text. The source text comes to me with line breaks that will be dynamically created. After reading thru the forums I have determined that to respect the line breaks I need to replace \n with Environment.Newline and this seems to work great. The problem that I have is that the background color only appears where there is text and not in the containing rectangle as desired.

I am using aspose.total.net (pdf version 18.1.0). Below is an example of the code

const string rawText = "This is a test\nThis\nis a\ntest\nThis is a";
var doc = new Document("Test.pdf"); //full filepath to document here
const double x = 350;
const double y = 575;
const double width = 300;
const double height = 150;
var page = doc.Pages[1];

//replace line breaks
var text = rawText.Replace("\n", Environment.NewLine);
var textFragment = new TextFragment(text);
textFragment.TextState.FontSize = 30;
textFragment.TextState.BackgroundColor = Color.FromRgb(System.Drawing.ColorTranslator.FromHtml("#426bf4"));

var rectangle = new Rectangle(x, y, x + width, y + height);
var paragraph = new TextParagraph
{
	Rectangle = rectangle,

	FormattingOptions = {WrapMode = TextFormattingOptions.WordWrapMode.ByWords},
	HorizontalAlignment = global::Aspose.Pdf.HorizontalAlignment.Left,
	VerticalAlignment = VerticalAlignment.Top
};
paragraph.AppendLine(textFragment);
var textBuilder = new TextBuilder(page);
textBuilder.AppendParagraph(paragraph);

doc.Save("result.pdf");

This is the result.
result.pdf (95.4 KB)

I can almost get what I am after if I add a bunch of trailing spaces to the string before adding the line breaks, but the rectangle is at best very ragged on the end.
Ex:
If I change the above code to use this string

const string rawText = "This is a test               \nThis                             \nis a                              \ntest                              \nThis is a                       ";

This is the output
result-adding-trailing-spaces.pdf (95.5 KB)

What I really want is something like this
desired.PNG (159.3 KB)

Not sure how to proceed. I have also attempted to draw a canvas with a rectangle and and then add text on top of it, but the coordinate system on drawing seems to be very different and the z-index does not seem to be respected when mixing a canvas draw rectangle and trying to add a text segment on top.

@cyrus10101

Thank you for contacting support.

We have worked over your requirements and would like to request you to use below code snippet in your environment and then share your kind feedback with us.

        const double x = 100;
        const double y = 50;
        const float width = 300;
        const float height = 150;
        Document document = new Aspose.Pdf.Document();
        Page page = document.Pages.Add();
        FloatingBox box = new  Aspose.Pdf.FloatingBox();
        box.Top = x;
        box.Left = y;
        box.Height = height;
        box.Width = width;
        box.BackgroundColor = Color.FromRgb(System.Drawing.ColorTranslator.FromHtml("#426bf4"));
        box.HorizontalAlignment = HorizontalAlignment.Left;
        box.VerticalAlignment = VerticalAlignment.Top;
        box.Border = new BorderInfo(BorderSide.None);
        TextFragment fragment = new Aspose.Pdf.Text.TextFragment("This is a test\r\nThis\r\nis a\r\ntest\r\nThis is a");
        fragment.TextState.FontSize = 30;
        box.Paragraphs.Add(fragment);
        page.Paragraphs.Add(box);
        document.Save(dataDir + "Test_18.3.pdf");

This code snippet adds a FloatingBox on a page of PDF document, sets several properties and finally adds the text on that FloatingBox. You can modify the position, height, width or any other property as per your requirements.

We hope this will be helpful. Please let us know if you need any further assistance.

I have updated my code to use the floating box as you suggested

const string rawText = “This is a test\nThis\nis a\ntest\nThis is a”;
var doc = new Document(“Test.pdf”); //full filepath to document here
const double x = 350;
const double y = 575;
const double width = 300;
const double height = 150;
var page = doc.Pages[1];

//replace line breaks
var text = rawText.Replace("\n", Environment.NewLine);
var box = new FloatingBox
{
	Top = x,
	Left = y,
	Height = height,
	Width = width,
	BackgroundColor = Color.FromRgb(System.Drawing.ColorTranslator.FromHtml("#426bf4")),
	HorizontalAlignment = global::Aspose.Pdf.HorizontalAlignment.Left,
	VerticalAlignment = VerticalAlignment.Top,
	Border = new BorderInfo(BorderSide.None)
};
var fragment = new TextFragment(text);
fragment.TextState.FontSize = 30;
box.Paragraphs.Add(fragment);
page.Paragraphs.Add(box);

This is the result
floatingbox.pdf (96.2 KB)

The desired affect is working as intended but, as you can see from the attachment, the location is very different from the previous when using the same coordinates. I know pdfs use a x,y coordinate system starting from the lower left corner of the page and am unsure how the floating box coordinates work. The width and height look to be correct but the x,y are way off.

I would expect the Left and x to be equivalent and then the pageheight - (Y + Height) to be equivalent to Top but this is not the case at all. I modified the code to use this assumption and the resulting pdf looks like this

floating-adjusted-top.pdf (96.2 KB)

This seems to put it a bit too high and too far to the right in comparison to the location specified in the pdf coordinates. Please advise. Thanks.

@cyrus10101

We are investigating it in our environment and will get back to you with our findings, soon.

@cyrus10101

Thank you for your kind feedback.

We are glad to know that your requirements have been fulfilled by suggested solution. Please refer to attached screenshot to understand FloatingBox.Top and FloatingBox.Left property. Floating Box Explained.jpg. Unlike the PDF page coordinate system, the FloatingBox’s (0,0) means Top Left i.e. zero points away from Top and zero points away from Left - provided the page margins are set to zero, otherwise the value of page margin is added to zero by default.

Please note that the basic measuring unit in Aspose.PDF for .NET is point, where 1 inch = 72 points and 1 cm = 1/2.54 inch = 0.3937 inch = 28.3 points.

I hope this will clarify any ambiguity. Please let us know if you need any further assistance.

Based on your explanation I was able to achieve the correct coordinates using these modifications.

Top = page.Rect.Height - (y + page.PageInfo.Margin.Bottom + height),
Left = x - page.PageInfo.Margin.Left,

It would be very useful if there was a utility function that would do this translation from one coordinate system to the other so that the end user only has to measure everything using one standard.

@cyrus10101

We appreciate your worthy feedback.

We are pleased to know that you have been able to achieve correct coordinates with suggested approach. Moreover, a feature request with ID PDFNET-44374 has been logged in our issue management system. We will further investigate if we can add Position(X Coordinate, Y Coordinate) property to FloatingBox class. The issue ID has been linked with this thread so that you will be notified as soon as some significant progress is made in this regard.

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan