Hi Thomas,
here is the function I use to build the TextFrame (which contains the table)
Code:
public Table BuildAbsoluteTable(List<TableElement> cells, List<double> wids, List<double> heis, Unit xPos, Unit yPos)
{
if (cells.Count % wids.Count == 0 && (cells.Count / wids.Count) / heis.Count == 1)
{
double _tblWidth = 0;
for (int i = 0; i < wids.Count; i++)
{
_tblWidth += wids[i];
}
MigraDoc.DocumentObjectModel.Shapes.TextFrame _textFrame = new MigraDoc.DocumentObjectModel.Shapes.TextFrame();
_textFrame.Left = xPos;
_textFrame.Top = yPos;
_textFrame.Width = _tblWidth + Unit.FromCentimeter(0.2);
int rows = cells.Count / wids.Count;
int _cellDataIdx = 0;
MigraDoc.DocumentObjectModel.Tables.Table _table = _textFrame.AddTable();
_table.TopPadding = 3;
_table.BottomPadding = 3;
_table.LeftPadding = 3;
_table.RightPadding = 3;
for (int cols = 0; cols < wids.Count; cols++)
{
_table.AddColumn(wids[cols]);
}
try
{
for (int r = 0; r < rows; r++)
{
Row _row = _table.AddRow();
_row.HeightRule = RowHeightRule.Exactly;
_row.Height = heis[r];
for (int c = 0; c < wids.Count; c++)
{
Cell _cell = _row.Cells[c];
if (cells[_cellDataIdx].format.borders.IndexOf("T") != -1)
{
_cell.Borders.Top.Width = cells[_cellDataIdx].format.borderWidth;
}
if (cells[_cellDataIdx].format.borders.IndexOf("B") != -1)
{
_cell.Borders.Bottom.Width = cells[_cellDataIdx].format.borderWidth;
}
if (cells[_cellDataIdx].format.borders.IndexOf("L") != -1)
{
_cell.Borders.Left.Width = cells[_cellDataIdx].format.borderWidth;
}
if (cells[_cellDataIdx].format.borders.IndexOf("R") != -1)
{
_cell.Borders.Right.Width = cells[_cellDataIdx].format.borderWidth;
}
Font font = MigFont.Clone();
font.Bold = cells[_cellDataIdx].format.bold;
font.Italic = cells[_cellDataIdx].format.italic;
font.Size = cells[_cellDataIdx].format.size;
font.Underline = cells[_cellDataIdx].format.underlined;
if (cells[_cellDataIdx].fill)
{
_cell.Shading.Color = cells[_cellDataIdx].fillColor;
}
if (cells[_cellDataIdx].mergeRight > 0)
{
_cell.MergeRight = cells[_cellDataIdx].mergeRight;
}
if (cells[_cellDataIdx].mergeDown > 0)
{
_cell.MergeDown = cells[_cellDataIdx].mergeDown;
}
BuildCell(_cell, cells[_cellDataIdx], font);
_cellDataIdx++;
}
}
CommonData.Instance.Document.LastSection.Add(_textFrame);
}
catch
{
}
return _table;
}
else
{
return null;
}
}
basically the parameters - List<TableElement> cells, List<double> wids, List<double> heis, Unit xPos, Unit yPos are:
- List<TableElement> cells - this is a collection of objects that contain info about a cell, i.e. its borders, bg fill, text image etc.
- wids - a collection of doubles relating to the width of each column
- heis - a collection of doubles relating to each rows height
- xPos - the xPosition on the page to set the TextFrame to
- yPos - the yPosition on the page to set the TextFrame to
this function is called each time I create one of my diagram table "items". Also, there is another table on the page which is built usign this function:
Code:
public Table BuildSizedTable()
{
int rows = Cells.Count / Wids.Count;
int _cellDataIdx = 0;
MigraDoc.DocumentObjectModel.Tables.Table _table = new Table();
_table.TopPadding = 3;
_table.BottomPadding = 3;
_table.LeftPadding = 3;
_table.RightPadding = 3;
for (int cols = 0; cols < Wids.Count; cols++)
{
_table.AddColumn(((Page.Width - LeftMargin - RightMargin) / 100) * Wids[cols]);
}
try
{
for (int r = 0; r < rows; r++)
{
Row _row = _table.AddRow();
_row.HeightRule = RowHeightRule.Auto;
for (int c = 0; c < Wids.Count; c++)
{
Cell _cell = _row.Cells[c];
if (Cells[_cellDataIdx].format.borders.IndexOf("T") != -1)
{
_cell.Borders.Top.Width = Cells[_cellDataIdx].format.borderWidth;
}
if (Cells[_cellDataIdx].format.borders.IndexOf("B") != -1)
{
_cell.Borders.Bottom.Width = Cells[_cellDataIdx].format.borderWidth;
}
if (Cells[_cellDataIdx].format.borders.IndexOf("L") != -1)
{
_cell.Borders.Left.Width = Cells[_cellDataIdx].format.borderWidth;
}
if (Cells[_cellDataIdx].format.borders.IndexOf("R") != -1)
{
_cell.Borders.Right.Width = Cells[_cellDataIdx].format.borderWidth;
}
Font font = MigFont.Clone();
font.Bold = Cells[_cellDataIdx].format.bold;
font.Italic = Cells[_cellDataIdx].format.italic;
font.Size = Cells[_cellDataIdx].format.size;
font.Underline = Cells[_cellDataIdx].format.underlined;
if (Cells[_cellDataIdx].fill)
{
_cell.Shading.Color = Cells[_cellDataIdx].fillColor;
}
if (Cells[_cellDataIdx].mergeRight > 0)
{
_cell.MergeRight = Cells[_cellDataIdx].mergeRight;
}
if (Cells[_cellDataIdx].mergeDown > 0)
{
_cell.MergeDown = Cells[_cellDataIdx].mergeDown;
}
BuildCell(_cell, Cells[_cellDataIdx], font);
_cellDataIdx++;
}
}
CommonData.Instance.Document.LastSection.Add(_table);
}
catch
{
}
return _table;
}
this function is pretty similar apart from there is no text frame and the "wids" parameter is percentages of the page width - this function is tried and tested and seems to work fine, but I have only really written the BuildAbsoluteTable function today.
Maybe it's got to do with the way I add the DocumentElement (i.e. the TextFrame) to the Document's section?:
Code:
CommonData.Instance.Document.LastSection.Add(_textFrame);
doesnt seem to matter when im building "normal" tables that will flow, but obviously when absolute positioning tables, i dont want any flowing to take place.
thanks
Mike