chrisfcarroll / MailMerge

Merge component for OfficeOpenXml .docx (i.e. Microsoft Word) documents
GNU General Public License v3.0
19 stars 7 forks source link

Having problem when having multiple line text #2

Closed apumex closed 4 years ago

apumex commented 4 years ago

I have a address field and its a free text with line breaks. Currently everything is displayed as a single line.

I was able to write a function which is getting the desired result, but I am not sure if this is the right approach. If not will be possible to share snippet or update the library

   private static void SetWordXmlNodeText(XmlDocument mainDocumentPart, XmlNode node, 
    string newText)
    {
        //Is the text a single line or multiple lines?>
        if (newText.Contains(System.Environment.NewLine))
        {
            //The new text is a multi-line string, split it to individual lines
            var lines = newText.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            //And add XML nodes for each line so that Word XML will accept the new lines
            for (int count = 0; count < lines.Length; count++)
            {
                var replacementNode = mainDocumentPart.CreateElement("w", "t", OoXmlNamespaces.Instance["w"]);
                replacementNode.InnerText = lines[count];
                node.ParentNode.AppendChild(replacementNode);
                if (count != lines.Length - 1)
                {
                    var brNode = mainDocumentPart.CreateElement("w", "br", OoXmlNamespaces.Instance["w"]);
                    node.ParentNode.AppendChild(brNode);
                }

            }

            //Remove the single line child node that was originally holding the single line text, only required if there was a node there to start with
            node.ParentNode.RemoveChild(node);
        }
        else
        {
            //Text is not multi-line, let the existing node have the text
            node.InnerText = newText;
        }
    }
chrisfcarroll commented 4 years ago

Hey, sorry for really slow reaction. Yes that looks about right. Do you want to do a pull request? Otherwise I can probably work on it in a couple of days

chrisfcarroll commented 4 years ago

Fixed in 2.3.0

apumex commented 4 years ago

Sorry, I too missed out on this message.

I see you have already made the changes. Thanks for supporting me. Will test in our template and will share the results.

Wanted to say a special thank you for building this package. We were able to use this package in 2 different projects already.

Meanwhile we have another requirement in which we need to list all the merge fields defined in the doc. For now we have done this based on the existing code base. I am kind of certain that there would be a better way of doing this. So it would be helpful if we can have this feature too.

Thanks Abraham Wilson

On Mon, Jul 20, 2020 at 11:42 PM Chris F Carroll notifications@github.com wrote:

Closed #2 https://github.com/chrisfcarroll/MailMerge/issues/2.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/chrisfcarroll/MailMerge/issues/2#event-3567169724, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA4WDVQNBOA3GBYZ2PWMSKDR4SCH5ANCNFSM4M4NNQGA .