Jump to content

Adding pages to make output PDF divisible by 4


CCS_Printing

Recommended Posts

What I need is a bit more complex than the title suggests. There are two separate sections in this book where the client can choose 1 to 5 optional pages. Each new section needs to start on the left, so if an uneven number of pages is chosen a filler page specific to that section will need placed. After that has happened the entire book (which will always be an even number of pages at this point) will need two specific pages placed at then end if it's not divisible by four for saddle stitch.

 

I've poured over the forums and other samples I have, but just can't quite get it working. Here are some helpful posts I referenced:

 

Inserting Multi-page PDF variable graphics

 

Print page depending on even or odd pages

 

I want to make an array for each of the two sections, count those pages and activate an unused body page if the count is odd. I'm using this in an OnRecordStart rule. Here's the code I've been trying to massage, taken and modified from a sample file:

 


var counter = 0;

   PAGEArray = [Field("FundCare_Orange1"), Field("Dental_Orange2"), Field("FlexSpend_Orange3"), Field("PlansWork_Orange4"), Field("HowToHSA_Orange5"), Field("ClientUpload_Orange")]



       var PAGECount = 0;
       for (i = 0 ; i < PAGEArray.length ; i++)
       {
           if (PAGEArray[i] != "")
           {
           counter++;
           var Pic = new FusionProResource(PAGEArray[i], "graphic", "true");
           var PicPages = Pic.countPages;

           if (PicPages % 2 > 0)
                   {
                        FusionPro.Composition.SetBodyPageUsage(“Orange_Filler1”, "true");
                   }            
           }
       }

But that just activates the unused body page anytime the array is true (not empty), and I can't seem to get the countPages aspect to work. When I use Print("PAGECount is " + PAGECount) and view the log, I simply get PAGECount is 0 repeated for each of the fields in the array that's populated.

 

As far as counting the pages of each complete record and activating the two unused body pages at the end based on whether or not it's divisible by four, I was thinking along the same lines but am not sure how to include the pages from the base template in the count, the pages that aren't part of a graphic field.

 

Any help is really appreciated, I always try to research and resolve things on my own as it really helps me learn, but I need some tutelage here and I'm running out of time. Thanks in advance!

 

OSX 10.10.5

Acrobat 10.1.16

FusionPro 9.3.15

Edited by CCS_Printing
Add software info
Link to comment
Share on other sites

What I need is a bit more complex than the title suggests. There are two separate sections in this book where the client can choose 1 to 5 optional pages. Each new section needs to start on the left, so if an uneven number of pages is chosen a filler page specific to that section will need placed.

After that has happened the entire book (which will always be an even number of pages at this point) will need two specific pages placed at then end if it's not divisible by four for saddle stitch.

Okay. How many pages is the book if the client does not select any additional pages in your two sections? I'm going to assume that it's a number divisible by 4.

 

I want to make an array for each of the two sections, count those pages and activate an unused body page if the count is odd. I'm using this in an OnRecordStart rule. Here's the code I've been trying to massage, taken and modified from a sample file:

 


var counter = 0;

   PAGEArray = [Field("FundCare_Orange1"), Field("Dental_Orange2"), Field("FlexSpend_Orange3"), Field("PlansWork_Orange4"), Field("HowToHSA_Orange5"), Field("ClientUpload_Orange")]



       var PAGECount = 0;
       for (i = 0 ; i < PAGEArray.length ; i++)
       {
           if (PAGEArray[i] != "")
           {
           counter++;
           var Pic = new FusionProResource(PAGEArray[i], "graphic", "true");
           var PicPages = Pic.countPages;

           if (PicPages % 2 > 0)
                   {
                        FusionPro.Composition.SetBodyPageUsage(“Orange_Filler1”, "true");
                   }            
           }
       }

But that just activates the unused body page anytime the array is true (not empty), and I can't seem to get the countPages aspect to work.

That's a good start. I'm going to assume that each of those fields in the PAGEArray represent 1 of 5 potential pages. If that's the case, then that would explain why your "Orange_Filler1" page is being turned on when the page count is odd (or 1 in this case). Moreover, if each PDF resource referenced in those fields is 1 page, then there's really no point in counting the pages or even converting it to a FusionPro Resource for that matter. Instead you could filter the array and determine the page count by the length of the array like this:

