PDF - How to make a text fill a rectangle

Example requests & Code samples for GdPicture Toolkits.
Post Reply
reisrf
Posts: 15
Joined: Thu May 28, 2015 4:30 pm

PDF - How to make a text fill a rectangle

Post by reisrf » Sun Oct 01, 2017 2:19 am

I have a PDF and I need to write a text inside a rectangle, but I want the letters of the text to fill the full rectangle area.

Please see the attachment for the example

How can I use the rectangle coordinates to calculate the SetTextSize and SetTextScalling parameters to be used?

Right now I am using

_gdPicturePDF.SetTextSize(bottom-top);
gdStatus = _gdPicturePDF.DrawText(_fontName, left, bottom, text );

But the letters are not filling all the space of the rectangle.

Any ideas?
e1.png
e1.png (10.92 KiB) Viewed 492 times

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

Re: PDF - How to make a text fill a rectangle

Post by delbeke » Tue Oct 10, 2017 10:41 am

Hi Reisfr

here is a small snippet with 2 methods to achieve the problem
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim oGdPicturePDF As New GdPicturePDF()
Dim status As GdPictureStatus = oGdPicturePDF.NewPDF()
If status = GdPictureStatus.OK Then
oGdPicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4)
oGdPicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter)
oGdPicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft)
'first way , set textSize to zero
'the DrawTextBox function will calculate a font size to fit the rectangle's width
Dim rectTop As Single = 10
Dim rectLeft As Single = 10
Dim rectWidth As Single = 130
Dim rectHeight As Single = 60
oGdPicturePDF.SetLineWidth(0.1)
oGdPicturePDF.DrawRectangle(rectLeft, rectTop, rectWidth, rectHeight, False, True)
Dim fntResName As String = oGdPicturePDF.AddTrueTypeFont("Arial", False, False, False)
oGdPicturePDF.SetTextSize(0)
oGdPicturePDF.DrawTextBox(fntResName, rectLeft, rectTop, rectLeft + rectWidth, rectTop + rectHeight, TextAlignment.TextAlignmentCenter, TextAlignment.TextAlignmentCenter, "Text sample")

'second way,
'search the font size to fit the height and then try changing the scaling to fit the width
rectTop = rectTop + 100
oGdPicturePDF.DrawRectangle(rectLeft, rectTop, rectWidth, rectHeight, False, True)
Dim fntSize As Single = 1
Dim textHeight As Single = 1
Do While textHeight < rectHeight
fntSize = fntSize + 1
textHeight = oGdPicturePDF.GetTextHeight(fntResName, fntSize, True)
Loop

Dim textWidth = oGdPicturePDF.GetTextWidth(fntResName, fntSize, "Text sample")
Dim textScaling As Single = 100 'default value
If textWidth < rectWidth Then
Do While textWidth < rectWidth
textScaling = textScaling + 1
oGdPicturePDF.SetTextScaling(textScaling)
textWidth = oGdPicturePDF.GetTextWidth(fntResName, fntSize, "Text sample")
Loop
ElseIf textWidth > rectWidth Then
Do While textWidth > rectWidth
textScaling = textScaling - 1
oGdPicturePDF.SetTextScaling(textScaling)
textWidth = oGdPicturePDF.GetTextWidth(fntResName, fntSize, "Text sample")
Loop
End If
oGdPicturePDF.SetTextSize(fntSize)
oGdPicturePDF.SetTextScaling(textScaling)
textHeight = oGdPicturePDF.GetTextHeight(fntResName, fntSize, True)
oGdPicturePDF.DrawText(fntResName, rectLeft, rectTop + (textHeight * 3 / 4), "Text sample")
oGdPicturePDF.SaveToFile("d:\pdfs\testTextSize.pdf")
MessageBox.Show("Done")
End If
End Sub
Hope this help you
Best regards
Jean-Luc

reisrf
Posts: 15
Joined: Thu May 28, 2015 4:30 pm

Re: PDF - How to make a text fill a rectangle

Post by reisrf » Wed Oct 11, 2017 3:37 pm

Thanks I will try soon. Just need to convert to C# .

reisrf
Posts: 15
Joined: Thu May 28, 2015 4:30 pm

Re: PDF - How to make a text fill a rectangle

Post by reisrf » Wed Oct 11, 2017 4:47 pm

Thanks for the snippet. It worked perfectly !!!!

Many thanks. Great support we get from GDPicture. As always!

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

Re: PDF - How to make a text fill a rectangle

Post by delbeke » Thu Oct 12, 2017 6:30 pm

Hi Reisrf

You are welcome
Thanks for the return.
Best regards
Jean-Luc

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests