Copy link to clipboard
Copied
I have a form that is two related but separate report forms. Both forms are usually, but not always, filled in together, with some information common to both forms. The advantage to having both forms together in a single PDF is that the person filling out the form does not have to enter information common to both forms more than once. There are also Invoice Templates so the user can generate either one invoice or two separate invoices.
Each form has Template pages that are added as needed. The format of the PDF is:
Report 1
Additional Comments (Template)
Photos (Template)
Invoice (Template)
Report 2
Additional Comments (Template)
Photos (Template)
Invoice (Template)
When I spawn Template pages, the spawned pages all go to the end of the document. I need the Templates for Report 1 to immediately follow Report 1 and the Templates for Report 2 to immediately follow Report 2. The Photo Page Templates are usually replicated anywhere from two to 12 times for each report. It is very important to have the Photo pages with thier respective report, not all together at the end of the document.
I also need to set the security to allow filling the form, etc. so that the users of the form cannot modify the form or have access to the JavaScript. I am able to get the pages to spawn to where I need them if the security is off or set to "Any except extracting pages", but not at any othr security level.
I think I know what the problem is with what I was doing and the security. Not realizing the security limitation, I generated a new page, then overlayed the spawned page onto it. The reason I did that was that I could not figure out how to get the spawned pages to spawn to a particular location without them being overlays. My solution worked until I set the security. Apparently, it is “this(newPage(currentpage+1)” that is causing the problem (I think that's what the problem is, but I'm not sure?). Also, I looked at the Adobe JavaScript Reference. It only shows examples of spawned template pages being overlays. This is the JavaScript I used:
// Get the template name
var templateName = "PhotosWind";
// Get the current page number
var currentPage = this.pageNum;
// Insert a blank page after the current page
this.newPage(currentPage+1);
// Overlay the template page on the new blank page
this.spawnPageFromTemplate(templateName, currentPage+1, true);
Acrobat generate an error saying that security setting will not allow that. When that didn’t work, I tried this:
var templateName = "PhotosWind";
var currentPage = this.pageNum;
this.getTemplate(templateName).spawn({currentPage:+1, bOverlay: false, bRename: true});
That puts the spawned page at the end of the document instead of the page following the current page (where the button to spawn the page is located). So, I am back to my original problem of not being able to spawn a page to a particular location.
Any help will be greatly appreciated. Thanks for taking the time to read this!
Copy link to clipboard
Copied
Ok so first, add a hidden form field next to the button that runs the spawn code.
Name the field "SpawnPage".
Add this code to the beginning of the script that does the spawning. It gets the next page value
var nSpPage = this.getField("SpawnPage").value;
var nCurrentPage = event.target.page;
if(!isNaN(nSpPage) && (nSpPage > nCurrentPage ))
nCurrentPage = nSpPage ;
Then add this code to the end of the script that does the spawning. It saves the next page value
this.getField("SpawnPage").value = nCurrentPage + 1;
Copy link to clipboard
Copied
Pages are spawned at page number specified in the spawn function.
This code is wrong:
.spawn({currentPage:+1, bOverlay: false, bRename: true});
"currentPage" is not an argument name for the spawn function. The name of the argument is "nPage"
Both of these lines are correct and do exactly the same thing.
.spawn({nPage:currentPage+1, bOverlay: false, bRename: true});
.spawn(currentPage+1, false, true);
Copy link to clipboard
Copied
Thanks Thom. I tried the code that you provided. It puts the spawned Template pages in the proper location, but creating multiple copies using a button-click to add each copy results is the field names all being the same on each copy. If I use a simple Spawn command without any arguments, the copies are all at the end of the document, but all have unique field names. Do you know how I can have the spawned pages follow the page with the button and have each copy have unique field names? Thanks again for your help.
Copy link to clipboard
Copied
Yes, put the new spawned page after the last spawned page. To do this you'll need to know where the last page was spawned. There are a few ways to do this.
A simple one is to keep track of the last spawned page in a form field. To start out the page number field value should be 0 or empty. If its empty or zero the script uses the page number of the spawn button, but if it's a real page number, then the script uses the field value.
Copy link to clipboard
Copied
Thanks Thom, I understand your logic, but the challenge for me is the actual JavaScript code. I've been through the Adobe JavaScript Reference and other sources for help, but this is just confounding me. I'm not a programmer. I'm trying to teach myself JavaScript. I've been able to figure out most things so far, but a few things like this one have me stumped. If you could provide an example, I'd greatly appreciate it. Thanks again for the help.
Copy link to clipboard
Copied
Ok so first, add a hidden form field next to the button that runs the spawn code.
Name the field "SpawnPage".
Add this code to the beginning of the script that does the spawning. It gets the next page value
var nSpPage = this.getField("SpawnPage").value;
var nCurrentPage = event.target.page;
if(!isNaN(nSpPage) && (nSpPage > nCurrentPage ))
nCurrentPage = nSpPage ;
Then add this code to the end of the script that does the spawning. It saves the next page value
this.getField("SpawnPage").value = nCurrentPage + 1;
Copy link to clipboard
Copied
That worked! Thank you Thom!! I really appreciate your help! You'd laugh if I showed you the convoluted JavaScript I came up with, but that didn't work anyway.
Copy link to clipboard
Copied
I for the life of me can't get this to work.
I have the following script:
var nSpPage = this.getField("SpawnPage").value;
var nCurrentPage = event.target.page;
if(!isNaN(nSpPage) && (nSpPage > nCurrentPage ))
nCurrentPage = nSpPage ;var a = this.getTemplate("Additional Signers 1");a.spawn();this.getField("SpawnPage").value = nCurrentPage + 1;
When I click the button it is still generating the new page after the last page of the document. I'm not sure what I'm doing wrong.
Copy link to clipboard
Copied
Sorry, wrong post.
--------------------------------------------------