We have a generic table builder that can inject tables into a word document at a specific Word field. I’ve simplified the code to reproduce problem and the document. Could you please tell me two things:
-
Why don’t the widths/layout of the cells all line up when the sum of the column widths for each of the rows are the same?
-
When I have Word set to display all formatting marks, what are the end marks at the end of each table row? I only seem to get those when I have a document template as the source (as in my sample code attached). But if I don’t open a sample document and just create an empty document then a documentBuilder from that, I don’t seem to get those (and the problem doesn’t seem to present itself).
Thanks so much,
Terry
Here is code to generate document:
public void TestTableBuilder()
{
var fileName = @"c:\users\terry.aney\desktop\Aspose.Testing\TableBuilder.doc";
Document document;
using (var ms = new MemoryStream(File.ReadAllBytes(@"c:\users\terry.aney\desktop\Aspose.Testing\TableBuilder.docx")))
{
document = new Document(ms);
}
var documentBuilder = new DocumentBuilder(document);
var table = documentBuilder.StartTable();
var field = document.Sections[0].Body.Range.Fields[0];
documentBuilder.MoveToField(field, false);
field.Result = ""; //can’t delete field name or the foreach doesn’t work
// Merge four cells
FormatCell(documentBuilder, 620, CellMerge.First, "Basic Information");
SpanColumns(documentBuilder, 4);
documentBuilder.EndRow();
// No Merge
FormatCell(documentBuilder, 260, CellMerge.None, "1");
FormatCell(documentBuilder, 150, CellMerge.None, "2");
FormatCell(documentBuilder, 20, CellMerge.None, "3");
FormatCell(documentBuilder, 190, CellMerge.None, "4");
documentBuilder.EndRow();
// Merge four cells
FormatCell(documentBuilder, 620, CellMerge.First, "at Benefit Commencement Date");
SpanColumns(documentBuilder, 4);
documentBuilder.EndRow();
// Merge two cells
FormatCell(documentBuilder, 260, CellMerge.First, "");
SpanColumns(documentBuilder, 1);
FormatCell(documentBuilder, 360, CellMerge.First, "at Benefit Commencement Date");
SpanColumns(documentBuilder, 3);
documentBuilder.EndRow();
// No Merge
FormatCell(documentBuilder, 260, CellMerge.None, "1");
FormatCell(documentBuilder, 150, CellMerge.None, "2");
FormatCell(documentBuilder, 20, CellMerge.None, "3");
FormatCell(documentBuilder, 190, CellMerge.None, "4");
documentBuilder.EndRow();
table.AllowAutoFit = false;
table.PreferredWidth = PreferredWidth.Auto;
table.LeftIndent = ConvertUtil.InchToPoint(0.1);
table.ClearBorders();
documentBuilder.EndTable();
document.Save(fileName);
}
public void SpanColumns(DocumentBuilder documentBuilder, int columnCount)
{
for (int i = 0; i
{
documentBuilder.InsertCell();
documentBuilder.CellFormat.HorizontalMerge = CellMerge.Previous;
}
}
public void FormatCell(DocumentBuilder documentBuilder, double width, CellMerge merge, string value)
{
documentBuilder.RowFormat.AllowBreakAcrossPages = false;
documentBuilder.RowFormat.HeadingFormat = false;
documentBuilder.InsertCell();
documentBuilder.CellFormat.HorizontalMerge = merge;
documentBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
documentBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(ConvertUtil.InchToPoint(width / 96));
documentBuilder.CellFormat.TopPadding = documentBuilder.CellFormat.BottomPadding = ConvertUtil.InchToPoint(0.02);
documentBuilder.CellFormat.LeftPadding = documentBuilder.CellFormat.RightPadding = ConvertUtil.InchToPoint(0.1);
documentBuilder.Write(value);
}