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

How to solve "User Cancelled File Select" error?

Community Beginner ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

Hi All,

Firstly I would like to say thank you for the people who gave me lots of help and advice regarding my previous questions in the forum. And I really learned a lot.

But there is still a question that I do not know how to figure it out.  According to Batch-Import List Data into PDF Form - KHKonsulting LLC

I try to modify the code but it always shows "User Cancelled File Select" error when I use custom command in Acrobat DC.

I only want to learn to add one field which is "Title" field to the fillable PDF form. But it doesn't work.

My data list is like(tab delimited text file): Only one row

111.png

My form is like: (fillable form)

333.png

My code is :


var dataFile = "/z/JavaScriptProject/form1.txt";

var tmpDoc = app.newDoc();
tmpDoc.addField("Title", "text", 0, [0, 0, 100, 100]);

var err = 0;
var idx = 0;
while (err == 0) {

  err = tmpDoc.importTextData(dataFile, idx);
   if (err == -1)

  app.alert("Error: Cannot Open File");
   else if (err == -2)

  app.alert("Error: Cannot Load Data");
   else if (err == 1)

  app.alert("Warning: Missing Data");
   else if (err == 2)

  app.alert("Warning: User Cancelled Row Select");
   else if (err == 3)

  app.alert("Warning: User Cancelled File Select");
   else if (err == 0) {

   var title = tmpDoc.getField("Title").value;
   this.getField("Title" + (idx + 1)).value = title;
   }

  idx++;
}

tmpDoc.closeDoc(true);
this.saveAs("/z/JavaScriptProject/temp1.pdf");

And it shows when I click the custom command created button:

222.png

I have no idea about how to solve it. Really need some help for that. Thank you for any advice.

TOPICS
Acrobat SDK and JavaScript , Windows

Views

3.4K

Translate

Translate

Report

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

correct answers 1 Correct answer

LEGEND , Apr 30, 2018 Apr 30, 2018

https://help.adobe.com/livedocs/acrobat_sdk/11/Acrobat11_HTMLHelp/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Acrobat1… This code puts the wrong meaning in error codes. Read documentation again. Also, print a count of rows found.

Votes

Translate

Translate
LEGEND ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

https://help.adobe.com/livedocs/acrobat_sdk/11/Acrobat11_HTMLHelp/wwhelp/wwhimpl/common/html/wwhelp.... This code puts the wrong meaning in error codes. Read documentation again. Also, print a count of rows found.

Votes

Translate

Translate

Report

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 Beginner ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

Thank you for your reply. So after I read this document, the error should be  "Error: Invalid Row", am I right?

And I'm sorry I don't understand what is "print a count of rows found" mean. Could you please give me some hints?  Thank  you.

Votes

Translate

Translate

Report

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 ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

Your text file has only one data row. You will get this error when you try reading a second data row.

And change line

   this.getField("Title" + (idx + 1)).value = title;

to

   this.getField("Title").value = title;

Votes

Translate

Translate

Report

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 Beginner ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

Hi Bernd Alheit,

I've changed the code as you said. But it still cannot run and show the error "Error: Invalid Row". I am feeling confused......I don't know where is the problem.

====================================================

var dataFile = "/z/JavaScriptProject/form1.txt";

var tmpDoc = app.newDoc();
tmpDoc.addField("Title", "text", 0, [0, 0, 100, 100]);
var err = 0;
var idx = 0;
while (err == 0) {

  err = tmpDoc.importTextData(dataFile, idx);

   if (err == -1)

  app.alert("Warning: User Cancelled File Select");
   else if (err == -2)

  app.alert("Warning: User Cancelled Row Select");
   else if (err == 1)

  app.alert("Error: Cannot Open File");
   else if (err == 2)

  app.alert("Error: Cannot Load Data");
   else if (err == 3)

  app.alert("Error: Invalid Row");
   else if (err == 0) {

 
   var title = tmpDoc.getField("Title").value;
   this.getField("Title").value = title;
   }

   idx++;
}

tmpDoc.closeDoc(true);
this.saveAs("/z/JavaScriptProject/temp1.pdf");

444.png

Votes

Translate

Translate

Report

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 ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

Did it fill the field?

Votes

Translate

Translate

Report

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 Beginner ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

No, the field is still blank. Do I need to add an "if" to end this loop like this:

var title = tmpDoc.getField("Title").value;
this.getField("Title").value = title;

=========================
if (idx == 2) {

   err = -99;
}

=========================

Votes

Translate

Translate

Report

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
LEGEND ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

The code you have written has a loop which will ONLY exit after an error message. I assume that's what you want. But you won't know if it has read any valid lines. So put an app.alert to tell you, after the loop, how many good lines you read. Normal debugging technique.

Votes

Translate

Translate

Report

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 Beginner ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

Currently, I only want to add one row and,  after that, save the pdf file. That's all. Do I need to add :

=============

if (idx == 0) {

   err = -99;
}

===========

if I only need one row to be filled and then finish this loop? A little confused...

Thank you. I will learn how to "print a count of rows found".

Votes

Translate

Translate

Report

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 ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