var docs = [Field("FundCare_Orange1"),Field("Dental_Orange2"),Field("FlexSpend_Orange3"),Field("PlansWork_Orange4"),Field("HowToHSA_Orange5"),Field("ClientUpload_Orange")].filter(String);
FusionPro.Composition.SetBodyPageUsage("Orange_Filler1", docs.length % 2);

 

When I use Print("PAGECount is " + PAGECount) and view the log, I simply get PAGECount is 0 repeated for each of the fields in the array that's populated.

Well that's because you defined PAGECount as 0 and never incremented it. In order to make your Print statement work, you'd have to make the following revisions:

PAGEArray = [Field("FundCare_Orange1"), Field("Dental_Orange2"), Field("FlexSpend_Orange3"), Field("PlansWork_Orange4"), Field("HowToHSA_Orange5"), Field("ClientUpload_Orange")]

var PAGECount = 0;
for (i = 0 ; i < PAGEArray.length ; i++)
{
   if (PAGEArray[i] != "")
   {
   var Pic = new FusionProResource(PAGEArray[i], "graphic", "true");
   var PicPages = Pic.countPages;
   [color="Red"]PAGECount += PicPages;[/color]
   if (PicPages % 2 > 0)
           {
                FusionPro.Composition.SetBodyPageUsage("Orange_Filler1", true);
           }            
   }
}

Print("PAGECount is " + PAGECount)

As far as counting the pages of each complete record and activating the two unused body pages at the end based on whether or not it's divisible by four, I was thinking along the same lines but am not sure how to include the pages from the base template in the count, the pages that aren't part of a graphic field.

Are you saying you don't know how many pages are in your template? Maybe you could try this:

var endPages = [
   // These are the names of the pages that will be
   // turned on to make the totalPage count == 4
   'FillerPage1',
   'FillerPage2',
   'FillerPage3'
];
var pagesNeeded = 4-(FusionPro.Composition.totalPages%4 || 4);
for (var i = 0; i < pagesNeeded; i++)
   FusionPro.Composition.SetBodyPageUsage(endPages[i], true);

 

And if you feel like collecting your template or posting a sample, that would probably clear up a lot of the things I just guessed on. Good luck!

Link to comment
Share on other sites

This is fantastic Step, thank you! There are actually 14 pages that will always be there, so if they don't add any optional sections or upload a section the two end filler pages will be added. Unfortunately I can't upload the file as is due to the nature of the material. But here's more detail:

 

14 static pages and 3 sections (Green, Orange, and Blue) with optional pages.

Green = 1 optional section with 2 pages.

 

Orange = 5 optional sections that are 1 page each and 1 option for uploading a PDF of varying page count.

 

Blue = 4 optional sections that are 1 page each

 

The Green section is simple, and this does exactly what I need for the Blue section (great idea to simply count the length of the array):

 

Instead you could filter the array and determine the page count by the length of the array like this:

var docs = [Field("FundCare_Orange1"),Field("Dental_Orange2"),Field("FlexSpend_Orange3"),Field("PlansWork_Orange4"),Field("HowToHSA_Orange5"),Field("ClientUpload_Orange")].filter(String); 
FusionPro.Composition.SetBodyPageUsage("Orange_Filler1", docs.length % 2); 

The page count is now appearing in the log after I incremented the PAGECount variable, thanks! It's still all or nothing; if the array has no value the filler page won't be added, but if it has any value at all it will be added. I tried using the same logic from the Blue section by applying the modulus 2 to the Pic resource created from the array, but I'm obviously still applying something incorrectly:

 

PAGEArray = [Field("FundCare_Orange1"), Field("Dental_Orange2"), Field("FlexSpend_Orange3"), Field("PlansWork_Orange4"), Field("HowToHSA_Orange5"), Field("ClientUpload_Orange")]

var PAGECount = 0;
for (i = 0 ; i < PAGEArray.length ; i++)
{
   if (PAGEArray[i] != "")
   {
   var Pic = new FusionProResource(PAGEArray[i], "graphic", "true");
   var PicPages = Pic.countPages;
   PAGECount += PicPages;
   if (PicPages % 2 > 0)
           {
                FusionPro.Composition.SetBodyPageUsage("Orange_Filler1", [color=Red]Pic.totalPages % 2[/color]);
           }            
   }
}

Print("PAGECount is " + PAGECount)

Once the Orange section is working properly it will always be an even page count at this point. So this logically looks like it should work too, but I wasn't getting any result after filling in the names of my specific unused body pages. I still have this placed in the OnRecordStart rule:

 

