Text Width in Fields

Hello! I need to specify if string fits textfield. If not, I have to split my string into two strings, and the second string should be added to the next textfield. My questions are: is there an easy way to measure/predict string width in textfield, have You got an idea to solve my problem? I tried using MeasureString, but width was calculated incorrect. Thank You.

Hi Marcin,


Thanks for your inquiry. You can get maximum length of TextBoxField and customize your code accordingly. Hopefully it will help you to accomplish the task. However, it there is any difference in your understanding and my understanding then please share some more details for your requiements, your sample document and code will be appreciated.

int maxlen = textBoxField.MaxLen;

Please feel free to contact us for any further assistance.

Best Regards,

Thank You for reply. Unfortunately, MaxLen returns -1, that’s why I cannot use it in my code. I need to specify if whole string is too long for field. If it is, I have to split my string and fill next field. For example, if my first field can contain up to 15 characters, the second one can contain 20 characters and my string is “Hello World, nice to meet You” I should fill my first field with text “Hello World,” and next with “nice to meet You”. I attach my pdf.

Thank You.

Hi Macrin,

Thanks for your feedback. You are getting -1 length as Maximum length property of form fields is not set. You may use the above suggestion once the maximum length property of form fields is set. Please check the following code snippet for the purpose. Hopefully, it will help you to accomplish the task.

FormEditor form = new FormEditor();
form.BindPdf(myDir + "MEN-I_37_2.pdf");
form.SetFieldLimit("nazwa01", 15);
form.Save(myDir + "MEN-I_37_2_1.pdf");

Document doc = new Document(myDir + "MEN-I_37_2_1.pdf");
Console.WriteLine("Limit: " + (doc.Form["nazwa01"] as TextBoxField).MaxLen);
Console.WriteLine("Limit: " + (doc.Form["nazwa02"] as TextBoxField).MaxLen);

Please feel free to contact us for any further assistance.

Best Regards,

Thank You. The problem is that I have to specify about 20 documents containing 50 fields. Specifying MaxLen for every field would take too long time, because I also have to adjust font size in range from 6 to 10, so it’s about 5000 integers. The best way (in my opinion) is somehow specify the width of the rendered string and compare to the width of the field - textBoxField.Box.Width. The question is: is it possible in Aspose library to get rendered text width? Now, I use:

Font timesFont = new Font(“Times-Italic”, rozmiar);
Graphics g = Graphics.FromImage(new Bitmap(1, 1));
return g.MeasureString(tekst, timesFont).Width;

but it gives incorrect result.

Best Regards.

Hi Marcin,


Thanks for your feedback. We have already logged an enhancement ticket PDFNEWNET-38076 to get width of text. We have linked your post to the issue id and will notify you as soon as it is resolved.

However, please check this workaround shared by another community member, hopefully it will help you to accomplish task for time being.

We are sorry for the inconvenience caused.

Best Regards

The issues you have found earlier (filed as PDFNEWNET-38076) have been fixed in Aspose.Pdf for .NET 16.11.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.
(2)

I see there is a way to do this now that PDFNEWNET-38076 is implemented. I could not find a code example of how this is done. Could you share a snippet of this new functionality.

Hi,

Thanks for your inquiry. We have included two new functions for string width measuring into the public API. We can invoke MeasureString() method of Aspose.Pdf.Text.Font or Aspose.Pdf.Text.TextState classes (or both). Please check following code snippet for details.

Aspose.Pdf.Text.Font font = FontRepository.FindFont("Arial");
Console.WriteLine(font.MeasureString("Sample text", 14));

TextState ts = new TextState();
ts.Font = font;
ts.FontSize = 14;
Console.WriteLine(ts.MeasureString("Sample text"));

for (char c = 'A'; c <= 'z'; c++) {
    double fnMeasure = font.MeasureString(c.ToString(), 14);
    double tsMeasure = ts.MeasureString(c.ToString());
    Console.WriteLine(String.Format("{0} - font measure {1}", c, fnMeasure));
    Console.WriteLine(String.Format("{0} - text state measure {1}", c, tsMeasure));
}

Best Regards,