facebooktwittermenuarrow-up

GemBox Support Forum

Text Linebreak not working anymore

Hello,

before we were using Gembox.Document version 25.3.30.1255.
now we use 33.0.0.1390.

we have text in a document like “foo\r\nfoo2”.
in the older version, this would translate to a linebreak when generating a pdf.
in the new version, that doesn’t work anymore.

we also tried just using “\n” with the new version.

any ideas on how to fix that?

Hi,

Can you tell us exactly how we can reproduce your issue?

The line breaks should still work in the latest version.
I’m not sure how I can recreate your problem.

Regards,
Mario

it took me a while to reproduce the problem in a test project.
please take a look at the file below:

  1. run both projects separately
  2. open the pdf files in the output/debug folder (test_edited.pdf)

MiscConsoleNET (gembox 25.*) produces the following result:
image

MiscConsole (gembox 33.*) however doesn’t do the line breaks

they both use exactly the same code.

MiscConsole (gembox 33.*) result:
image

Hi,

I see now, you’re using System.IO.Packaging to modify the DOCX file’s internal XML.

Try saving that modified document stream into a DOCX file, for example:

// ...

// flush and return
package.Close();
documentStream.Position = 0;

// Save the modified "documentStream" as "modified.docx" file.
File.WriteAllBytes("modified.docx", ((MemoryStream)documentStream).ToArray());

var document = GemBox.Document.DocumentModel.Load(documentStream, GemBox.Document.LoadOptions.DocxDefault);

Now open that “modified.docx” file in Microsoft Word, you’ll see this:

modified-word

In other words, the interpretation of those newline characters in the older version of GemBox.Document was wrong. As you can see from the above screenshot, Microsoft Word doesn’t treat them as new lines. The newer versions of GemBox.Document have a fix for this.

Anyway, what you need to do is instead of using the ‘\n’ character use the <w:br/> XML element.
So, instead of this line of code:

placeholder.Element(W.sdtContent).Descendants(W.t).Single().Value = contentDic[tag];

You would need these lines of code:

var textElement = placeholder.Element(W.sdtContent).Descendants(W.t).Single();
var replaceTextElements = new XRaw(string.Join("<w:br/>",
    contentDic[tag].Split('\n').Select(line => $"<w:t>{line}</w:t>")));
textElement.ReplaceWith(replaceTextElements);

Also, here is the XRaw element that I’m using in those lines:

public class XRaw : XText
{
    public XRaw(string text) : base(text) { }
    public XRaw(XText text) : base(text) { }
    public override void WriteTo(XmlWriter writer) =>  writer.WriteRaw(this.Value);
}

Last, in case you’re interested, to achieve this with just GemBox.Document is much simpler than this.
All you need to do is this:

using System.Collections.Generic;
using GemBox.Document;
using GemBox.Document.CustomMarkups;

class Program
{
    static void Main()
    {
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
        TestGemboxDocument();
    }

    static void TestGemboxDocument()
    {
        const string inputFile = "test.docx";
        string outputFile = "test_edited";

        var document = DocumentModel.Load(inputFile);
        var contentDic = new Dictionary<string, string>
        {
            ["variable"] = "foo\vbreak\vfoo2"
        };

        foreach (IContentControl placeholder in document.GetChildElements(true,
            ElementType.InlineContentControl, ElementType.BlockContentControl))
        {
            string tag = placeholder.Properties.Tag;
            if (tag == null || !contentDic.TryGetValue(tag, out string value))
                continue;
            placeholder.Content.LoadText(value, placeholder.Properties.CharacterFormat);
        }

        bool saveAsPDF = true;
        if (saveAsPDF)
            document.Save(outputFile + ".pdf");
        else
            document.Save(outputFile + ".docx");
    }
}

One difference you should note, I replaced the ‘\n’ characters with ‘\v’ because they represent different things.
The first one is a hard break or the paragraph’s end, you get that when using ENTER in Microsoft Word.
The second one is a soft break or the line break character, you get that when using SHIFT + ENTER in Microsoft Word.

I hope this helps, let me know if you have any questions.

Regards,
Mario

that works for me.
the manual approach here is because it’s legacy code, so i won’t be able to replace it easily with the newer gembox features, which would be more convenient of course.

thanks