Parse rtf code in a word document

Hi there,

Thanks for your inquiry.

fmr patel:

*First of all the below code to remove the pargraph break is not working. The run.text is never giving the ControlChar.Cr for any of the runs…

foreach (Run run in para.Runs.ToArray())
{
if (run.Text.Contains(ControlChar.Cr))
run.Text = run.Text.Replace(ControlChar.Cr, “”);
}

Also, We need to remove or comment the below line as we are already deleting the StartNode.
para.Remove();*

In your case, there is no need to use the above code.

fmr patel:

*I am able to resolve the above issue by using the code highlighted in yellow in CodeSample.docx.

Let me know, If you have any other alternative solution to handle the scenarios explained above.

However, I am still having the formatting issues mentioned in the comments in the Aspose_Ouput.doc*

The code shared in CodeSample.docx is correct. As you are extracting the RTF text from a Paragraph and insert back RTF document to same paragraph. In this case, the code in CodeSample.docx is correct.

Regarding formatting issue, if you extract the RTF contents manually from input document and save it as RTF document, you will get the same output. Please check the rtfDocs.zip from here:
https://forum.aspose.com/t/51234

fmr patel:

Table borders are not displayed. See the original doc.

You can set the table’s border by using Table.SetBorder method and CellFormat.Borders.Top/Left/Bottom/Right properties. Please check the following code snippet for your kind reference.

Document rtfDoc = RtfStringToDocument(rtfText.Trim());
foreach (Table table in rtfDoc.GetChildNodes(NodeType.Table, true))
{
    table.ClearBorders();
    // Set a green border around the table but not inside. 
    table.SetBorder(BorderType.Left, LineStyle.Single, 1.5, Color.Green, true);
    table.SetBorder(BorderType.Right, LineStyle.Single, 1.5, Color.Green, true);
    table.SetBorder(BorderType.Top, LineStyle.Single, 1.5, Color.Green, true);
    table.SetBorder(BorderType.Bottom, LineStyle.Single, 1.5, Color.Green, true);
    foreach (Cell cell in table.GetChildNodes(NodeType.Cell, true))
    {
        cell.CellFormat.Borders.Top.LineStyle = LineStyle.Single;
    }
}

Hi Tahir,

Thanks for your inputs.

I also noticed one more issue. Infact it looks like a MS word behavior. You can notice the same in out.docx file and rtfDocs.zip output files as well.

There is one char space at the begining of each rtf To Doc converted text. How can we remove this white space programatically from our code immidiately after execution of below line.

Document rtfDoc = RtfStringToDocument(rtfText.Trim());

Please see the attached 1.rtf.png file(1.rtf file was taken from rtfDocs.zip file) for issue description and assist me.

Hi there,

Thanks for your inquiry. The RTF contents in your input file (Input_File.rtf) contain the single space at the beginning of RTF (the text of first paragraph start with space character). So, the behavior of Aspose.Words is correct. However, you can remove this space character by using following code snippet.

Hope this helps you. Please let us know if you have any more queries.

Document rtfDoc = RtfStringToDocument(rtfText.Trim());
if (rtfDoc.FirstSection.Body.FirstParagraph.Runs[0] != null)
    rtfDoc.FirstSection.Body.FirstParagraph.Runs[0].Text = rtfDoc.FirstSection.Body.FirstParagraph.Runs[0].Text.Trim();

rtfDoc.Save(MyDir + "Out.docx");
InsertDocument(para, rtfDoc);