Run.Text does not Insert Line Break for "\r" in Table Cell using .NET

We would upgrade our Aspose version from 20.1 to 20.12 but we see that in 20.7 newline in a Run object not more is working.
What is the new solution to make a newline?

Thanks,
Matthijs

@GazpromEnergy

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

@tahir.manzoor

Hi Tahir,

I have upload a “test.zip” with the example. Also a output files from Aspose.Word 20.6 (correct) and Aspose.Word 20.12 (issue no enter)

Can you let me know that that you have enough information?

Thanks,

Matthijs
test.zip (92.6 KB)

The 2 output examples also added as pdf:
output with 20.6.pdf (41.7 KB)
output with version 20.12.pdf (41.7 KB)

Matthijs

For not needed to read the zip file also added the code from the test application:

using Aspose.Words;
using Aspose.Words.Saving;
using Aspose.Words.Tables;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = null;

            using (var fileStream = new FileStream("..\\..\\test.docx", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                doc = new Aspose.Words.Document(fileStream);
            }

            var table = GetTable(doc);

            var firstRow = table.Rows[0];

            var firstCell = firstRow.Cells[0];

            AddText(firstCell.Paragraphs, doc, "Hello\rAfter new line", false, false, 20);

            using (var fileStream = File.Create("..\\..\\out.pdf"))
            {
                var pdfSaveOptions = new Aspose.Words.Saving.PdfSaveOptions
                {
                    SaveFormat = Aspose.Words.SaveFormat.Pdf,
                    PreserveFormFields = true,
                    CustomPropertiesExport = PdfCustomPropertiesExport.None,
                    MemoryOptimization = true,
                };
                doc.Save(fileStream, pdfSaveOptions);
                fileStream.Flush();
                fileStream.Close();
            }
        }

        public static void AddText(ParagraphCollection paragraphCollection, Document document, string text, bool italics, bool bold, double? size)
        {
            RunCollection runCollection = paragraphCollection[paragraphCollection.Count - 1].Runs;
            Run run;
            if (runCollection.ToArray().Length > 0)
            {
                run = (Run)runCollection.ToArray().Last().Clone(true);
            }
            else if (paragraphCollection.Count > 1 &&
                     paragraphCollection[paragraphCollection.Count - 2].Runs.ToArray().Length > 0)
            {
                run = (Run)paragraphCollection[paragraphCollection.Count - 2].Runs.ToArray().Last().Clone(true);
            }
            else
            {
                run = new Run(document);
            }

            if (size.HasValue) run.Font.Size = size.Value;
            run.Font.Bold = bold;
            run.Font.Italic = italics;
            run.Text = text;
            runCollection.Add(run);
        }


        private static Table GetTable(Document doc)
        {
            foreach (Node node in doc.ChildNodes)
            {
                if (node is Section && ((Section)node).Body != null && ((Section)node).Body.Tables != null)
                {
                    return ((Section)node).Body.Tables[0];
                }
            }
            return null;
        }

    }
}

@GazpromEnergy

We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-21525. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

@tahir.manzoor

Thanks for reproduce the issue and open the issue on your tracking system.

Matthijs de Vree

@tahir.manzoor

We see that it is not fixed in 21.1, but if we use Aspose.Words.ControlChar.LineBreak it works. We hope this is resolved in 21.2, but for now we can use Aspose.Words.ControlChar.LineBreak.

Regards,
Matthijs

@GazpromEnergy

After the initial investigation, we come to conclusion that it is not a bug in Aspose.Words. You can replace ‘\r’ with ControlChar.LineBreak to get the desired output.

The issues you have found earlier (filed as WORDSNET-21525) have been fixed in this Aspose.Words for .NET 21.9 update.