Skip to main content
Participant
December 6, 2011
Question

Stacking Order of pageItems in CS5

  • December 6, 2011
  • 3 replies
  • 8902 views

I have not been able to get the correct stacking order of pageItems within a layer/page (in CS5). This was simple to do in CS4 as something like this (CurrentPageItem = myPage.pageItems😉 would return pageItems in the actual stacking order in the document.

With CS5 all textframes come together and all rectangles come together, irrespective of them stacked in any order (TextFrame, Rectangle, TextFrame,.... would come out as TextFrame, TextFrame and Rectangle). Some solutions already in the forum point to changing to older version (6.0), but I don't want to do that.

Can there be a way to determine STACKING ORDER within the layer/page in CS5?

This topic has been closed for replies.

3 replies

grefel
Community Expert
Community Expert
December 9, 2015

I use the findings of this thread to get the z-index for an HTML Export. This works well with less than ~200 PageItems per Page. With more PageItems InDesign becomes unresponsive and I need to kill the process manually. Tested with Windows 7 and CS 6/CC 2015.

The everyItem() function slow with > 150 PageItems and unusable with > 600 PageItems.

I don't see a solution, but want to add this bit of information to the thread.

For testing I used the following script:

var items = 200;

var dok = app.documents.add();

var page = dok.pages[0];

$.writeln("start " + Date());

for (var i = 0; i < items; i++) {

    page.textFrames.add();

}

$.writeln("analyse complete " + Date());

var zOrderById = {};

var itemZO = page.pageItems.everyItem().index, 

itemIds = page.pageItems.everyItem().id, 

t = page.pageItems.length; 

while( t-- ) {

    zOrderById[itemIds] = itemZO

}

$.writeln("end " + Date());

Legend
December 9, 2015

Not too bad here, Mac mini i7 16GB CC2015.1

start Wed Dec 09 2015 13:25:40 GMT-0000

analyse complete Wed Dec 09 2015 13:25:44 GMT-0000

end Wed Dec 09 2015 13:25:50 GMT-0000

grefel
Community Expert
Community Expert
December 9, 2015

How many items did you tried?

Change the number of items in line 1. I could not process more than 1000 without killing the process after ~10 minutes.

Would be interesting if it runs better on Mac.

Known Participant
December 16, 2011

Hi, I'm new to this thread but used information from it recently to improve the behavior of some code that needs to process pageitems in back-to-front order.

The changes I made -- using the 'index' property -- are like this, assuming oPage is a Page object:

   var pageItemArray = oPage.pageItems.everyItem().getElements();     // get the collection's pageitems in an array

   var indexes = oPage.pageItems.everyItem().index;          // get the index values of each item too

   for( var iPageItem=pageItemArray.length-1; iPageItem > -1; iPageItem-- ) {

      var oPageItem = pageItemArray[ indexes[iPageItem] ];     // use index[iPageItem] instead of iPageItem to find the next pageitem

      ...

This seemed to work, but I'm processing an InDesign file today with a three-page spread -- a back cover, a spine, and a front cover of a book.  The back cover has 2 pageitems and the front has 5 items (one of which overlaps the spine page; the spine page itself has no items).

When my loop has 'oPage' looking at the back cover, the 'indexes' array is [5,3], and pageItemArray is [  [object Rectangle],[object Rectangle] ]

So now I'm confused.  The documentation says a pageitem's 'index' property is "The index of the PageItem within its containing object." -- but oPage only has two objects.  The code blows up if I run it with this set of data, because  pageItemArray[ indexes[0] ] translates to pageItemArray[5].

I wondered whether maybe the contents of the 'indexes' array is spread-relative, since 5 and 3 would work as indexes into oPage.parent.pageItems.everyItem().getElements().  Yet when my page loop comes around to the 'front' page,the 'indexes' array now contains [5,3,2,1,0].  I did not expect to see duplicates here, if these index values are spread-relative.  So that theory seems like it must be bogus.

Harbs or somebody, can you explain what I'm doing wrong here?  When I'm getting the 'indexes' array from oPage.pageItems.everyItem().index, shouldn't (according to the documentation) the values be the index of each of the page's items within that page?  I need code that will let me list the pageitems for each page of a spread, in bottom-to-top z-order for each page.  Do I need to just forget about using this approach altogether, and use each item's parentPage property to find which page it's on, and pre-process a spread's items into separate page-specific lists, then go through each list I've created?

Thanks for any help.

- Rich

Harbs.
Legend
December 17, 2011

You can't use the index to specify the items within your array (like you discovered). The indexes specify the stacking order within the spread.

Instead of trying to explain how to use the indexes, it was easier to just write a function which does it...

function getPageItemsInZOrder(container){

    var objs = [], i;

    var pis = container.pageItems.everyItem().getElements();

    var indexes = container.pageItems.everyItem().index;

    for(i=0;i<pis.length;i++){

        objs.push({

            obj:pis,

            index:indexes

        })

    }

    objs.sort(sortByIndex);

    var retVal = [];

    for(i=0;i<objs.length;i++){

        retVal.push(objs.obj);

    }

    return retVal;

    function sortByIndex(a,b){

        return a.index-b.index;

    }

}

To use the function, you'd do something like this:

var myPageItems = getPageItemsInZOrder(myPage);

Harbs

Known Participant
December 17, 2011

Thank you Harbs – your approach appears (after limited testing) to work great! But I can’t get by without at least understanding a little more about what’s going on here – you say “The indexes specify the stacking order within the spread.” – Why would two of the pages have index arrays of and ? Your code looks like it just uses a page’s index array as relative stacking order numbers; seemingly that’s all they’re good for, right? Do they correspond to any other data available for the spread or pages, like one of the allPageItems arrays’ entries or something like that?

But again, thanks – I appreciate not just an answer but a solution!

- Rich

Harbs.
Legend
December 6, 2011

Check the index of the pageItems.

The index is the placement in the z order and does not necessarily match the position in the collection...

Harbs

sumitkdevAuthor
Participant
December 6, 2011

Hi Harbs,

Well, the index property did give me the stacking order of pageItems, just that it clubs the index values according to the type of pageItem. So again, I got index value "0" twice, one for the textframes and one for the "rectangle". Ideally, I expected if the textframe was on top of the rectangle(an image container), the rectangle would have the index "0" and the textframe to have index "1" (if the z-index theory holds for all pageItems). Is there any other property that holds the information of stacking order (across all types of pageItems)? The desktop version of CS5 has this property within the layer (one can stack pageItems within the layer). Is there something analogous in the layer object?      

Harbs.
Legend
December 6, 2011

I'm not getting duplicate indexes.

Can you give me more details on what you did to get the duplicate indexes?

Harbs