0

In the code, I generate formatted text into a WPF RichTextBox. I use Environment.NewLine to insert line break. Everything looks fine. However, when I copy the contents of a RichTextBox to Clipboard (using RichTextBox context menu) and paste it into Word or WordPad, line breaks disappear. However, if I paste the contents of the clipboard as plain text (e.g. into notepad), the new lines do not disappear. Don't know why this is so?

Update:

// _richTextBox is my object RichTextBox
var doc = _richTextBox.Document;
var paragraph = doc.Blocks.FirstOrDefault() as Paragraph;
if (paragraph == null) return;

var span = new Span();
span.Inlines.Add(new Underline(new Run($"Header: {Environment.NewLine}")));
span.Inlines.Add(new Run("Text content"));

paragraph.Inlines.Add(span);
Clyde
  • 311
  • 2
  • 14
  • Not sure, but is it because of `\r\n` vs `\n` issue? As per https://learn.microsoft.com/en-us/dotnet/api/system.environment.newline?view=net-6.0, for Windows it's `\r\n`, for Unix it's `\n`. – kiner_shah Jan 19 '22 at 09:20
  • Maybe you can provide some code. Also, check this https://stackoverflow.com/a/4624392/4688321. – kiner_shah Jan 19 '22 at 09:24
  • Environment.NewLine inserts on my OS (Win 10) `\r\n`, which should be correct. – Clyde Jan 19 '22 at 09:26
  • @kiner_shah I added code for you. I also looked at the recommended post. Unfortunately, this does not solve my problem. I'd like to know why the native RichTextBox copy-to-clipboard function doesn't insert line breaks when pasted into a Word document. – Clyde Jan 19 '22 at 09:52
  • It doesn't seem to be a [mre] as there are some parts missing e.g. what is `doc`? Where are you copying to clipboard, etc? – kiner_shah Jan 19 '22 at 09:54
  • @kiner_shah I am sorry . What is doc I added. As I already wrote, I copy to the clipboard using the control's native functions - I select the mouse text in the control or use Ctr+A, then mouse right-click and select Copy from the context menu. Is this sufficient or is some information still missing? – Clyde Jan 19 '22 at 09:59
  • I found a link (may not solve your issue but worth reading): https://stackoverflow.com/a/18966530/4688321 – kiner_shah Jan 19 '22 at 10:09

2 Answers2

1

The solution was simple in the end. I didn't notice that there was a LineBreak object. If you use LineBreak instead of \r\n (Environment.NewLine), then line break will work in both plain text and formatted text.

Clyde
  • 311
  • 2
  • 14
0

I am working with RichTextBox which client must be copy-paste with Word application. To my surprise, the Environment.NewLine did not work. You have to insert paragraph.Inlines.Add( new LineBreak( ) );

Here is complete summary when copy paste from RichTextBox to each application:

enter image description here

  1. The wrong-est \n not working in any application, unless it is newline aware such as Notepad++

         var doc = m_RichTextBox.Document;
         doc.Blocks.Clear( );
         var p = new Paragraph( );
         doc.Blocks.Add( p );
    
         p.Inlines.Add( new Run( "Line1\n" ) );
         p.Inlines.Add( new Run( "Line2\n" ) );
         p.Inlines.Add( new Run( "Line3\n" ) );
    
  2. The Environment.NewLine Notepad says ok, but Word still decided to protest.

         /* Same setup code omitted .... */
         p.Inlines.Add( new Run( "Line1" + Environment.NewLine ) );
         p.Inlines.Add( new Run( "Line2" + Environment.NewLine ) );
         p.Inlines.Add( new Run( "Line3" + Environment.NewLine ) );
    
  3. The correct one

         /* Same setup code omitted .... */
         p.Inlines.Add( new Run( "Line1" ) );
         p.Inlines.Add( new LineBreak( ) );
         p.Inlines.Add( new Run( "Line2" ) );
         p.Inlines.Add( new LineBreak( ) );
         p.Inlines.Add( new Run( "Line3" ) );
         p.Inlines.Add( new LineBreak( ) );
    
Wappenull
  • 1,181
  • 13
  • 19