PDF - How to make a text fill a rectangle
PDF - How to make a text fill a rectangle
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?
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?
Re: PDF - How to make a text fill a rectangle
Hi Reisfr
here is a small snippet with 2 methods to achieve the problem
Best regards
Jean-Luc
here is a small snippet with 2 methods to achieve the problem
Hope this help youPrivate 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
Best regards
Jean-Luc
Re: PDF - How to make a text fill a rectangle
Thanks I will try soon. Just need to convert to C# .
Re: PDF - How to make a text fill a rectangle
Thanks for the snippet. It worked perfectly !!!!
Many thanks. Great support we get from GDPicture. As always!
Many thanks. Great support we get from GDPicture. As always!
Re: PDF - How to make a text fill a rectangle
Hi Reisrf
You are welcome
Thanks for the return.
Best regards
Jean-Luc
You are welcome
Thanks for the return.
Best regards
Jean-Luc
Who is online
Users browsing this forum: No registered users and 1 guest