ASPOSE.WORDS. How to substitute of ASPOSE.Tables BY Paragraphs objecs

Hi!
There is a question: how to substitute following code with use object builder.InsertParagraph();
…etc.

In other words is it possible to build word tables by means ASPOSE Paragraph object with rich (the same) Formatting features as (in the code below) ASPOSE Table does?
As a result, please, see attached picture.

…
BorderCollection borders = builder.CellFormat.Borders;
borders.LineStyle = Aspose.Words.LineStyle.Single;
borders.LineWidth = 1;
borders.Color = System.Drawing.Color.LightGray;
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.White;
…

builder.StartTable();
builder.CellFormat.Width = 75;
//builder.CellFormat.Width = builder.CurrentSection.PageSetup.PageWidth;
builder.RowFormat.Height = 18;
builder.RowFormat.HeightRule = HeightRule.Exactly;
builder.Font.Bold = false;
builder.RowFormat.Alignment = RowAlignment.Center;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Top;

for (int i = 0; i < tbl.Rows.Count; i++)
{
    for (int j = 0; j < tbl.Columns.Count; j++)
    {
        builder.RowFormat.Alignment = RowAlignment.Center;
        builder.InsertCell();

        switch (j)
        {
            case 0:
                builder.CellFormat.Width = 90;
                break;
            case 3:
                builder.CellFormat.Width = 85;
                break;
            case 4:
                builder.CellFormat.Width = 105;
                break;
            default:
                builder.CellFormat.Width = 60;
                break;
        }

        if (i == 0)
        {
            builder.Font.Bold = true;
            builder.Write(tbl.Rows[0][j].ToString().Replace("\n", " "));
        }
        else
        {
            builder.Font.Bold = false;
            builder.Write(tbl.Rows[i][j].ToString());
        }
    }
    builder.EndRow();
}

builder.EndTable();

Hi Alex,

Could you clarify exactly what you mean? From what I understand you want to insert rich text into a cell by making the table out of paragraphs instead? You can instead simply insert paragraphs and change font for each cell. See the code below which shows you how to achieve this:

Cell currentCell = builder.InsertCell();
Run run = new Run(doc, "Text");
run.Font.Bold = true;
Paragraph para = new Paragraph(doc);
para.AppendChild(run);
currentCell.AppendChild(para);

If this is not what you are trying to do could you provide an example of what you are trying to achieve?

Thanks,

Thank you. I’ll try that and let you know of results later. May be no additional info will be needed.

Adam, this example is OK, but I cannot use it, because template of the document is already loaded into builder like that:

Document doc = new Document(System.IO.Path.Combine(sPathToReportBuilderWordTemplate, "ReportBuilder.docx")); 

So, I cannot use Run object:

Run run = new Run(doc, "Text");

and all insertion in document is done through builder obj like that:

builder.Write(tbl.Rows[i][j].ToString());

Do you have another variants for this solution?

Adam, this example is OK, but I cannot use it, because template of the document is already loaded into builder like that:

Document doc = new Document(System.IO.Path.Combine(sPathToReportBuilderWordTemplate, "ReportBuilder.docx")); 

So, I cannot use Run object:

Run run = new Run(doc, "Text");

and all insertion in document is done through builder obj like that:

builder.Write(tbl.Rows[i][j].ToString());

Do you have another variants for this solution?

Hi Alex,

Do you mean you are inserting text into an existing table? If so then you can still attach a new run and paragraph to the table which will give you the formatting you want. Could you please attach your template here and a snippet of the code your using? We can take a look and suggest a method that will work for you.

Table table = doc.Sections[0].Body.Tables[0];
Run runID = new Run(doc, tbl.Rows[i][j].ToString());
table.Rows[0].Cells[0].FirstParagraph.AppendChild(runID);

All attachments can only be opened by Aspose staff so your document will be kept private.

Thanks,

No, everything much simpler in my case
“ReportBuilder.docx” I use/load as template, but it’s just absolutely empty Microsoft Word document

Document doc = new Document(System.IO.Path.Combine(sPathToReportBuilderWordTemplate, "ReportBuilder.docx")); 
DocumentBuilder builder = new DocumentBuilder(doc);
builder.StartTable(); // I need to get rid of that and use instead Paragraph object
builder.InsertCell();
...
builder.Write("something");
builder.EndRow(); // I need to get rid of that and use instead Paragraph object
builder.EndTable(); // I need to get rid of that and use instead Paragraph object

How I’m building table and how that table looks like, please see at my first post.
But if I use Paragraph object I need the same formatting

builder.RowFormat.HeightRule = HeightRule.Exactly;
builder.Font.Bold = false;
builder.RowFormat.Alignment =
RowAlignment.Center;

Hi Alex,

Could you explain a little further excatly what you’re trying to achieve? It sounds to me like you just want to insert the text into new paragraphs with custom formatting but still have it look like a table? Why can’t you use the table as how you have it now and insert the text into new paragraphs inside the table?

Hopefully answering these will clear this up and I can give you some suggestions.

Thanks,

I understand that now it may be simpler for me to build tables by means Aspose words tables objects, but I’m just asking (repeating the question in first post):

  1. if it possible to do that only by means Aspose Paragraphs objects ? (if yes - show please ‘how’)

  2. I presume that in future I’ll benefit from using Paragraph object in comparison with Aspose table object for building Aspose word report (having in hands much richer Aspose features to build word report) - is that all true or not???

Thanks.

Hi

Thank you for additional information. Actually, you should note that table actually consist of rows containing cells containing paragraphs. Please see Aspose.Words Document Object Model to learn more:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/object-model-overview.html

When, you build table using DocuemntBuidler, you can use all formatting features. For example, see the following code:

DocumentBuilder builder = new DocumentBuilder();
// build simple table with different text and paragraph formating:
builder.InsertCell();
builder.CellFormat.Width = 200;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Size = 20;
builder.Write("this is big text");
builder.Font.ClearFormatting();
builder.Write("this is simpel text");
builder.InsertCell();
builder.CellFormat.Width = 300;
builder.ParagraphFormat.ClearFormatting();
builder.Font.Italic = true;
builder.Write("this is italic text");
builder.EndRow();
builder.EndTable();
builder.Document.Save(@"Test001\out.doc");

Hope this helps.

Best regards.

Thank you Alexey!
This answer do helped me.
This issue may be closed.