Free Support Forum - aspose.com

How to create a floating table?

I can create a table with success using the following example code...

Dim table As New Aspose.Words.Tables.Table(_Document)

' Create the table rows
For rowIndex = 0 To 4
Dim row As New Aspose.Words.Tables.Row(_Document)
row.RowFormat.Height = 30
table.Rows.Add(row)

' Create the cells for the row
For colIndex = 0 To 4
Dim cell As New Aspose.Words.Tables.Cell(_Document)
cell.CellFormat.Borders.LineWidth = 1
cell.CellFormat.Borders.Color = System.Drawing.Color.Black
cell.CellFormat.Width = 30
row.Cells.Add(cell)
Next
Next

_Builder.CurrentParagraph.ParentNode.InsertAfter(table, _Builder.CurrentParagraph)

However, I need the table to be floating so that it is positioned at an exact location, in the same way you can exactly position a TextBox. Is this possible and if so how?

Hi

Thanks for your request. Unfortunately, there is no direct way to make the table floating. However, you can achieve this by putting your table into a text box shape. For example, see the following code:

Document doc = new Document();

// Create table.

Table table = new Table(doc);

for (int rowIndex = 0; rowIndex < 5; rowIndex++)

{

Row row = new Row(doc);

row.RowFormat.Height = 30;

table.Rows.Add(row);

// Create the cells for the row

for (int cellIndex = 0; cellIndex < 5; cellIndex++)

{

Cell cell = new Cell(doc);

cell.CellFormat.Borders.LineWidth = 1;

cell.CellFormat.Borders.Color = System.Drawing.Color.Black;

cell.CellFormat.Width = 30;

row.Cells.Add(cell);

}

}

// Create a shape for table.

Shape tableBox = new Shape(doc, ShapeType.TextBox);

// Configure text box.

tableBox.Width = 154;

tableBox.Height = 154;

tableBox.Stroked = false;

tableBox.TextBox.InternalMarginBottom = 2;

tableBox.TextBox.InternalMarginLeft = 2;

tableBox.TextBox.InternalMarginRight = 0;

tableBox.TextBox.InternalMarginTop = 0;

tableBox.TextBox.FitShapeToText = true;

// Set absolute position of shape.

tableBox.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;

tableBox.RelativeVerticalPosition = RelativeVerticalPosition.Page;

tableBox.HorizontalAlignment = HorizontalAlignment.Center;

tableBox.VerticalAlignment = VerticalAlignment.Center;

// Insert a table into a text box.

tableBox.AppendChild(table);

// Insert text box into the document.

doc.FirstSection.Body.FirstParagraph.AppendChild(tableBox);

// Save output.

doc.Save(@"Test001\out.doc");

Hope this helps.

Best regards,

Thanks, it does indeed allow me to create a floating table which is just what I need. There is however a rendering issue. Outputting to docx works just great. But outputing to .html causes the border on the left edge of the table to be missing. Outputting to .pdf just about works in that you get the left edge of the table but it is much thinner than it should be.

I tried altering the internal margin on the TextBox but that did not help.

Hi

Thanks for your request. Please try to center the table inside textbox:

Row row = new Row(doc);

row.RowFormat.Height = 30;

// this is needed to center the table.

row.RowFormat.Alignment = RowAlignment.Center;

table.Rows.Add(row);

Hope this helps.

Best regards,

Changing the row alignment solves the issue when outputting to pdf. Now it is just the HTML that is incorrect. In that case it misses off the right hand edge!

Hi

Thanks for your request. Please try to make a text box a little wider and set internal margin greater than zero:

// Configure text box.

tableBox.Width = 170;

tableBox.Height = 170;

tableBox.Stroked = false;

tableBox.TextBox.InternalMarginBottom = 2;

tableBox.TextBox.InternalMarginLeft = 2;

tableBox.TextBox.InternalMarginRight = 2;

tableBox.TextBox.InternalMarginTop = 2;

tableBox.TextBox.FitShapeToText = true;

Best regards,