Indent for text with background color


That should be an extremely easy question, however I’m stuck with it.

There’s a possibility to make a colored background for a text object, but by default the text looks sticky to the background top. Is there a way to create some sort of indent for text?

Hi Anton,

Thanks for contacting support.

In case you are creating PDF file using Aspose.Pdf.Generator namespace, you can use FloatingBox object to place text on top of background image as watermark and also you can use ZIndex property of FloatingBox class to specify the Z-Order of objects. You may also use FloatingBox to set image as background color for text and set the Z-Order for text object. A floating box with larger ZIndex will be placed over the floating box with
smaller ZIndex. ZIndex can be negative. Floating box with negative ZIndex will
be placed behind the text in the page.

In case I have not properly understood your requirement, please share some further details.

I’m afraid, that’s not an option. I’ve fixed some of background color problems using watermarks already, however that’s a harsh solution.

Imagine, that I have several programmatically generated tables in document. Each table column has a header cell, that has its own color. Now I must calculate the coordinates of each cell, and add a colored square/text there as a watermark. Theoretically, that can be solved, but too severe… And the calculations will be very interesting, as the tables doesn’t have a fixed height, as the number of elements can be different.

So, I’m asking, if there’s an easier way?

Hi Anton,

Thanks for sharing the details.

As per my understanding, when you are creating PDF file from scratch, you can specify the background color for Text as well as Table and other objects. However when you are trying to add background to contents of an existing PDF file, you need to parse the contents of file so that you can get the information related to their coordinates.

Nevertheless, can you please share some sample PDF file or image file highlighting the issue which you are facing, as it will help us in understanding the requirement in more appropriate manner. We apologize for the delay and inconvenience.

All right, here's the difference with test project.

Ms word document shows, how the doc should look like.

Pdf docs are my attempts with Aspose.Pdf for MVC4.

First, I've used the lineSpacing property to prevent the header text to stick to the top of background.

(textHeader.TextInfo = new TextInfo() { FontSize = 11, LineSpacing = lineSpacing, FontName = usedFontName, BackgroundColor = new Color("Yellow") };)

However, I can't see, it working with table cells(table.DefaultCellTextInfo.LineSpacing = lineSpacing;). As you can see in pdf, the text in header cells looks sticked to the cell.

I also tried to use DefaultCellPadding for table however that also looks ugly (second pdf file).

So, how can I achieve the table appearance similar to msword document?

Hi Anton,

Thanks for sharing the sample documents.

I have tested the scenario using following code snippet where I have used the Document Object Model of Aspose.Pdf namespace and I am unable to notice any issue. The output PDF is identical to word document which you have shared.

For your reference, I have also attached the PDF document generated with Aspose.Pdf for .NET 8.5.0.


Document doc = new

// add page to PDF document


// add text fragment as heading

Aspose.Pdf.Text.TextFragment top_heading = new TextFragment("TOP SECRET DOCUMENT HEADER");

top_heading.TextState.BackgroundColor = Aspose.Pdf.Color.Yellow;

top_heading.Margin.Bottom = 30;

// add text fragment to document


// add text fragment as heading

Aspose.Pdf.Text.TextFragment test_heading = new TextFragment("This document contains top secret information about something.");

test_heading.Margin.Bottom = 30;

// add text fragment to document


// add text fragment as heading

Aspose.Pdf.Text.TextFragment table_heading = new TextFragment("Table");

table_heading.TextState.ForegroundColor = Aspose.Pdf.Color.Yellow;

table_heading.TextState.BackgroundColor = Aspose.Pdf.Color.Green;

table_heading.Margin.Bottom = 30;

// add text fragment to document


Aspose.Pdf.Table table = new Aspose.Pdf.Table();

table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5f, Aspose.Pdf.Color.Black);

table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5f, Aspose.Pdf.Color.Black);

Aspose.Pdf.Row HeaderRow = new Aspose.Pdf.Row();

HeaderRow.FixedRowHeight = 15;

// default padding information for header row contents

HeaderRow.DefaultCellPadding = new Aspose.Pdf.MarginInfo(5, 0, 0, 0);

// add row to table object


// add a blank cell in header row

HeaderRow.Cells.Add(new Aspose.Pdf.Cell());

// add a new cell to HeaderRow and set background color to red

HeaderRow.Cells.Add(new Aspose.Pdf.Cell() { BackgroundColor = Aspose.Pdf.Color.Red });

// contents of second cell in header row

HeaderRow.Cells[1].Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Header1"));

// add a new cell to HeaderRow and set background color to Blue

HeaderRow.Cells.Add(new Aspose.Pdf.Cell() { BackgroundColor = Aspose.Pdf.Color.DodgerBlue });

// contents of second cell in header row

HeaderRow.Cells[2].Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Header2"));

// add a new cell to HeaderRow and set background color to red

HeaderRow.Cells.Add(new Aspose.Pdf.Cell() { BackgroundColor = Aspose.Pdf.Color.Yellow });

// contents of second cell in header row

HeaderRow.Cells[3].Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Header3"));

// add few row to table object

for(int row_counter =1; row_counter<=6;row_counter++)


Aspose.Pdf.Row sample_row = new Aspose.Pdf.Row();

sample_row.FixedRowHeight = 15;

sample_row.DefaultCellPadding = new Aspose.Pdf.MarginInfo(5, 0,0,0);

sample_row.Cells.Add("Row" + row_counter);

sample_row.Cells.Add("" + row_counter + "," + row_counter * 2);

sample_row.Cells.Add("" + row_counter + "," + row_counter * 3);

sample_row.Cells.Add("" + row_counter + "," + row_counter * 4);



// add table to PDF file


//save the PDF file


Interesting. Seems that Aspose.Pdf* and Aspose.Pdf.Generator* namespace objects have a bit different behavior.

I've tested your code, it worked well. Thanks.

Hi Anton,

Aspose.Pdf namespace is new DOM approach and it has better results as compare to Aspose.Pdf.Generator. Therefore we recommend you to please try using Aspose.Pdf namespace as all the fixes and enhancements are being introduced in this namespace.

In case I can be of any further assistance, please feel free to contact.