Text Fragment rendering as Aspose.Pdf.Text.TextFragment inside PDF row cells

Hi,

I have a situation where I need to add rectangular color box from hex code(for ex: #ffddee) and append before the name inside a cell as follows

var tf= new TextFragment("");
tf.TextState.BackgroundColor = Color.Yellow;
tf.Margin.Bottom = 100;//Need something for rectangle


//Generating PDF using Aspose.Pdf
var pdfDoc = new Document();
var section = pdfDoc.Pages.Add();

var table=new Table();
var row = table.Rows.Add();

var cell=row.Cells.Add(tf +" "itemName);

When I do this it shows as

Aspose.Pdf.Text.TextFragment SomeName instead of showing as [yellow_color_rect_box] SomeName

I tried using other features such as floating box and graph canvas and I am getting return type as object just like above case.

But when I add this textfragement to the page section it’s showing correctly.

My requirement is to have color for each cell in each row(dynamic coming from c#).



Hi Avinash,

Thanks for contacting support.

I have gone through your requirement and as per my understanding, you need to set foreground color for items in each cell. I am working on creating a code snippet and will get back to you soon.

I think you have confused rectangular color box with text.

Basically I want rectangular box. I tried using canvas. It’s rendering now using

rowcell. Paragraph. Add(rectangularCanvas(itemcolor) );
rowcell. Cells. Add (itemName) ;

But now Color cell is in one row and itemname moved to next row.
Color should be besides name in same row

Hi Avinash,

Please try using following code snippet. For your reference, I have also attached the output generated over my end.

In case I have not properly understood the requirement or you have any further query, please feel free to contact.

[C#]

Document pdfDocument = new Document();

Page pdfPage = pdfDocument.Pages.Add();

Table table = new Table();

table.Margin = new MarginInfo { Left = 20, Right = 20, Top = 10, Bottom = 0 };

table.Border = new BorderInfo(BorderSide.All, Aspose.Pdf.Color.LawnGreen);

table.DefaultCellBorder = new BorderInfo(BorderSide.All, Aspose.Pdf.Color.LightBlue);

pdfPage.Paragraphs.Add(table);

table.ColumnWidths = "150 150";

Row row = table.Rows.Add();

Cell cell1 = row.Cells.Add();
cell1.Paragraphs.Add(new TextFragment("yellow_color_rect_box"));
cell1.DefaultCellTextState.ForegroundColor = Aspose.Pdf.Color.Yellow;

Cell cell2 = row.Cells.Add();
cell2.Paragraphs.Add(new TextFragment("Red_color_rect_box"));
cell2.DefaultCellTextState.ForegroundColor = Aspose.Pdf.Color.Red;

pdfDocument.Save("c:/pdftest/TableOutput.pdf");

Hi,

Using text fragment was one of ways I tried. Please forget it.
I NEED small colored rectangular box (not text,just rectangular box filled with color) and name next to it in single row. I am on mobile now. Can’t add reference of image. Please do the needful. Will add some file tomorrow

Hi Avinash,

Thanks for sharing the details.

Do you think following code snippet can fulfill your requirement.

[C#]

Document pdfDocument = new Document();

Page pdfPage = pdfDocument.Pages.Add();


Table table = new Table();

table.Margin = new MarginInfo { Left = 20, Right = 20, Top = 10, Bottom = 0 };

// table.Border = new BorderInfo(BorderSide.All, Aspose.Pdf.Color.LawnGreen);

// table.DefaultCellBorder = new BorderInfo(BorderSide.All, Aspose.Pdf.Color.LightBlue);

pdfPage.Paragraphs.Add(table);

table.ColumnWidths = "100 100";


Row row = table.Rows.Add();

Cell cell1 = row.Cells.Add();

cell1.BackgroundColor = Aspose.Pdf.Color.Yellow;

Cell cell2 = row.Cells.Add();

cell2.Paragraphs.Add(new TextFragment("Yellow Label"));


Row row2 = table.Rows.Add();

Cell row2_cell1 = row2.Cells.Add();

row2_cell1.BackgroundColor = Aspose.Pdf.Color.Green;

Cell row2_cell2 = row2.Cells.Add();

row2_cell2.Paragraphs.Add(new TextFragment("Green Label"));


pdfDocument.Save("c:/pdftest/TableOutput.pdf");

No no no.

I don’t want textfragment at all.As I told I need colored rectangular box. I started with text fragment but it is not the right way for me. Tried with canvas.
Please find attatchments

Hi Avinash,

Thanks for sharing the details.

The reasons I have used TextFragment is to indicate on how to add some label with colored rectangles. However, if you do not want to render TextFragment, you can comment out the code line where TextFragment is being added to table cell.

Please take a look over following code snippet which generates exact output as specified in earlier shared image.

[C#]

Document pdfDocument = new Document();

Page pdfPage = pdfDocument.Pages.Add();

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

table.Margin = new Aspose.Pdf.MarginInfo { Left = 20, Right = 20, Top = 10, Bottom = 0 };

// table.Border = new BorderInfo(BorderSide.All, Aspose.Pdf.Color.LawnGreen);

// table.DefaultCellBorder = new BorderInfo(BorderSide.All, Aspose.Pdf.Color.LightBlue);

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

pdfPage.Paragraphs.Add(table);

table.ColumnWidths = "50 50";

Aspose.Pdf.Row row2 = table.Rows.Add();

row2.FixedRowHeight = 20;

Aspose.Pdf.Cell row2_cell1 = row2.Cells.Add();

row2_cell1.BackgroundColor = Aspose.Pdf.Color.Green;

Aspose.Pdf.Cell row2_cell2 = row2.Cells.Add();

row2_cell2.Paragraphs.Add(new TextFragment("ABC"));

// add blank row for spacing

Aspose.Pdf.Row row_blank = table.Rows.Add();

row_blank.Cells.Add();

Aspose.Pdf.Row row = table.Rows.Add();

row.FixedRowHeight = 20;

Aspose.Pdf.Cell cell1 = row.Cells.Add();

cell1.BackgroundColor = Aspose.Pdf.Color.Yellow;

Aspose.Pdf.Cell cell2 = row.Cells.Add();

cell2.Paragraphs.Add(new TextFragment("XYZ"));

pdfDocument.Save(“c:/pdftest/TableOutput_New.pdf”);