PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Jul 09, 2024 12:13 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Tue Nov 20, 2012 1:27 am 
Offline

Joined: Tue Nov 06, 2012 10:43 pm
Posts: 7
Hello all,
I noticed there were some discussions on this in the past. How to display a text with line breaks,
for example "1\n2\n3\n4\n5", but did not find an answer. Is this possible at all in PDFSharp?
Thanks.
-Kaveh


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 20, 2012 9:30 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
Hi!
Use XTextFormatter class (and don't forget the '\n' in your strings).
viewtopic.php?p=6414#p6414

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 20, 2012 4:22 pm 
Offline

Joined: Tue Nov 06, 2012 10:43 pm
Posts: 7
Dear Thomas,
Thank you for the quick response. I actually saw a sample of XTextFormatter and tried it. Here is the part of the code with hard coded values:
..................
PdfDocument s_document = new PdfDocument();
PdfPage page = new PdfPage();
page = s_document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XStringFormat drawFormat = new XStringFormat();
drawFormat = XStringFormats.TopLeft;
XFont font = new XFont(node.Font.Name, node.Font.Size);
XTextFormatter tf = new XTextFormatter(gfx);
XRect rect = new XRect(62, 666, 449, 114);
Node.Text = "1\n2\n3\n4\n5\n6\n7\n8";
gfx.DrawString(node.Text, font, XBrushes.Black, rect, drawFormat);
.......................
The output comes in a rectangle as
1 2 3 4 5 6 7 8

So it seems that the \n is converted to tab or several spaces.


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 20, 2012 4:26 pm 
Offline

Joined: Tue Nov 06, 2012 10:43 pm
Posts: 7
Sorry, I forgot the Frawrectangle statement. Here is the corrected code again

PdfDocument s_document = new PdfDocument();
PdfPage page = new PdfPage();
page = s_document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XStringFormat drawFormat = new XStringFormat();
drawFormat = XStringFormats.TopLeft;
XFont font = new XFont(node.Font.Name, node.Font.Size);
XTextFormatter tf = new XTextFormatter(gfx);
XRect rect = new XRect(62, 666, 449, 114);
gfx.DrawRectangle(XBrushes.LightYellow, rect);
node.Text = "1\n2\n3\n4\n5\n6\n7\n8";
gfx.DrawString(node.Text, font, XBrushes.Black, rect, drawFormat);


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 20, 2012 5:34 pm 
Offline

Joined: Tue Nov 06, 2012 10:43 pm
Posts: 7
Problem sloved, thanks Thomas.

I had a mistake, I used
gfx.DrawString(node.Text, font, XBrushes.Black, rect, drawFormat);
instead of using the text formatter
tf.DrawString(node.Text, font, XBrushes.Black, rect, drawFormat);

Thank you very much for the assistance.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 28 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group