Bold text with DrawTextBox

Discussions about PDF management.
Post Reply
puiguija
Posts: 5
Joined: Thu Dec 20, 2012 12:04 pm

Bold text with DrawTextBox

Post by puiguija » Tue Jun 10, 2014 10:26 am

Hi,

I need to write text with DrawTextBox method but part of this text must be in bold. Bold text is variable in length because comes from user input, i.e:

This is a sample text with bold words.

Any help would be appreciated.

Thanks,

SamiKharma
Posts: 352
Joined: Tue Sep 27, 2011 11:47 am

Re: Bold text with DrawTextBox

Post by SamiKharma » Tue Jun 10, 2014 10:46 am

Hi,

You can easily do that, all you have to do is set the resource font you want as bold, for example, create a font using https://www.gdpicture.com/guides/gdpicture/GdP ... FontU.html
and mark the Bold parameter as true.

Then use that font in DrawTextBox.

Hope this helps.
Best,
Sami

puiguija
Posts: 5
Joined: Thu Dec 20, 2012 12:04 pm

Re: Bold text with DrawTextBox

Post by puiguija » Tue Jun 10, 2014 10:51 am

Hi,

Thanks for the quick answer, but your proposal writes all text in Bold whereas I need to write just some words in bold.

Other option would be to combine several calls to DrawTextBox with different fonts but then I would need to know the x,y coordinates where to place the next word.

Regards,

delbeke
Posts: 89
Joined: Wed Oct 31, 2012 5:07 pm

Re: Bold text with DrawTextBox

Post by delbeke » Tue Jun 10, 2014 12:30 pm

Hi Puiguija

You can use the function GetTextWidth() to know the width taken by a text in a specified font.

See this simple sample in VbNet to how you can use it

Code: Select all

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Pdf.NewPDF()
    Pdf.NewPage(PdfPageSizes.PdfPageSizeA4)
    Dim fntRegular As String = Pdf.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica)
    Dim fntBold As String = Pdf.AddStandardFont(PdfStandardFont.PdfStandardFontHelveticaBold)
    Pdf.SetOrigin(PdfOrigin.PdfOriginTopLeft)
    Pdf.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter)
    Dim PosX As Double
    Dim PosY As Double
    PosX = 10
    PosY = 10
    PosX = DrawText(fntRegular, PosX, PosY, 10, "This text is regular.")
    PosX = DrawText(fntBold, PosX, PosY, 10, "This text is bold.")
    PosX = DrawText(fntRegular, PosX, PosY, 10, "This text is regular.")
    Pdf.SaveToFile("d:\test.pdf")
End Sub

Private Function DrawText(ByVal FntId As String, ByVal PosX As Double, ByVal PosY As Double, ByVal FntSize As Double, ByVal Text As String)
    Pdf.SetTextSize(FntSize)
    Pdf.DrawText(FntId, PosX, PosY, Text)
    Return PosX + Pdf.GetTextWidth(FntId, FntSize, Text)
End Function
Hope this help

Jean-Luc

puiguija
Posts: 5
Joined: Thu Dec 20, 2012 12:04 pm

Re: Bold text with DrawTextBox

Post by puiguija » Thu Jun 12, 2014 11:58 am

Thanks delbeke, that helped me a lot.

delbeke
Posts: 89
Joined: Wed Oct 31, 2012 5:07 pm

Re: Bold text with DrawTextBox

Post by delbeke » Thu Jun 12, 2014 9:15 pm

You are wellcome
Jean-Luc

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest