Hi,
I can draw a paragraph in pdf using following line
graphics.DrawString(txt, font, brush, new RectangleF(0, graphics.ClientSize.Height - 160, graphics.ClientSize.Width, 140), format);
my paragraph is set at the bottom of the page but above some text
Here I've to specify height of rectangle so the paragraph fits in it properly.
Is there any method to get the space (height) required by the paragraph if I know the with in which it need to be drawn
Hope you understood my problem
after trying so much things, here is how I'm able to calculate height of rectangle required to fit long string.
Here MaxWidth is allowable maxWidth
private float SizeOfText(PdfFont font, string txt, float MaxWidth)
{
float requiredHeight = 0;
SizeF size = font.MeasureString(txt);
string[] strs = txt.Split(new string[] { " " }, StringSplitOptions.None);
int LastIndex = 0;
for(int i = 0; i < strs.Length; i++)
{
if (i > 0)
{
string[] tstr = new string[i + 1 - LastIndex];
Array.Copy(strs, LastIndex, tstr, 0, tstr.Length);
if(font.MeasureString(string.Join(" ", tstr)).Width > MaxWidth)
{
requiredHeight += size.Height + 1;
LastIndex = i;
}
}
}
//Add Last Unfinished Line
if (LastIndex < strs.Length + 1) requiredHeight += size.Height + 1;
return requiredHeight;
}
Posting here thinking this might help someone in desperate need
Thanks for your help & sorry for utilizing your valuable time.
Amit
Hi Amit,
Thank you for your update. We are glad to know that the reported issue is resolved. We can be able to find the height of the paragraph using MeasureString API in PdfFont Class.
Please follow the below links for more information,
https://www.syncfusion.com/kb/893/how-to-measure-a-text-to-draw-in-pdf-using-c-and-vb-net
https://help.syncfusion.com/file-formats/pdf/working-with-text#measuring-a-string
Please let us know if you need any further assistance in this.
Regards,
Irfana J.
Thanks for your reply,
I tried MeasureString API in PdfFont Class & it is useful to calculate height of string that fits in single line.
If string is multiline then it is useless.
Code I provided is to calculate height of multiline string & it uses same MeasureString API in PdfFont Class.
Please check with your team regarding the same.
My problem as mentioned above was to know the height of rectangle required to fit the provided string (it can be single line or multi line)
Anyway thanks for your help
Amit
Hi Amit,
Thank you for your update. We are glad to know that the reported issue is resolved.
Regards,
Gowthamraj K