Document table/paragraph format issue

Hi,

we are using aspose-word licensed version 14.12.0.0 and aspose-pdf version 8.2.0. I would like to
generate the document with content controls and document generated successfully. But we are facing some alignment issue. Please see the we attached the sample project and screenshot for your review and give me the solution based on our sample project.
Is there any way to avoid this alignment issue. How can i achieve this?.

Note: Please include the dll aspose-word version 14.12.0.0 and aspose-pdf version 8.2.0.

We are using following code for table/paragraph issue.

string File = “D:\Table Issue.Docx”;
Aspose.Words.Document doc = new Aspose.Words.Document(File);

        List<string> Tags = new List<string>();

        List<string> Row1 = new List<string>();
        Row1.Add("Test1");
        Row1.Add("Test2");

        string value1 = string.Join(Aspose.Words.ControlChar.LineBreak, Row1);

        List<string> Row2 = new List<string>();
        Row2.Add("These messages also appear in green text bubbles and go through your carrier. Group SMS messages don’t support multimedia attachments, like photos or videos. All responses in a Group SMS are sent as individual text messages and the recipients can’t see the other responses from the group.");
        Row2.Add("These messages appear in green text bubbles and go through your carrier instead of Apple. In a group MMS, everyone can:Send and receive photos and videos See all responses from the group Mute notifications.");

        string value2 = string.Join(Aspose.Words.ControlChar.LineBreak, Row2);

        List<string> Row3 = new List<string>();
        Row3.Add("Test Row1");
        Row3.Add("Test Row2");

        string value3 = string.Join(Aspose.Words.ControlChar.Lf, Row3);

        Dictionary <string, string> aProductCost = new Dictionary<string, string>();
        aProductCost.Add("PL_Test_Network1", value1);
        aProductCost.Add("PL_Test_Network2", value2);
        aProductCost.Add("PL_Test_Network3", value3);

        List<Aspose.Words.Markup.StructuredDocumentTag> sdtNodes = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<Aspose.Words.Markup.StructuredDocumentTag>().ToList<Aspose.Words.Markup.StructuredDocumentTag>();

        string Value = string.Empty;
        foreach (Aspose.Words.Markup.StructuredDocumentTag cc in sdtNodes)
        {
            Value = aProductCost[cc.Title];
            if (Value != null)  //when found, apply the value. 
            {
                mergeDataToContentControl(cc, Value);
            }
        }

        System.IO.MemoryStream outStream = new System.IO.MemoryStream();
        doc.Save(outStream, Aspose.Words.SaveFormat.Docx);
        byte[] docBytes = null;
        docBytes = outStream.ToArray();
        FileStream output = new FileStream("D:\\Table Issue1.Docx", FileMode.Create,
        FileAccess.Write);
        // Write byte array contents in the output file stream**
        output.Write(docBytes, 0, docBytes.Length);
        // Close output file
        output.Close();

Table Issue.zip (1.3 MB)
Excepted Output.jpg (58.0 KB)
CurrentOutput.jpg (67.0 KB)

@smani

Thanks for your inquiry. In your case, we suggest you please insert the content in table’s cells instead of content control. you need to insert new rows in the table and insert the text in row’s cell.

If you still face the issue, please share the code of mergeDataToContentControl method. Please also share the problematic output and expected output Word documents. We will investigate the issue and provide you more information on it.

PS : Please create the expected output document using MS Word.

Hi tahir.manzoor,

Thanks for your quick response. You suggest use content in table’s cells instead of content control, But based on our customer requirements only use content control not table’s cells. So we want to solution based on our sample project(previously attached) with content control.

Also you request, we are used below code for mergeDataToContentControl method.

private void mergeDataToContentControl(Aspose.Words.Markup.StructuredDocumentTag tag, string value)
{
var paragraphs = tag.GetChildNodes(NodeType.Paragraph, true).ToArray();
if (paragraphs != null && paragraphs.Length > 0)
{

            Run run = null;
            Paragraph par = (Paragraph)paragraphs[0];

            if (par.Runs != null && par.Runs.Count > 0)
            {
                run = (Run)par.Runs[0].Clone(true);
                run.Text = value;
            }
            else
            {
                run = new Run(tag.Document, value);
            }

            par.Runs.Clear();
            par.Runs.Add(run);
        }
        else  //case: when there is no paragraph as part of the Content Control.
        {
            var runs = tag.GetChildNodes(NodeType.Run, true).ToArray();
            if (runs != null && runs.Length > 0)
            {
                Run r = (Run)runs[0];
                r.Text = string.Empty;
                r.Text = value;
            }
            else
            {
                tag.AppendChild(new Run(tag.Document, value));
            }
        }
    }

and attached the problematic output and expected output Word documents zip file for your reference. Please refer the our input file and give me the solution based on our sample project. One of our valuable customer waiting for solution for this issue.

Please give the solution ASAP.

Thanks in advance.

TableIssue.zip (43.5 KB)

@smani

Thanks for sharing the detail. In your expected output document, you added empty paragraphs in table’s cells. This is not good approach to set the text in table’s cells. In your case, we suggest you please clone the row using the following code example and insert the text in the content controls in each cell. Hope this helps you.

Document doc = new Document(MyDir + "OriginalFileWithContentControls.docx");
Table table = doc.FirstSection.Body.Tables[0];
Row row = (Row)table.FirstRow.Clone(true);

table.Rows.Add(row);
doc.Save(MyDir + @"Output.docx");

Hi tahir.manzoor,

