9

I am using the Novacode DocX c# library to create word documents and have run into a problem. I want my paragraphs to 'keep together' at page breaks. But, I also want to use soft returns to force my pictures to display vertically between lines of text.

So my question is, how do I add soft returns within paragraphs?

Hicham Bouchilkhi
  • 682
  • 10
  • 29
  • By "soft returns", do you mean the vertical tab character (ASCII 11, i.e. x0B)? (Apparently `"\v"` in C# syntax.) – YowE3K May 30 '17 at 22:21
  • @YowE3k: In Word, when you press the 'enter' key a 'hard return' is added to the doc and the current 'paragraph' (Word concept) is ended. However if you instead press the 'shift + enter' combination, something similar to a 'newline' is added and the 'paragraph' is not ended. Hard returns end paragraphs. – Darin Sease May 31 '17 at 16:19
  • OK - so your "soft return" is a vertical tab character. I believe that is a `"\v"` in C#. – YowE3K May 31 '17 at 16:26
  • @YowE3k, Before I try to figure out how to implement your suggestion I'm going to hold out for a method call that maybe I have overlooked. – Darin Sease Jun 01 '17 at 00:29
  • @YowE3k, (ignore the above), Before I try to figure out how to implement your suggestion I'm going to hold out for a method call that maybe I have overlooked. But while were on the subject: It is my understanding that \n = newline (which causes the cursor to move down to the next line and position itself at the beginning of the screen). Are you saying that \v functions the exact same way?. – Darin Sease Jun 01 '17 at 00:38
  • No, a newline (assuming that that is an ASCII 10, i.e. x0A) would start a new paragraph. An ASCII 11 is the equivalent of a Shift-Enter. So the C# string `"aaaaa\nbbbbb\vccccc"` would (I believe) leave you with two paragraphs, one saying `"aaaaa"` and the other `"bbbbb/ccccc"` (where the "/" signifies a line break - bit hard to show in a comment) – YowE3K Jun 01 '17 at 00:48
  • And, FWIW, the XML generated by Word for the example I gave in the previous comment was `aaaaabbbbbccccc` so, in the XML file Word produces, it converts the vertical tab to a ``. – YowE3K Jun 01 '17 at 01:12
  • See also https://stackoverflow.com/q/24193619/6535336 – YowE3K Jun 01 '17 at 01:16
  • @YowE3k: Thanks for the help. Turns out it was'nt the the '\v' I needed but the '\n' ascii 10. Insert it into a string and that translates into a soft return. I got locked into thinking there was a method call that I could not find. Thanks again. – Darin Sease Jun 06 '17 at 05:14
  • I'm surprised that the `\n` works - that seemed to just generate a normal paragraph break when I used the equivalent in VBA. But as long as you have worked out what you need to get it to do what you want, that's all that matters. (And, you never know, the bounty I offered might yet get you a better answer.) – YowE3K Jun 06 '17 at 07:34

2 Answers2

1

You can try to add one of those unicode char at the end of your paragraph, for instance :

using (DocX document = DocX.Create(@"docs\myDoc.docx"))
{
    Paragraph p = document.Paragraphs[0];
    p.Append("\u000D");
    p.Append("\u000A");
}

Tell me if it solves your problem

G.Dealmeida
  • 325
  • 3
  • 14
0

I solved this with:

using (DocX document = DocX.Create(@"docs\myDoc.docx"))
{
    Paragraph p = document.Paragraphs[0];
    p.Append("\u000D");
}

Note: this is adapted from @G.Dealmeida's answer. If you include the additional \u000A then you get a second line break.

ChrisFox
  • 346
  • 4
  • 11