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

Free Support Forum - aspose.com

Dynamic Table Row Creation Adding Extra Line Feed

When generating a dynamic table such as the code posted on the wiki demonstrates:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Start a table

builder.StartTable();
builder.RowFormat.Borders.LineStyle= LineStyle.Single;
builder.RowFormat.Borders.LineWidth = 1;

// Insert a cell

builder.InsertCell();

// Apply formatting to the current cell

builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;

// Output cell content

builder.Writeln("This is row 1 cell 1");
// Insert a cell

builder.InsertCell();

// Output cell content

builder.Writeln("This is row 1 cell 2");

// End current row

builder.EndRow();

a extra spacing row is added to the table thereby inflating the look of the table.

Can this behavior be fixed?

thanks

That is easily solved . Just use Write instead of Writeln.
Best regards,

Fair enough…
But what if I wanted to control the horizontal alignment of the text in each table cell for the row?
I used the InsertHtml method and inserted a div tag in order to do it with version 3.5.1.0 but the latest version injects a new line with the same div method.
Any thoughts?
Thanks,

Basically, formatting of the text inside table cell can be controlled by setting formatting properties in ParagraphFormat, CellFormat, etc.
If you need some particular formatting and cannot find it in the current API then please describe it in terms of MS Word and I will find out if we already have it in the public API or it should be added there.
Concerning InsertHtml, please provide example illustrating the problem and we will try to fix it if possible.
Best regards,

Could you provide me the code needed to create the attached table dynamically?
Thanks

Here is the code:

public void CreateTable()
{

    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    // fill-in data
    ServiceRecordCollection records = new ServiceRecordCollection();
    records.Add(new ServiceRecord("Basic Installation of CPU & Monitor", 50, 143.88));
    records.Add(new ServiceRecord("Install CPU Only", 0, 0));
    records.Add(new ServiceRecord("Peer-to-Peer Data Migration (2GB)", 0, 0));
    records.Add(new ServiceRecord("Dell Program Management", 0, 0));
    // build table
    records.WriteTable(builder);
    string filename = Application.StartupPath + @"\testDynamicTableCreation Out.doc";
    // Save resulting document.
    doc.Save(filename);
    // Open saved document in MS Word.
    System.Diagnostics.Process.Start(filename);
}
public class ServiceRecord
{

    public ServiceRecord(string description, int quantity, double price)
    {
        this.description = description;
        this.quantity = quantity;
        this.price = price;
    }
    private string description;
    public string Description
    {
        get { return description; }
    }
    private int quantity;
    public int Quantity
    {
        get { return quantity; }
    }
    private double price;
    public double Price
    {
        get { return price; }
    }
}
public class ServiceRecordCollection : CollectionBase
{

    public void Add(ServiceRecord serviceRecord)
    {
        List.Add(serviceRecord);
    }
    public ServiceRecord this[int index]
    {
        get { return (ServiceRecord)List[index]; }
    }
    public void WriteTable(DocumentBuilder builder)
    {
        double cellWidth1 = 293.4;
        double cellWidth2 = 108;
        double cellWidth3 = 77.4;
        builder.Font.Name = "Arial";
        builder.Font.Size = 10;
        builder.RowFormat.AllowAutoFit = true;
        builder.RowFormat.Borders.LineStyle = LineStyle.Single;
        builder.RowFormat.Borders.LineWidth = 1;
        builder.RowFormat.Borders.Color = Color.FromArgb(112, 128, 144);
        builder.CellFormat.Shading.BackgroundPatternColor = Color.Silver;
        // write table header
        builder.Font.Bold = true;
        builder.StartTable();
        builder.InsertCell();
        builder.CellFormat.Width = cellWidth1;
        builder.Write("Blended Services");
        builder.InsertCell();
        builder.CellFormat.Width = cellWidth2;
        builder.Write("Estimated Quantity");
        builder.InsertCell();
        builder.CellFormat.Width = cellWidth3;
        builder.Write("Price");
        builder.EndRow();
        builder.Font.Bold = false;
        builder.CellFormat.Shading.BackgroundPatternColor = Color.Empty;
        builder.RowFormat.Borders[BorderType.Horizontal].LineStyle = LineStyle.None;
        // write table contents
        foreach (ServiceRecord rec in this)
        {
            builder.InsertCell();
            builder.CellFormat.Width = cellWidth1;
            builder.Write(rec.Description);
            builder.InsertCell();
            builder.CellFormat.Width = cellWidth2;
            if (rec.Quantity != 0)
                builder.Write(rec.Quantity.ToString());
            builder.InsertCell();
            builder.CellFormat.Width = cellWidth3;
            if (rec.Price != 0)
                builder.Write(rec.Price.ToString("F2"));
            builder.EndRow();
        }
        builder.RowFormat.Borders[BorderType.Horizontal].LineStyle = LineStyle.Single;
        builder.EndTable();
    }
}

Phew Have spent quite much time trying to trim all this properties for a perfect match.
I really think we should make some tool like document disassembler that will allow users to get the Aspose.Words code required for building document or document parts automatically.

Just out of interest why do you want to create that table from code at all? I think you should create the table in MS Word, just one header row and one data row. Then insert merge fields into the row to form a mail merge region and then use Aspose.Words to just insert data into that merge region. I really think creating large chunks of documents programmatically is not something we should recommend to the customers.

Hi team,
Could you please provide a sample code for
“create the table in MS Word, just one header row and one data row. Then insert merge fields into the row to form a mail merge region and then use Aspose.Words to just insert data into that merge region.”
Thanks for your help
Jagadeesh

Hi there,

Thanks for your inquiry.

I think the following article with code examples and video is what you need:

https://docs.aspose.com/words/net/types-of-mail-merge-operations/

Please let us know if you need anything else.

Thanks,