Thanks for your quick response. We referred the your latest solution and we tried to resolve the this issue. But our customer documents used one or more tables, how to identify the table and insert the content controls in documents. Please refer the my attached latest documents and give me the solution. We need to sample code for this issue, based on my existing code and current attached documents.

Please give the solution ASAP.

Thanks in advance.

TableIssue.zip (14.9 KB)

@smani

Thanks for sharing the detail. You can identify the table using StructuredDocumentTag.Tag property. Following code example identifies the table using tag property of content control, clone the table’s rows, and insert the text inside content control. Hope this helps you.

Document doc = new Document(MyDir + "OriginalFileWithContentControls.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

var nodes = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>().ToList().Where(sdt => sdt.Tag == "PL_Test_Network1").ToList();
if (nodes.Count > 0)
{
    StructuredDocumentTag sdt = nodes[0];

    if (sdt.GetAncestor(NodeType.Table) != null)
    {
        Table table = doc.FirstSection.Body.Tables[0];
        //e.g. your datatable has 10 rows.
        for (int i = 0; i < 10; i++)
        {
            Row row = (Row)table.FirstRow.Clone(true);
            table.Rows.Add(row);
        }

        foreach (Row row in table.Rows)
        {
            foreach (Cell cell in row.Cells)
            {
                foreach (StructuredDocumentTag structuredDocumentTag in cell.GetChildNodes(NodeType.StructuredDocumentTag, true))
                {
                    if (structuredDocumentTag.Tag == "PL_Test_Network1")
                    {
                        builder.MoveTo(structuredDocumentTag.FirstChild);
                        builder.Write("some content1");
                    }
                    else if (structuredDocumentTag.Tag == "PL_Test_Network2")
                    {
                        builder.MoveTo(structuredDocumentTag.FirstChild);
                        builder.Write("some content2");
                    }
                    else if (structuredDocumentTag.Tag == "PL_Test_Network3")
                    {
                        builder.MoveTo(structuredDocumentTag.FirstChild);
                        builder.Write("some content3");
                    }
                }
            }
        }

    }
}

doc.Save(MyDir + @"Output.docx");

Hi Tahit.manzoor,

we have updated aspose lastest version(Aspose.Pdf -Version 19.4.0- Aspose.Words -Version 19.4.0)
but still, we are facing the same issue.
previously we used aspose-word licensed version 14.12.0.0 and aspose-pdf version 8.2.0.
I would like to generate the document with content controls and document generated successfully. But we are facing some alignment issue. Please see we attached the sample project and screenshot for your review and give me the solution based on our sample project.
Is there any way to avoid this alignment issue. How can I achieve this?.
Note: lastest aspose Version aspose-word version 19.4.0 and aspose-pdf version 19.4.0

We are using following code for table/paragraph issue.

string File = “D:\Table Issue.Docx”;
Aspose.Words.Document doc = new Aspose.Words.Document(File);

Blockquote

    List<string> Tags = new List<string>();

    List<string> Row1 = new List<string>();
    Row1.Add("Test1");
    Row1.Add("Test2");

    string value1 = string.Join(Aspose.Words.ControlChar.LineBreak, Row1);

    List<string> Row2 = new List<string>();
    Row2.Add("These messages also appear in green text bubbles and go through your carrier. Group SMS messages don’t support multimedia attachments, like photos or videos. All responses in a Group SMS are sent as individual text messages and the recipients can’t see the other responses from the group.");
    Row2.Add("These messages appear in green text bubbles and go through your carrier instead of Apple. In a group MMS, everyone can:Send and receive photos and videos See all responses from the group Mute notifications.");

    string value2 = string.Join(Aspose.Words.ControlChar.LineBreak, Row2);

    List<string> Row3 = new List<string>();
    Row3.Add("Test Row1");
    Row3.Add("Test Row2");

    string value3 = string.Join(Aspose.Words.ControlChar.Lf, Row3);

    Dictionary <string, string> aProductCost = new Dictionary<string, string>();
    aProductCost.Add("PL_Test_Network1", value1);
    aProductCost.Add("PL_Test_Network2", value2);
    aProductCost.Add("PL_Test_Network3", value3);

    List<Aspose.Words.Markup.StructuredDocumentTag> sdtNodes = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<Aspose.Words.Markup.StructuredDocumentTag>().ToList<Aspose.Words.Markup.StructuredDocumentTag>();

    string Value = string.Empty;
    foreach (Aspose.Words.Markup.StructuredDocumentTag cc in sdtNodes)
    {
        Value = aProductCost[cc.Title];
        if (Value != null)  //when found, apply the value. 
        {
            mergeDataToContentControl(cc, Value);
        }
    }

    System.IO.MemoryStream outStream = new System.IO.MemoryStream();
    doc.Save(outStream, Aspose.Words.SaveFormat.Docx);
    byte[] docBytes = null;
    docBytes = outStream.ToArray();
    FileStream output = new FileStream("D:\\Table Issue1.Docx", FileMode.Create,
    FileAccess.Write);
    // Write byte array contents in the output file stream**
    output.Write(docBytes, 0, docBytes.Length);
    // Close output file
    output.Close();

CurrentOutput.jpg (67.0 KB)
Excepted Output.jpg (58.0 KB)
Table Issue.zip (1.3 MB)

@smani

You are facing the expected behavior of Aspose.Words. The cell’s content contains line break (ControlChar.LineBreak). So, the data “Test2” and “Test Row2” will be on the second line of cell.

In your case, we suggest you please split the data by line break , clone the table’s row and insert the content into new row. Please check the code example shared in my previous post.