PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Apr 26, 2024 7:16 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Wed May 28, 2014 6:30 pm 
Offline

Joined: Tue Apr 01, 2014 7:33 pm
Posts: 19
I have this code that allows bold and italics text to be displayed on my pdf without having the hmtl tags display. however when i try to draw the string i notice that the text is overlapping eachother, it looks like the bold and italics are getting written on top of the regualr text and tha tis causing some issues. does anyone have some advice or the correct code to have these different text appear one after another instead of on top of eachother? here is the code that aloows bold and italic and also draws the string. i really just need the text to display on one line or word wrap if it is a longer line. thanks so much for any help or support.

protected int lineHeight = 10;
public static int margin = 20;
public static int indentedMargin = margin + 20;

var graphics = XGraphics.FromPdfPage(page);
var currentTop = 10;
var currentLeft = margin;
var page = document.AddPage();
var font2 = new XFont("Arial", 9, XFontStyle.Regular);



public static void DrawDescription(string description, XGraphics graphics)
{

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(description);

var count = 0;

foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//span"))
{
bool hasBold = false;
bool hasItalic = false;
string text = link.InnerText;
if (link.Attributes["style"].Value.Contains("bold"))
{
hasBold = true;
}
if (link.InnerHtml.Contains("<i>"))
{
hasItalic = true;
}

var font2 = GetFont(hasBold, hasItalic);
var document = new PdfDocument();
var page = document.AddPage();
var lineheight = 10;

graphics.DrawString("" + text, font2, XBrushes.Black, new XRect(margin + 90, page.Height - (lineheight * 37), page.Width, page.Height), XStringFormats.TopLeft);

}
}

private static XFont GetFont(bool hasBold, bool hasItalic)
{
XFont font;
if (hasBold && hasItalic)
font = new XFont("Verdana", 10, XFontStyle.BoldItalic);
else if (hasBold)
font = new XFont("Verdana", 10, XFontStyle.Bold);
else if (hasItalic)
font = new XFont("Verdana", 10, XFontStyle.Italic);
else
font = new XFont("Verdana", 10, XFontStyle.Regular);
return font;
}


Top
 Profile  
Reply with quote  
PostPosted: Thu May 29, 2014 11:12 pm 
Offline

Joined: Thu May 29, 2014 5:05 pm
Posts: 5
I posted a similar issue about how font selection is not working correctly inside PdfSharp library. The issue has to do with how the font names are being constructed. PdfSharp needs to use the font names read from the font data file instead of some artificially created names. Try the solution I proposed below to see if it helps with your situation.

viewtopic.php?f=3&t=2826


Top
 Profile  
Reply with quote  
PostPosted: Fri Jun 06, 2014 5:43 pm 
Offline

Joined: Tue Apr 01, 2014 7:33 pm
Posts: 19
Unfortunetly that doesnt seem to fix the problem. Would you have any other ideas? I just need to place the text in the correct order, because im assuming the way I defined what is bold and italic is just getting placed on top each other instead of in a proper order. thanks


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 10, 2014 8:17 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
adandrea wrote:
however when i try to draw the string i notice that the text is overlapping eachother, it looks like the bold and italics are getting written on top of the regualr text and tha tis causing some issues.
The second text should be drawn behind the first text. I do not see code that updates the left position of the text.

Why don't you just use MigraDoc? MigraDoc will do the line breaks and page breaks automatically.
Add an empty paragraph with "AddParagraph()" and then call "AddFormattedText(...)" with the formats you need.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 10, 2014 6:28 pm 
Offline

Joined: Tue Apr 01, 2014 7:33 pm
Posts: 19
I was able to add in the word wraping to the fields i needed but i am still having issue with the text overlapping...can you give me an example of what i am missing "updates the left position of the text.
" its been hard for me to get a good grasp on all this pdfsharp stuff so any more advice or support would be great.. i am just hoping for a simple solution to allow all text to display in the right order and look normal.

thanks so much


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 11, 2014 7:34 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Suppose you want to write "normal1 italic normal2" in one line.
"normal1" starts at x position 20 mm. When you write "italic" also at x position 20 mm, those words will overlap.
The correct x position of "italic" will be 20 mm plus width of "normal1" plus width of " ".

See method "DemonstrateFormattedText(Document document)" to let MigraDoc make the calculations for you:
http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashx

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 11, 2014 6:30 pm 
Offline

Joined: Tue Apr 01, 2014 7:33 pm
Posts: 19
will this still work if i have a different description field of text each time? just not sure how you can define something to go after a word if the words and formatting will always be changing? does that make sense?


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 12, 2014 8:01 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Suppose you want to write "normal1 italic normal2" in one line.
"normal1" starts at x position 20 mm. When you write "italic" also at x position 20 mm, those words will overlap.
The correct x position of "italic" will be 20 mm plus width of "normal1" plus width of " ".

For flexibility, determine the width of "normal1" and " " at runtime.
That's what XTextFormatter does (without support for bold and italics), that's what MigraDoc does (with support for bold and italics).

If you write the text at hard-coded positions (such as 20 mm, 50 mm, 80 mm) then changing text might lead to gaps or overlapping.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Jun 13, 2014 12:33 am 
Offline

Joined: Tue Apr 01, 2014 7:33 pm
Posts: 19
Thomas Hoevel wrote:
Suppose you want to write "normal1 italic normal2" in one line.
"normal1" starts at x position 20 mm. When you write "italic" also at x position 20 mm, those words will overlap.
The correct x position of "italic" will be 20 mm plus width of "normal1" plus width of " ".

For flexibility, determine the width of "normal1" and " " at runtime.
That's what XTextFormatter does (without support for bold and italics), that's what MigraDoc does (with support for bold and italics).

If you write the text at hard-coded positions (such as 20 mm, 50 mm, 80 mm) then changing text might lead to gaps or overlapping.



would this be placed on the drawstring line or be define seperatly? not really understanding where this needs to make place and how to determine the layout of this. for example drawstring place bold after italics...any more advice or code sample would be really helpful

thank u


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 16, 2014 7:36 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Let MigraDoc do the work (see DemonstrateFormattedText method):
http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashx

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 18, 2014 11:18 pm 
Offline

Joined: Tue Apr 01, 2014 7:33 pm
Posts: 19
are you saying that I can just add my above code to the DemonstrateFormattedText method ? anyway you can give me a little code sample of how to set up?

for example i just need to format the text for this description field which can change content/text formatting at any time. would i change paragraph to description and then hope that the add formatted text field finds the correct place to put it ??

Paragraph paragraph = document.LastSection.AddParagraph();

paragraph.AddText("Text can be formatted ");

paragraph.AddFormattedText("bold", TextFormat.Bold);

paragraph.AddText(", ");

paragraph.AddFormattedText("italic", TextFormat.Italic);

paragraph.AddText(", or ");

paragraph.AddFormattedText("bold & italic", TextFormat.Bold | TextFormat.Italic);

just not sure where to start and want to make sure im on the right track...any advice or more sample would be great..thanks so much!


Top
 Profile  
Reply with quote  
PostPosted: Fri Jun 20, 2014 9:39 pm 
Offline

Joined: Tue Apr 01, 2014 7:33 pm
Posts: 19
Thomas Hoevel wrote:
Let MigraDoc do the work (see DemonstrateFormattedText method):
http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashx


can you please explain to me how i would use this migradoc sample if my description field is going to have different text and formatting each time a user changes the description field. it looks to me like this sample is formatting a never changing text document. is that correct or is it possible to format the text not knowing what description can be picked ?


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 298 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group