Based on your statement that you only want to fill the form with one row of data, and the way your form looks (from the limited screenshot in your question), I assume you are using the wrong approach. The blog post you referred to is about importing data into a table in your PDF file, you don't necessarily have table data, you have a "normal" form, so the approach outlined in Batch-Import Excel Data into PDF Forms - KHKonsulting LLC​ would be better suited for your form. What I said before about the data file and the form needing to match exactly holds still true. 

Votes

Translate

Translate

Report

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 Beginner ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

Hi Karl,

Thanks for providing me this link Batch-Import Excel Data into PDF Forms - KHKonsulting LLC

I am wondering that is it possible to put these code you provided(can work in custom command in Acrobat DC) into the document level JavaScript? If not, is there a method to do this?  I find your another post about this http://khkonsulting.com/2014/10/document-level-scripts-acrobat-standard/

Document Level Scripts With Acrobat Standard - KHKonsulting LLC and I've already installed the script. It seems there are some differences between "embedded scripts in document level JavaScript" and "put scripts into custom command in Acrobat DC".

Really appreciate.

Votes

Translate

Translate

Report

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 ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

Have you looked at the documentation for Doc.importTextData()? If not, please take a look at this: Acrobat DC SDK Documentation

You cannot program for Acrobat without a good understanding of the API documentation, which means that you cannot google your way to a working solution. When you look at the documentation for this call, you will find this:

"Note:(Acrobat 8.0) If cPath is specified, this method can only be executed during batch and console events. See Privileged versus non-privileged context for details. The event object contains a discussion of JavaScript events."

This means that as long as you want to specify the path of the data file in your code, you cannot use this method in a document level script. You can create  folder level script that provides a privileged function to populate the form, which you can call from your document level script, but you will not be able to do that without installing something in Acrobat first.

Votes

Translate

Translate

Report

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 Beginner ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

Hi Karl,

I just try to export the data from this one-page sample form, I've filled some data and saved already. This is a fillable form and only these fields below(pink color areas) can be filled.

123.png

After that, I need to export these data to a txt file to see how it looks just as you said "The best way to figure out what data columns you need is to fill your form with sample data and then export the data to a text field. This will show you how your data file needs to look. "

I followed the instructions from Collecting and managing PDF form data, Adobe Acrobat

234.png

After I click "prepare form" it will look like :

345.png

Then I exported the form to txt file. It looks like:

567.png

This file only has some numbers and row titles. But no filled data. I got confused because this form is absolutely filled already. Could you please give me any advice? Really thanks!

Votes

Translate

Translate

Report

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 ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

There are very complex field names in the form.

Votes

Translate

Translate

Report

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 Beginner ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

5678.png

Hi Karl,

I can see the data that I input right now, but it looks.....so irregular.

Votes

Translate

Translate

Report

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 ,
May 01, 2018 May 01, 2018

Copy link to clipboard

Copied

It does not have to be pretty for the computer to understand the data. As Bernd already commented on, it's the field names you are using. Is this a form you created? If so, you can simply the field names, if it's something you cannot change, then just assume that Acrobat will do the right thing when you feed it the correct data in the text file.

Votes

Translate

Translate

Report

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 Beginner ,
May 01, 2018 May 01, 2018

Copy link to clipboard

Copied

LATEST

OK, I got it. I will have a try to simply the field names. Thank you so much for your advice.

Votes

Translate

Translate

Report

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
LEGEND ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

as I said your original code will always finish with an error message (which may not mean an error, it’s your message). So a way to force the loop exit may be what you want, if your intention was different. 

Votes

Translate

Translate

Report

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 ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

You are referencing my sample, so let me jump in:

Have you downloaded the files from the same blog post where you found the sample code? If those work, then the code is correct (and yes, there may be a problem with the messages associated with the error codes, but the error codes listed in at least some versions of the SDK are incorrect as well).

My experience is that almost all problems are caused by a mismatch between form fields and data file. You need exactly the same number of columns with data in you text file as you have fields in your document. The data column names also have to match the field names exactly. If this is not the case, you will end up with errors. The best way to figure out what data columns you need is to fill your form with sample data and then export the data to a text field. This will show you how your data file needs to look. The order of the columns is not important, but everything else needs to match.

Votes

Translate

Translate

Report

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 Beginner ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

Hi Karl,

Thank you for your reply.

Yes, I've tested your sample and also downloaded the files(txt file and pdf file), it works.

The form is below. Just as the code I put above, what I need to do is only to fill one field, not all(e.g. Title field). So do I also need to fill every field on this page and export the data?

Another question is:

You said that "You need exactly the same number of columns with data in you text file as you have fields in your document." But in my case, this form is a little bit different from your sample form because the columns are not regular and atactic. So I don't know how to calculate the number of the columns. In your sample, the data is : one column has many data(fields), e.g. many email addresses under "email column", my case is each fillable blank only need to be filled with one field, e.g. only "Mr." is under "Title". 

555.png

Votes

Translate

Translate

Report

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 ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

Regardless of how many fields you want to fill with data, your data file needs to have data (which can be an empty string) for _ALL_ fields in the document. There is no way around that. Again, export data from your file to see what the file needs to look like.

Yes, your form is different, that's why I was referring you to a different post, which is for "normal" (and not table based) forms.

Votes

Translate

Translate

Report

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