This is a migrated thread and some comments may be shown as answers.

[Solved] RadDocument PDF - newlines

4 Answers 279 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Justin Lee
Top achievements
Rank 1
Justin Lee asked on 17 Oct 2011, 09:45 PM

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

4 Answers, 1 is accepted

Sort by
0
Grégory
Top achievements
Rank 1
answered on 19 Oct 2011, 07:15 PM
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.
0
Justin Lee
Top achievements
Rank 1
answered on 19 Oct 2011, 07:48 PM
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
0
Accepted
Iva Toteva
Telerik team
answered on 20 Oct 2011, 05:16 PM
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 >>

0
Justin Lee
Top achievements
Rank 1
answered on 20 Oct 2011, 05:23 PM
Perfect!  That's exactly what I was looking for.  Thanks!
Tags
General Discussions
Asked by
Justin Lee
Top achievements
Rank 1
Answers by
Grégory
Top achievements
Rank 1
Justin Lee
Top achievements
Rank 1
Iva Toteva
Telerik team
Share this question
or