Inserting Table

Hi all, i’m lost…
i can help…i dont know how can i insert table after my text… i write text to document

builder.write("some text");

and i have method which return a Aspose.Word.Tables.Table and i don’ t know how can my table insert after text…

my method:

public Aspose.Words.Tables.Table NakresliTabulku(DocumentBuilder db, int stlpcov, int riadkov)
{
    Aspose.Words.Tables.Table Tabulka = new Aspose.Words.Tables.Table(db.Document);

    db.RowFormat.Borders.Color = Color.Black;
    db.RowFormat.Borders.LineStyle = LineStyle.Single;
    db.RowFormat.Borders.LineWidth = 1;
    for (int i = 0; i <= riadkov - 1; i++)
    {
        Aspose.Words.Tables.Row riadok = new Aspose.Words.Tables.Row(db.Document);

        Tabulka.Rows.Add(riadok);

        for (int j = 0; j <= stlpcov - 1; j++)
        {
            Aspose.Words.Tables.Cell bunka = new Aspose.Words.Tables.Cell(db.Document);

            bunka.CellFormat.Borders.LineStyle = LineStyle.Single;

            if (i % 2 == 0)
                bunka.CellFormat.Shading.BackgroundPatternColor = Color.DarkGray;
            else
                bunka.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;

            if (i == 0 || j == 0)
                bunka.CellFormat.Shading.BackgroundPatternColor = Color.Gray;

            Tabulka.Rows[i].Cells.Add(bunka);
        }
    }

    db.InsertBreak(BreakType.LineBreak);

    return Tabulka;
}

Hi

Thanks for your request. There are two ways of building tables using Aspose.Words. You can build tables using DocumentBuilder as described here:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/insertcell/
Also, you can build table using DOM (Document Object Model).
You are using both of these ways. I little bit changed your method, please see the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Test");
NakresliTabulku(builder, 10, 10);
// Save output document.
doc.Save(@"Test194\out.doc");
public void NakresliTabulku(DocumentBuilder db, int stlpcov, int riadkov)
{
    db.StartTable();
    db.RowFormat.Borders.Color = Color.Black;
    db.RowFormat.Borders.LineStyle = LineStyle.Single;
    db.RowFormat.Borders.LineWidth = 1;
    for (int i = 0; i <= riadkov - 1; i++)
    {
        for (int j = 0; j <= stlpcov - 1; j++)
        {
            db.CellFormat.Borders.LineStyle = LineStyle.Single;
            if (i % 2 == 0)
                db.CellFormat.Shading.BackgroundPatternColor = Color.DarkGray;
            else
                db.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
            if (i == 0 || j == 0)
                db.CellFormat.Shading.BackgroundPatternColor = Color.Gray;
            db.InsertCell();
        }
        db.EndRow();
    }
}

Best regards,

i try this and it work… it’s a right creating word document?

private void button1_Click(object sender, EventArgs e)
{

    DocumentBuilder builder = new DocumentBuilder();

    VypisText(builder, textBox3.Text, StyleIdentifier.Heading1);
    VypisText(builder, textBox4.Text, StyleIdentifier.Normal);
    VypisText(builder, textBox5.Text, StyleIdentifier.Heading2);
    builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
    builder.Writeln();

    builder.CurrentSection.Body.Tables.Add(NakresliTabulku(builder, 5, 3));

    VypisText(builder, textBox2.Text, StyleIdentifier.Normal);

    builder.Document.Save("C:\2003.doc", SaveFormat.Doc);
    builder.Document.Save("C:\2007.docx", SaveFormat.Docx);
}

=====================================================================

private Paragraph VypisText(DocumentBuilder builder, string Textik, StyleIdentifier Styl)
{
    Paragraph P = new Paragraph(builder.Document);
    Run R = new Run(builder.Document);
    Aspose.Words.TabStop ts = new TabStop(2.0);

    ts.Alignment = Aspose.Words.TabAlignment.Right;
    R.Text = Textik;
    P.ParagraphFormat.StyleIdentifier = Styl;
    P.Runs.Add«;

    builder.CurrentSection.Body.Paragraphs.Addº;

    return P;
}

public Aspose.Words.Tables.Table NakresliTabulku(DocumentBuilder db, int stlpcov, int riadkov)
{
    Aspose.Words.Tables.Table Tabulka = new Aspose.Words.Tables.Table(db.Document);

    db.RowFormat.Borders.Color = Color.Black;
    db.RowFormat.Borders.LineStyle = LineStyle.Single;
    db.RowFormat.Borders.LineWidth = 1;
    for (int i = 0; i <= riadkov - 1; i++)
    {
        Aspose.Words.Tables.Row riadok = new Aspose.Words.Tables.Row(db.Document);

        Tabulka.Rows.Add(riadok);

        for (int j = 0; j <= stlpcov - 1; j++)
        {
            Aspose.Words.Tables.Cell bunka = new Aspose.Words.Tables.Cell(db.Document);

            bunka.CellFormat.Borders.LineStyle = LineStyle.Single;

            if (i % 2 == 0)
                bunka.CellFormat.Shading.BackgroundPatternColor = Color.DarkGray;
            else
                bunka.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;

            if (i == 0 || j == 0)
                bunka.CellFormat.Shading.BackgroundPatternColor = Color.Gray;

            Tabulka.Rows[i].Cells.Add(bunka);
        }
    }

    db.InsertBreak(BreakType.LineBreak);

    return Tabulka;
}

Hi Michal,
Your code looks good, apart from you normally should be using document builder off a new document object. Please see the code below:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// generate table here
doc.Save(...);

Thanks,

Ok… thans for youre reply…