Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
5

Resize automatically the height of a field according to the number of lines

Community Expert ,
Feb 23, 2024 Feb 23, 2024

Hi,

I am looking for resizing automatically the height of a field according to the number of lines with the fieldFull method.

Here is the script I tryed:

if (event.fieldFull) {
	//app.alert("The text field is full.\rWe are going to increase its height to be able to continue typing.");
	var nomChamp=event.target.name;
	this.resetForm([nomChamp]); // Reset field to lose focus
	var aRect=event.target.rect;
	aRect[3]=aRect[3]-event.target.textSize*1.16;
	event.target.rect=aRect;
	event.change=event.changeEx;
	this.getField(nomChamp).setFocus();
}

 That works approximatly because I lose the focus of the field and I need to type on this field to continue typing... Is there a way to avoid that?

P.S.: The 1.16 line spacing ratio is used for the Helvetica font.

TOPICS
JavaScript , PDF forms
4.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Feb 23, 2024 Feb 23, 2024

Nice one!

I was able to solve it by setting the focus to another field, and setting that field's On Focus event to return the focus to the original field right away. It works quite nicely! See attached.

Only issue I'm seeing is it doesn't make the field smaller when you back-space or delete the text you entered...

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 23, 2024 Feb 23, 2024

Nice one!

I was able to solve it by setting the focus to another field, and setting that field's On Focus event to return the focus to the original field right away. It works quite nicely! See attached.

Only issue I'm seeing is it doesn't make the field smaller when you back-space or delete the text you entered...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 23, 2024 Feb 23, 2024

Great! That works fine, thanks.

I had a first thought about how to resize the field when you delete some texts, but so far I haven't found...

I will continue to look for.

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 23, 2024 Feb 23, 2024

I believe it will require calculating the number of lines in the field, which is much more complicated, since the length of each character is different (unless it's a mono-spaced font)... but if you find a solution, please post it here!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 23, 2024 Feb 23, 2024

I did a solution resizing the field from a button, but it only works with Acrobat Pro as you must add it temporary add a new page, so you must add an application script:

if (app.formsVersion>=8) {
	ajoutPage=app.trustedFunction(function(cettePage,largeur,hauteur) {
		app.beginPriv();
		this.newPage(cettePage,largeur,hauteur);
		app.endPriv();
	});
	suppressionPage=app.trustedFunction(function(cettePage) {
		app.beginPriv();
		this.deletePages(cettePage);
		app.endPriv();
	});
}

See the attached file (in French) and if it interests you, tomorrow I will translate the script in English...

For the fieldFull method, I will look for...

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2024 Feb 27, 2024

Hi,

Here is a first working file I did rather quickly, but I am locked with few problems for a couple of days.

It seems that works pretty fine when modifications are done from the end of the text except when we add a new character just after the last one following the "fieldFull" flag.

Example in my attached file:

Capture d’écran 2024-02-27 à 18.56.57.pngexpand image

If I delete the "d" character

Capture d’écran 2024-02-27 à 18.57.11.pngexpand image

then I re-add it, the field becomes blank

Capture d’écran 2024-02-27 à 18.57.24.pngexpand image

while if you delete the "d" then the "l"

Capture d’écran 2024-02-27 à 18.58.05.pngexpand image

you can continue typing correctly

Capture d’écran 2024-02-27 à 18.58.20.pngexpand image

An other problem is when we add some charaters a the middle of the text field. At the next "fieldFull" flag the cursor comes back at the end of the text, so it's not possible to directly continue the typing...

 

If you have an idea!!!

Let me know if you think these are problems that can be solved...

I hope you will understand my logic!

Ps: You can take your time 😉 I will not able to take care of that for few days;

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2024 Feb 27, 2024

I will test it a bit more thoroughly (also need to understand exactly what all the variables mean), but I think the resetForm command is not needed. I commented it out and it seems to still work quite well, and the text doesn't disappear as it did in your example.

Also, I've noticed that sometimes the value of one of the items in longueursLignes is zero (after adding text and then deleting it). In that case you should remove it entirely from the array, so it would delete that line and resize the field accordingly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2024 Feb 27, 2024

In a few words:

Capture d’écran 2024-02-27 à 19.45.22.pngexpand image

longueurLignes is a table incremented after each "fieldFull" state that indicated the number of characters from the begining of the text until the end of each line.

ligneNo is the number of lines. It could be longueurLignes.length that would avoid ligneNo++ and ligneNo--.

reduireHauteur is a flag (0 or 1) that allows reducing the height of the field.

Take your time...

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2024 Mar 02, 2024
LATEST

Hi try67,

As you suggested I did a test removing the resetForm command, but that didn't work very well when the user write too quickly.... so I kept it.

I solved the first problem by reducing the number indicated for each line by 1 character. Indeed, when the fieldFull state is at 1 the maxi number of characters is the number preceding this change of state. This works well, but in this same case I still have a loss of the first character if the user types too quickly...

For the problem of modification in the middle of the text, I am still looking for a good solution. I had the idea to change "doNotScroll" to false if (event.selStart<leTexte.length && event.selEnd<leTexte.length) but that doesn't work very well... If you have an idea!

Attached is my last example file.

Thanks & @+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines