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

Action manager: Add script to change initial view

Community Beginner ,
Jun 27, 2022 Jun 27, 2022

Copy link to clipboard

Copied

Hi there,

 

I'm using a content management systems to create PDF files. These can either be single-page manuals or printed double-page quick guides.

Problem: By default all PDFs are opened in single-page view. I'd like to provide comfort to the user and thus include a custom meta information regarding the initial view:

document-defaultview = single-page

or

document-defaultview = double-page

 

Now, my problem is the realization in the action manager. My best bet would be to include some JavaScript within the PDF that sets the initial view.

When I ran those commands in the JavaScript console they worked fine.

this.zoom=100;
this.viewState = {overViewMode:3};
this.viewState = {pageViewLayoutMode:4};

Now, the one-million-dollar-question is: How do I attach these to the PDF that's being processed?

I stored those in variables for easier handling:

var setzoomscript = "this.zoom=100;";
var setoverviewmodescript = "this.viewState = {overViewMode:3}";
if(initialview="single-page"){
    var setviewscript = "this.viewState = {pageViewLayoutMode:2};"
}else{
    var setviewscript = "this.viewState = {pageViewLayoutMode:4};"

I've found out that there is a addScript()-method to do this, but I am lacking the knowledge to assemble a working command here.

This didn't work: this.addScript(setzoomscript, setoverviewscript, setviewscript);

When I open the modified PDF, the settings are not applied. Can someone help?

 

Thanks!

TOPICS
Edit and convert PDFs , How to , JavaScript

Views

608

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

Community Expert , Jun 27, 2022 Jun 27, 2022

This code will not work (correctly), because it contains errors. You didn't define the initialview variable anywhere, and when you're trying to compare it to a string you're using the wrong operator, whereby you're assigning a value to it, not comparing the two.

Votes

Translate

Translate
Community Beginner ,
Jun 27, 2022 Jun 27, 2022

Copy link to clipboard

Copied

Sorry, just read through it and maybe it's not really clear: The metadata is already included in the PDF, I just need to process the PDF depending on them.

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 ,
Jun 27, 2022 Jun 27, 2022

Copy link to clipboard

Copied

Figured it out, a case of RTFM (although I didn't find much info on addScript).

I had a typo and added a = between this.addScript and the (.

This works:

/* stores view scripts to variables */
var setzoomscript = "this.zoom=100;";
var setoverviewmodescript = "this.viewState = {overViewMode:3}";
if(initialview="single-page"){
var setviewscript = "this.viewState = {pageViewLayoutMode:2};"
}else{
var setviewscript = "this.viewState = {pageViewLayoutMode:4};"
}

/* adds view scripts to PDF */
this.addScript("Initial view settings", setzoomscript, setoverviewmodescript, setviewscript);

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 ,
Jun 27, 2022 Jun 27, 2022

Copy link to clipboard

Copied

This code will not work (correctly), because it contains errors. You didn't define the initialview variable anywhere, and when you're trying to compare it to a string you're using the wrong operator, whereby you're assigning a value to it, not comparing the two.

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 ,
Jun 27, 2022 Jun 27, 2022

Copy link to clipboard

Copied

Hi there,

 

thanks for your feedback. It's not the complete code, sorry for that. Currently the complete code looks like this.

/* PDF preparation for upload except compression*/

/* Assumption: PDF was previously compressed */

/* reads document creation date */
var creationdate = this.info["creationdate"];

/* convert creationdate to ISO-String */
creationdateISO=creationdate.toISOString();

/* extracts year from creationdate-ISO */
var creationyear = creationdateISO.substr(0,4);
/* extracts month from creationdate */
var creationmonth = creationdateISO.substr(5,2);
/* extracts day from creationdate */
var creationday = creationdateISO.substr(8,2);

/* rearranges date */
creationdatefinal = creationday+"."+creationmonth+"."+creationyear;

/* reads meta document-newfilename */
var newfilename = this.info["document-newfilename"];

/* replaces INSERTDATE with creationdatefinal */
var newfilename=newfilename.replace(/INSERTDATE/i, creationdatefinal);
this.info["document-newfilename"]=newfilename;

/* changes document-status to upload_web */
this.info["document-status"] = "upload_web";

/* reads desired initial view from metadata */
var initialview=this.info["document-defaultview"];

/* stores view scripts to variables */
var setzoomscript = "this.zoom=100;";
var setoverviewmodescript = "this.viewState = {overViewMode:3}";
if(initialview="single-page"){
    var setviewscript = "this.viewState = {pageViewLayoutMode:2};"
}else{
    var setviewscript = "this.viewState = {pageViewLayoutMode:4};"
}

/* adds view scripts to PDF */
this.addScript("Initial view settings", setzoomscript, setoverviewmodescript, setviewscript);

/* Saves PDF in same directory with document-newfilename as filename */
this.saveAs(this.path.replace(this.documentFileName, newfilename + ".pdf"));

I guess you're right regarding the operator, I can remember that from other programming languages. It should have been ==, not =. I'll fix that.

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 ,
Jun 27, 2022 Jun 27, 2022

Copy link to clipboard

Copied

You don't need to reinvent the wheel, you can duplicate and customize this Preflight fixup.
It can then be used in an Action or in a Droplet.

 

Capture_518.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 Beginner ,
Jun 27, 2022 Jun 27, 2022

Copy link to clipboard

Copied

This method can't automatically change it's output in relation to single-page oder double-page. Thus, it's not suitable for my purposes, sorry.

Plase correct me if I am wrong, though.

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 ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

LATEST

I don't know what criteria you use for this but an Action and a droplet can sort files in a "success" folder and in a "error" folder.

 

Capture_521.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