We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Auto size for 2 cells in a row

Hi,

I am trying to add a Table in a PDF having 1 row that should look as follows: 2018-11-26_1252.png (1.8 KB)

The key thing here is that the left cell should push the right one as further as possible to the right while both cells do not break.

I tried to play with both ColumnWidths and ColumnAdjustment for the table but nothing really works.

Hoe can I accomplish this?

Thank you!

@gwert

Thank you for contacting support.

You can manipulate widths of cells using cell.ColSpan property before adding that table in paragraph collection of the page. You may devise a mathematical expression to calculate column span based on width of page, text fragment and page margins, as per your requirements. A simple code snippet explaining the approach has been shared below for your kind reference.

// Instantiate Document object
Document doc = new Document();
// Create table instance
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
// Set width for table cells
table.ColumnWidths = "60 60";
table.DefaultCellBorder = new BorderInfo(Aspose.Pdf.BorderSide.All, 1F);
// Create row object and add it to table instance
Aspose.Pdf.Row row = table.Rows.Add();
// Create cell object and add it to row instance
Aspose.Pdf.Cell cell1 = row.Cells.Add();
// Add textfragment to paragraphs collection of cell object
cell1.Paragraphs.Add(new TextFragment("First Wide cell"));
// Add another cell to row object
Cell cell2 = row.Cells.Add();
TextFragment fragment = new TextFragment("Second cell");
cell2.Paragraphs.Add(fragment);
// Create page object and add it to pages collection of document instance
Aspose.Pdf.Page page = doc.Pages.Add();
page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
Console.WriteLine("Fragment Width: " + fragment.Rectangle.Width);
Console.WriteLine("Page Width: " + page.PageInfo.Width);
Console.WriteLine("Page Margins: " + page.PageInfo.Margin.Right + page.PageInfo.Margin.Left);
// Calclate suitable column span based on above Widths and margins
cell1.ColSpan = 2;
// Add table to paragraphs collection of page object
page.Paragraphs.Add(table);
dataDir = dataDir + "TwoCells_18.11.pdf";
// Save PDF file
doc.Save(dataDir);

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

Hi,

Sorry for getting back on this one so lateā€¦

The good news that we accomplished the desired result using a similar approach based on setting the width of the last/right cell to the width of a the TextFragment containing the text of the cell.

Just wanted to say thank you!

:smiley:

@gwert

Thank you for your kind feedback.

We are glad to know that you have accomplished your requirements with similar approach. Please keep using our API and in event of any further query, feel free to ask.