Using JS to get username in a Text form

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

PDGBeton
User
Posts: 4
Joined: Wed Jan 11, 2023 12:11 pm

Using JS to get username in a Text form

Post by PDGBeton »

I have a problem I can't fix and hoping someone here could help me.

So I have a text form that I want to automatically fill with the username of the person who opens the document. I tried to do this with JavaScript. The code that came closest is:

Code: Select all

this.getField('Text2').value = identity.name;
The problem with this is that it fills in my name, but when I send it to my coworker it doesn't change the name to his but it keeps mine instead. And I seem to not be able to get this to work like I want to, so any help would be appreciated.


Some snippets of stuff I also tried, but that didn't work:

Code: Select all

//var usrName = Current.User.Identity.Name;
//this.getField('Text3').value = (name = usrName);

//Backendless.UserService.getCurrentUser()

//let userAgent = navigator.userAgent;
//let username = userAgent.split("\\")[1]

//const os = require("os");
//const username = os.userInfo().username;
//this.getField('Text3').value = username;

//app.host;

//const os = require("os");
//const username = os.userInfo().identity.name;
//this.getField('Text2').value = identity.name;
Last edited by PDGBeton on Wed Jan 11, 2023 12:45 pm, edited 1 time in total.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

Re: Using JS to get username in a Text form

Post by Stefan - PDF-XChange »

Hello PDGBeton,

Are you creating a dynamic stamp with that field? It will show a preview that might indeed show your name, but when the stamp is used - it should then update to the actual user's name when placing the stamp.

If it is a general field inside a fillable PDF form - then please consider adding your JS to the document level scripts and executing that on file load - that way - when the file loads on a new machine - the field will be auto populated with the then current username value.
However that might then be a problem if the form is not "printed" to a less dynamic version/paper - as when you then receive and open that file on your end - you will get the name to update once again :)

Kind regards,
Stefan
PDGBeton
User
Posts: 4
Joined: Wed Jan 11, 2023 12:11 pm

Re: Using JS to get username in a Text form

Post by PDGBeton »

No it's not for a stamp as I made a document that is pre-made for like 25 people, apart from their name (obviously). So what I did was make a text field and using the Value Calculation option to make it run a JavaScript.
then please consider adding your JS to the document level scripts and executing that on file load
So I'm guessing I have to do it like that, but I don't know how to do that, yet. Seeing as I'm new to this program. Do you have some reading material at hand to point me in the right direction?
However that might then be a problem if the form is not "printed" to a less dynamic version/paper - as when you then receive and open that file on your end - you will get the name to update once again
Would there be a way around that? Like if I could say it should only change if xchange finds our our license key?



I also forgot to mention that I tried the code

Code: Select all

var userName = context.getCurrentUserName();
console.log("The current user's name is: " + userName);
But I get the error that context is not defined.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

Re: Using JS to get username in a Text form

Post by Stefan - PDF-XChange »

Hello PDGBeton,

To get the name you need just:

Code: Select all

var userName = identity.name;
That other code you have tried does not seem to be what PDF files use as JS - and JS is case sensitive so e.g. "identity.name" is different than "identity.Name"

Using the console in the Editor you can usually use console.println("..."); - to output your data:
image.png
As for adding the script to the document level - please press Ctrl + D - this will open the Document Properties Windwo, and on the left you can click "JavaScript" and then add your document level scripts:
image(1).png
And when you close and reopen your file - the script does get executed.


Please find attached a sample document with such a document level script that checks if the field is currently empty, and if it is - populates it with the user name. If the field is not empty - it stays as it is.


Kind regards,
Stefan
You do not have the required permissions to view the files attached to this post.
PDGBeton
User
Posts: 4
Joined: Wed Jan 11, 2023 12:11 pm

Re: Using JS to get username in a Text form

Post by PDGBeton »

Thank you, I'll be trying it tomorrow. Have a nice day.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

Re: Using JS to get username in a Text form

Post by Stefan - PDF-XChange »

Hello PDGBeton,

I did update the above post - and provided a sample. Please take a look at it - as it will likely clarify what you need to do with your own project file.

Kind regards,
Stefan
PDGBeton
User
Posts: 4
Joined: Wed Jan 11, 2023 12:11 pm

Re: Using JS to get username in a Text form

Post by PDGBeton »

Sorry for the late reply, for some reason I have a lot of trouble logging in to the site. It keeps kicking me.

But my issue here is fixed, so thank you for your help.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

Re: Using JS to get username in a Text form

Post by Stefan - PDF-XChange »

Hello PDGBeton,

Happy to hear that the original issue is fixed, and as for the logging in problems - I've not heard anyone else complaining, but if you do keep getting such - you can try to e-mail us to support@pdf-xchange.com and we will be happy to assist you there.

Kind regards,
Stefan