This project has moved. For the latest updates, please go here.

Table column width messed up

Jul 23, 2012 at 4:52 AM

Hi,

Firstly let me thank you for creating such a great API.

I'm trying to insert a table with 16 columns into the document and when it is created, the table columns got messed up (going outside of the page). Any idea on how should I go about fixing that? I have tried Autofit.Window but it couldn't work.

Jul 23, 2012 at 5:42 PM

Can you show us your code?

Developer
Jul 23, 2012 at 6:25 PM

You could try going by static column width.

Jul 23, 2012 at 10:31 PM

May I know how I can use static column width? Below are the codes that I've written.

public static void CreateDocument()
{
	using (DocX document = DocX.Create(@"document.docx"))
	{
		Paragraph pTitle = document.InsertParagraph().Append("Title").Bold().Font(new FontFamily("Times New Roman")).FontSize(18).UnderlineStyle(UnderlineStyle.singleLine);
		pTitle.AppendLine();
		DataTable tableHeader = new DataTable("tableHeader");
		DataTable tableData = new DataTable("tableData");
		GenerateTable(tableHeader, tableData);
		Table table = pTitle.InsertTableAfterSelf(tableData.Rows.Count + 1, tableHeader.Rows.Count);
	
		// Loop through the header table
		for (int headerCell = 0; headerCell < table.ColumnCount; headerCell++
		{
			Paragraph pHeader = table.Rows[0].Cells[headerCell].Paragraphs[0];
			pHeader.Insert(tableHeader.Rows[headerCell].ItemArray[0].ToString(), false);
			pHeader.Alignment = Alignment.center;
		}
	
		// loop through the data table
		for (int row = 1; row < table.RowCount; row++)
			{
				for (int cell = 0; cell < table.Rows[row].Cells.Count; cell++)
				{
					Paragraph cellParagraph = table.Rows[row].Cells[cell].Paragraphs[0];
					cellParagraph.InsertText(tableData.Rows[row - 1].ItemArray[cell].ToString(), false);
				}
			}
	table.Design = TableDesign.TableGrid;
	table.Alignment = Alignment.left;
	document.InsertParagraph();
}

private static void GenerateTable(DataTable tableHeader, DataTable tableData)
{
	// header
	tableHeader.Columns.Add();
	tableHeader.Rows.Add(new object[] { "SN" });
	tableHeader.Rows.Add(new object[] { "Name" });
	tableHeader.Rows.Add(new object[] { "Name" });
	tableHeader.Rows.Add(new object[] { "Name" });
	tableHeader.Rows.Add(new object[] { "Name" });
	tableHeader.Rows.Add(new object[] { "Name" });
	tableHeader.Rows.Add(new object[] { "Name" });
	tableHeader.Rows.Add(new object[] { "Name" });
	tableHeader.Rows.Add(new object[] { "Name" });
	tableHeader.Rows.Add(new object[] { "Name" });
	tableHeader.Rows.Add(new object[] { "Name" });
	tableHeader.Rows.Add(new object[] { "Name" });
	tableHeader.Rows.Add(new object[] { "Name" });
	tableHeader.Rows.Add(new object[] { "Name" });
	tableHeader.Rows.Add(new object[] { "Name" });
	tableHeader.Rows.Add(new object[] { "Name" });
	
	// data
	tableData.Columns.Add();
	tableData.Columns.Add();
	tableData.Columns.Add();
	tableData.Columns.Add();
	tableData.Columns.Add();
	tableData.Columns.Add();
	tableData.Columns.Add();
	tableData.Columns.Add();
	tableData.Columns.Add();
	tableData.Columns.Add();
	tableData.Columns.Add();
	tableData.Columns.Add();
	tableData.Columns.Add();
	tableData.Columns.Add();
	tableData.Columns.Add();
	tableData.Columns.Add();
	
	for (int i = 0; i < 10; i++)
	{
		tableData.Rows.Add(new object[] { i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i });
	}
}