Are you saying you don't know how many pages are in your template? Maybe you could try this:

var endPages = [     
// These are the names of the pages that will be     
// turned on to make the totalPage count == 4     
'End Filler Left',     
'End Filler Right'     
]; 
var pagesNeeded = 4-(FusionPro.Composition.totalPages%4 || 4); 
for (var i = 0; i < pagesNeeded; i++)
FusionPro.Composition.SetBodyPageUsage(endPages[i], true); 

I feel like this is all really close, and, the help is very much appreciated. I'll try to pay it forward when I'm able!
Link to comment
Share on other sites

14 static pages and 3 sections (Green, Orange, and Blue) with optional pages.

Green = 1 optional section with 2 pages.

Orange = 5 optional sections that are 1 page each and 1 option for uploading a PDF of varying page count.

Blue = 4 optional sections that are 1 page each

Okay cool. Good to know. How are you adding the "sections?" I'm assuming you have 3 separate text rules set up to handle each section and each of those 3 pages is allowed to overflow if needed with the section-specific filler page following the overflow page in case it's needed. Is that correct? If you wanted to simplify things and do it all within the onRecordStart rule, with only one text frame and one overflow page you'd be able to if you brought in your "filler" pages as resources instead of making them part of your template – just something to think about. I will come back to this topic momentarily.

 

The page count is now appearing in the log after I incremented the PAGECount variable, thanks! It's still all or nothing; if the array has no value the filler page won't be added, but if it has any value at all it will be added. I tried using the same logic from the Blue section by applying the modulus 2 to the Pic resource created from the array, but I'm obviously still applying something incorrectly:

PAGEArray = [Field("FundCare_Orange1"), Field("Dental_Orange2"), Field("FlexSpend_Orange3"), Field("PlansWork_Orange4"), Field("HowToHSA_Orange5"), Field("ClientUpload_Orange")]

var PAGECount = 0;
for (i = 0 ; i < PAGEArray.length ; i++)
{
   if (PAGEArray[i] != "")
   {
   var Pic = new FusionProResource(PAGEArray[i], "graphic", "true");
   var PicPages = Pic.countPages;
   PAGECount += PicPages;
   if (PicPages % 2 > 0)
           {
                FusionPro.Composition.SetBodyPageUsage("Orange_Filler1", [color=Red]Pic.totalPages % 2[/color]);
           }            
   }
}

Print("PAGECount is " + PAGECount)

'totalPages' is not a property of FusionPro resources so 'Pic.totalPages' won't work – you'd have to write 'Pic.countPages.' Aside from that, you're stepping through each field in that array and turning on/off the filler page based on whether the number of pages in each resource is odd/even. What you need to do is pull the part where you're toggling the filler page out of that for loop and only do it once based on the total page count (PAGECount):

PAGEArray = [Field("FundCare_Orange1"), Field("Dental_Orange2"), Field("FlexSpend_Orange3"), Field("PlansWork_Orange4"), Field("HowToHSA_Orange5"), Field("ClientUpload_Orange")]

var PAGECount = 0;
for (i = 0 ; i < PAGEArray.length ; i++)
{
   if (PAGEArray[i] != "")
   {
    var Pic = new FusionProResource(PAGEArray[i], "graphic", "true");
    var PicPages = Pic.countPages;
    PAGECount += PicPages;
   }
}

[color="Red"]if (PicPages % 2 > 0)
{
   FusionPro.Composition.SetBodyPageUsage("Orange_Filler1", true);
}    [/color]        

Print("PAGECount is " + PAGECount)

 

After your clarifications and if my aforementioned assumptions are correct, I think what you want to do is this:

var pages = 14; // Number of static pages
var endPages = ['End Filler Left','End Filler Right']; // Names of the Filler Pages at the end

// Sections (Named in accordance to their "filler" page)
sections = {
   'Orange_Filler1': [
       Field("FundCare_Orange1"), 
       Field("Dental_Orange2"), 
       Field("FlexSpend_Orange3"), 
       Field("PlansWork_Orange4"), 
       Field("HowToHSA_Orange5"), 
       Field("ClientUpload_Orange")
   ], 
   'Green_Filler1': [
       Field("Green1")
   ],
   'Blue_Filler1': [
       Field("Blue1"),
       Field("Blue2"),
       Field("Blue3"),
       Field("Blue4")
   ]
};

