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

Free Support Forum - aspose.com

CR (\r) doesn't work in some cases

Dear Sir Or Madam,

I use \r to make a hard return. In some cases it works in some not. Please look into the attached document. Text in red contains squares for \r s and text in blue renders them properly. Moreover if I copy the text from first (red) paragraph and paste somewhere else in the document \r s will be rendered as expected. Could you please what’s the difference between these two paragraphs? Why is the first one shown incorrectly and second one looks right?

Regards,
Alex

Hi Alex,

Thanks for your inquiry. I have worked with your document and have not found any different in both paragraphs. In this case, I suggest you following workaround of this issue to get the required output. Hope this helps you.

Document doc = new Document(MyDir + "HardReturns.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    if (para.GetText().Contains("AASB"))
    {
        string str = para.GetText();
        builder.MoveTo(para);
        builder.Writeln("");
        break;
    }
}
doc.Save(MyDir + "Out.docx");

Hi Tahir,

Thank you for your reply. Unfortunately this workaround is not acceptable as the code we use for inserting runs and paragraphs in our document is pretty complex due to requirements. Isn’t it possible to find a root cause of the issue in order to avoid it in our code.

Regards,
Alex

Hi Alex,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. In your case, Aspose.Words reads the content correctly. MS Word behaviour is different for both paragraphs. You can confirm in “document.xml” of your document. See the attached image for detail.

You may try the following solution for this issue. Insert the paragraph break if a Run node’s text starts with ‘\r’. Hope this helps you.

Document doc = new Document(MyDir + "HardReturns.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    foreach (Run run in doc.GetChildNodes(NodeType.Run, true))
    {
        if (run.GetText().StartsWith("\r"))
        {
            builder.MoveTo(run);
            builder.InsertBreak(BreakType.ParagraphBreak);
            run.Text = run.Text.Replace("\r", "");
        }
    }
}
doc.Save(MyDir + "Out.docx");

Hi Tahir,

Thank you for your reply! Yep I tried something like this (.Replace("\r","\v")) but the problem is that with paragraph alignment = Justify it stretches the paragraph content to fit the line. Looks ugly for the small paragraphs.

Regards,
Alex

Hi Alex,

Thanks for your inquiry. Please use the following workaround to achieve the required output. Hope this helps you.

Document doc = new Document(MyDir + "HardReturns.docx");
MemoryStream stream = new MemoryStream();
doc.Save(stream, SaveFormat.Rtf);
stream.Position = 0;
Document doc2 = new Document(stream, new LoadOptions(LoadFormat.Rtf, "", ""));
doc2.Save(MyDir + "Out.docx", SaveFormat.Docx);