0

I need to show an address on DOCX invoice, we generate to our customers using OOXML SDK. Invoice's header has a table with a cell, allocated to the address.

The address has been kept in SQL as a separate column per each line and on the invoice each line shall be presented as a new one, e.g.

Science Museum, London
Exhibition Rd, Kensington
London
SW7 2DD

Sadly, no matter what I try, I always get it all in one line, so the invoice can be seen as

Science Museum, London Exhibition Rd, Kensington
London SW7 2DD

For that purpose I have a StringBuilder (tried a simple string as well to no avail)

StringBuilder headerAddress = new StringBuilder();

which I populate. I have tried many ways to force the break line, e.g. \r\n, Environment.NewLine or + (char)13 + (char)10. None has worked. Below is my code's sample. For ease of reading I have shown all three options I have tried at once, yet in my real code I have tried all three separately.

Can one advise what I am doing wrong, please?

protected override void OnLeaveRow()
        {
            if (_AccountInvAddress.AddressLine1 != "")
                _parent.headerAddress.Append(_AccountInvAddress.AddressLine1.Trim() + "\r\n");
            if (_AccountInvAddress.AddressLine2 != "")
                _parent.headerAddress.Append(_AccountInvAddress.AddressLine2.Trim() + "\r\n");
            if (_AccountInvAddress.AddressLine3 != "")
                _parent.headerAddress.Append(_AccountInvAddress.AddressLine3.Trim() + Environment.NewLine);
            if (_AccountInvAddress.AddressLine4 != "")
                _parent.headerAddress.Append(_AccountInvAddress.AddressLine4.Trim() + Environment.NewLine);
            if (_AccountInvAddress.AddressLine5 != "")
                _parent.headerAddress.Append(_AccountInvAddress.AddressLine5.Trim() + (char)13 + (char)10);
            if (_AccountInvAddress.AddressLine6 != "")
                _parent.headerAddress.Append(_AccountInvAddress.AddressLine6.Trim() + (char)13 + (char)10);
            if (_AccountInvAddress.AddressLine7 != "")
                _parent.headerAddress.Append(_AccountInvAddress.AddressLine7.Trim() + (char)13 + (char)10);

        }
KDWolf
  • 398
  • 2
  • 14
  • 1
    You can use [`AppendLine()`](https://msdn.microsoft.com/en-us/library/ebk18257(v=vs.110).aspx) – maccettura Mar 06 '18 at 16:17
  • 1
    Why not `AppendLine`? – Johnny Mar 06 '18 at 16:17
  • 1
    Side note, it is usually better to use `string.IsNullOrEmpty` instead of `!= ""` – FCin Mar 06 '18 at 16:18
  • @FCin I prefer [`IsNullOrWhiteSpace()`](https://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace(v=vs.110).aspx) myself – maccettura Mar 06 '18 at 16:19
  • 2
    `I need to show an address on DOCX invoice, we generate to our customers using OOXML SDK. Invoice's header has a table with a cell, allocated to the address` <= maybe that is a part of the problem, that line breaks are not shown in the document or location of the document where the text is being outputted to. – Igor Mar 06 '18 at 16:20
  • 1
    Maybe OOXML needs html breaks `
    ` instead of line breaks? http://officeopenxml.com/WPtextSpecialContent-break.php
    – juharr Mar 06 '18 at 16:22
  • You don't use newlines in XML content - they are treated as whitespace. You'd have to HTML encode special characters and insert eg ` &`. There *are* no newlines in Word though, there are *paragraphs* and *line breaks*. Dont try to manipulate the raw content, use the SDK itself to create a new paragraph, run, range, whatever and set its contents – Panagiotis Kanavos Mar 06 '18 at 16:32
  • If you insist on manipulating the raw content, [check the schema first](http://officeopenxml.com/WPtextSpecialContent-break.php). The `` element is a range that can contain line breaks, ``. – Panagiotis Kanavos Mar 06 '18 at 16:39
  • You can cheat too. Create a word document the way you like it and *unzip it into a folder*. `docx` is a zipped package of XML files. Inspect the files to see how paragraphs, breaks etc are represented. Another nice trick is to create a file as template, including, eg mail merge fields or placeholders where you want to insert your own strings. Open the file and replace the merge fields with your own. – Panagiotis Kanavos Mar 06 '18 at 16:40
  • Yes, we do use docx template, which has a table in it and I need to place the address in the correct cell of that table. I will follow the various comments here. Thank you for the help – KDWolf Mar 06 '18 at 17:04

0 Answers0