for (i in sections) {
   pgCount = 0;
   sections[i].filter(String).forEach(function(s){ 
       pgCount += CreateResource(s, 'graphic').countPages;
   });
   pages += pgCount;
   if (pgCount % 2)
       FusionPro.Composition.SetBodyPageUsage(i, pages++);
}

var pagesNeeded = 4-(pages%4 || 4);

while (pagesNeeded--)
   FusionPro.Composition.SetBodyPageUsage(endPages[pagesNeeded], true);

Here's what it's doing:

The 'sections' object has 3 properties: 'Orange_Filler1', 'Green_Filler1' and 'Blue_Filler1'. These are the names of your "filler" pages. I do realize that you probably don't have a "green" filler page because it'll be even either way (2 pages or 0 pages) but the other names need to match what you've named your body pages so make adjustments as needed. The value of each property is an array of the fields that hold the name of the resource used to comprise each section.

 

The 'for' loop steps through the 'sections' object and:

  • Filters each array (removing empty fields)
  • Creates a resource from the remaining fields in order to get a page count
  • Updates the total number of pages (pages)
  • And depending on whether or not the section is odd/even, the filler sheet is activated and the page count is incremented to account for the filler sheet

 

Finally, a while loop is used to turn on extra pages to make the total page count divisible by 4.

 

As I mentioned before, you can also use this code to assign the sections to a single frame that is set to overflow (assuming that you create resources for your "filler" pages named by the 'sections' property to which they correspond ('Orange_Filler1.pdf'):

var frameName = 'TextFrame'; // Name of Text Frame to display sections
var result = []; // Array to hold sections
var pages = 14; // Number of static pages
var endPages = ['End Filler Left','End Filler Right']; // Names of the Filler Pages at the end

// Sections (Named in accordance to their "filler" page)
sections = {
   'Orange_Filler1.pdf': [
       Field("FundCare_Orange1"), 
       Field("Dental_Orange2"), 
       Field("FlexSpend_Orange3"), 
       Field("PlansWork_Orange4"), 
       Field("HowToHSA_Orange5"), 
       Field("ClientUpload_Orange")
   ], 
   'Green_Filler1.pdf': [
       Field("Green1")
   ],
   'Blue_Filler1.pdf': [
       Field("Blue1"),
       Field("Blue2"),
       Field("Blue3"),
       Field("Blue4")
   ]
};

function addPage(res) {
   res = CreateResource(res, 'graphic');
   for (var n=1; n<=res.countPages; n++) {
       res.pagenumber = n;
       result.push(res.content)
       pgCount++
   }
}

for (i in sections) {
   pgCount = 0;
   sections[i].filter(String).forEach(function(s){ addPage(s); });
   if (pgCount % 2) addPage(i);
   pages += pgCount;
}

FindTextFrame(frameName).content = result.join('<p>');

var pagesNeeded = 4-(pages%4 || 4);

while (pagesNeeded--)
   FusionPro.Composition.SetBodyPageUsage(endPages[pagesNeeded], true);

Edited by step
Typo
Link to comment
Share on other sites

Thanks, sorry for the delay. This does exactly what I was trying to do:

 


var pages = 14; // Number of static pages
var endPages = ['End Filler Left','End Filler Right']; // Names of the Filler Pages at the end

// Sections (Named in accordance to their "filler" page)
sections = {
   'Orange_Filler1': [
       Field("FundCare_Orange1"), 
       Field("Dental_Orange2"), 
       Field("FlexSpend_Orange3"), 
       Field("PlansWork_Orange4"), 
       Field("HowToHSA_Orange5"), 
       Field("ClientUpload_Orange")
   ], 
   'Green_Filler1': [
       Field("ConsultPhysician_Green")
   ],
   'Blue_Filler1': [
       Field("EmpAssist_Blue1"),
       Field("SupportMoms_Blue2"),
       Field("NICU_Blue3"),
       Field("MatProg_Blue4")
   ]
};

for (i in sections) {
   pgCount = 0;
   sections[i].filter(String).forEach(function(s){ 
       pgCount += CreateResource(s, 'graphic').countPages;
   });
   pages += pgCount;
   if (pgCount % 2)
       FusionPro.Composition.SetBodyPageUsage(i, pages++);
}

var pagesNeeded = 4-(pages%4 || 4);

while (pagesNeeded--)
   FusionPro.Composition.SetBodyPageUsage(endPages[pagesNeeded%2], true); 

Making all sections elements of one array and using .forEach simplified it. Thanks again Step, this was extremely helpful!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...