2

I am programmatically creating a FlowDocument, then translating it to Rich Text, so a user can edit it in a Rich Text box. I cannot figure out how to insert a horizontal line in the FlowDocument that will display across the whole width of the edit box (and subsequent rendered PDF).

I found this thread which (allegedly) shows how to do exactly what I want, in XAML:

Simple (I think) Horizontal Line in WPF?

And this one:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/bcd334c7-8e2b-4e59-a344-4376b30cf847/

I have attempted to replicate this programmatically, like this:

        Line pLine = new Line();
        pLine.Stretch = Stretch.Fill;
        pLine.Stroke = Brushes.Black;
        pLine.X2 = 1;
        para.Inlines.Add( pLine );

But, this ends up displaying nothing at all in the resulting RTF edit box. The rest of the Rich Text is there. Just no line at all.

I also tried creating a Table and inserting the text I want to come after the horizontal line into a cell. Then I set a custom border, just for the top of the Table. That gives me a horizontal line, but it doesn't go all the way across the screen (after it's converted to RTF and shown in the RTF edit box), and it doesn't show at all after I render it into a PDF.

I have resorted to the complete hack of just inserting a Run of 60 underscore ('_') characters and a LineBreak. That "works", but it sucks. Can anybody tell me how to do this correctly?

Community
  • 1
  • 1
StuartV
  • 109
  • 1
  • 10

2 Answers2

1

i am using this little monster to insert a line at the current cursor position in a RichTextBox.

var ruler = new Line { X1 = 0, Y1 = 0, X2 = 1000, Y2 = 0, Stroke = new SolidColorBrush(Colors.Black), StrokeThickness = 2 };

new InlineUIContainer(ruler, richtextbox.CaretPosition.GetInsertionPosition(LogicalDirection.Forward));

I think you forgot to set the StrokeThickness. And even if you set Stretch.Fill your line is only one device independent unit long.

Otto Gierling
  • 359
  • 1
  • 8
  • 1
    I used the above example to display the vertical line in a `ScrollViewer`, and was able to send it to the printer successfully...Here is what I used: `Line ruler = new Line { X1 = 0, X2 = 10, Y1 = 0, Y2 = 0, Stroke = new SolidColorBrush(Colors.Black), StrokeThickness = 2 }; ruler.Stretch = Stretch.Fill; InlineUIContainer container = new InlineUIContainer(ruler); flowDocument.Blocks.Add(new Paragraph(container));` OR you can set the `X2` value to `99999`, and that should go across the screen automatically. If you do that, you don't need the `Stretch.Fill`. – Marwan مروان Aug 08 '13 at 23:36
1

My experience with Otto's method was that it draws a nice horizontal line in the editor but the line is not saved in the RTF file. My handy little O'Reilly "RTF Pocket Guide" yielded a block of RTF that does both. Here is the click handler for my "Insert Line Break" toolbar button...

    private void InsertLineBreak_Click(object sender, RoutedEventArgs e)
    {
        MemoryStream stream = 
            new MemoryStream(ASCIIEncoding.Default.GetBytes(@"{\pard\brdrb\brdrs\brdrw10\brsp20\par}"));
        rtbEditor.Selection.Load(stream, DataFormats.Rtf);
    }