RadDocument PDF - newlines

5 posts, 1 answers
  1. Justin Lee
    Justin Lee avatar
    176 posts
    Member since:
    Dec 2009

    Posted 17 Oct 2011 Link to this post

    Hi,
    I'm using a RadDocument to export some text to pdf.  I am dynamically building sections and paragraphs, and have noticed odd behavior with newlines.  I've created a simple sample to demonstrate.

    Section section = new Section();
    Paragraph paragraph = new Paragraph();
     
    paragraph.Inlines.Add(new Span() { Text = "Line 1" });
    paragraph.Inlines.Add(new Span() { Text = "\rLine 2\r\r" });
    paragraph.Inlines.Add(new Span() { Text = "Line 3" });
    paragraph.Inlines.Add(new Span() { Text = "\rLine 4" });
    section.Blocks.Add(paragraph);
    document.Sections.Add(section);

    This code should produce:
    Line 1
    Line 2

    Line 3
    Line 4

    However, when exported to pdf, there is not linebreak between "Line 3" and "Line 4".  Instead it puts them on the same line, and puts 2 squares between them like this:

    Line 1
    Line 2

    Line3 􀀀􀀀Line 4

    Please let me know if there is a work around.

    Thanks,
    Justin

  2. Grégory
    Grégory avatar
    21 posts
    Member since:
    Mar 2011

    Posted 19 Oct 2011 Link to this post

    Hello,

    I have done this by spliting the paragraph:
    Paragraph paragraph2 = new Paragraph();         
    section.Blocks.Add(paragraph2);             
    Paragraph paragraph3 = new Paragraph();             
    section.Blocks.Add(paragraph3);

    Every new paragraph is a new line inside document.
  3. Justin Lee
    Justin Lee avatar
    176 posts
    Member since:
    Dec 2009

    Posted 19 Oct 2011 Link to this post

    Thanks for the tip.  I have considered that, however my senario is I retrieve the text from a database where it is stored with the newlines in the text.  I can split the text by the line breaks, and put each part into its own paragragh like this:

    string[] parts = text.Split('\r');
    foreach (string p in parts)
    {
        Paragraph p = new Paragraph();
        p.Inlines.Add(new Span() { Text = p });
        section.Blocks.Add(p);
    }

    However I'm already splitting the text by something else (there are special characters in it that represent headers)  This makes the code somewhat complicated, and it would be nice for the paragraph to handle newlines correctly.

    If I don't hear back from Telerik with a simple workaround like replacing \r with \r\n (which doesn't work), I will have to bite the bullet and rework the code to put everything in separate paragraphs. But I'm hoping I don't have to do that.

    Justin
  4. Answer
    Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 20 Oct 2011 Link to this post

    Hi Justin,

    Carriage Returns ("\r") should not be inserted in the document by being passes in the Text properties of Spans. This would not only cause the issue you have observed, but will prevent the proper functioning of some commands and can result in exceptions being thrown.

    However, there is a simple fix that should be just what you are looking for and that is to replace all occurrences of "\r" with .Replace("\r", FormattingSymbolLayoutBox.LINE_BREAK) like this:

    Span span2 = new Span() { Text = "\rLine 2\r\r".Replace("\r", FormattingSymbolLayoutBox.LINE_BREAK) };

    I hope this helps.

    Greetings,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  5. Justin Lee
    Justin Lee avatar
    176 posts
    Member since:
    Dec 2009

    Posted 20 Oct 2011 Link to this post

    Perfect!  That's exactly what I was looking for.  Thanks!
Back to Top