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

How can I find out on which page of a document an Xref can be found using ExtendScript?

Community Beginner ,
Jan 10, 2013 Jan 10, 2013

Copy link to clipboard

Copied

Hi,

I want to find out on which page of a document an external XRef can be found. Is there a simple way to manage that using ExtendScript? The equivalent in FrameScript is XRef.Page.PageNumString.

Thank you in advance.

TOPICS
Scripting

Views

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

Community Expert , Jan 10, 2013 Jan 10, 2013

Here is the modified function, which now requires a doc object as its second parameter.

function getPage (obj, doc) {

    var frame = 0, cell = 0;

    var objType = "", prop = 0;

    while (obj) {

        frame = obj;

        objType = obj.constructor.name;

        switch (objType) {

            case "SubCol" :

                obj = obj.ParentTextFrame;

                break;

            case "Tbl" :

                obj = obj.FirstRowInTbl.FirstCellInRow;

                break;

            case "Row" :

       

...

Votes

Translate

Translate
Community Expert ,
Jan 10, 2013 Jan 10, 2013

Copy link to clipboard

Copied

Here is a general purpose function to get you started. You pass it an object and it returns the parent page object. At this point, it does not handle XRefs directly, but you can pass in the paragraph that contains the XRef.

var page = getPage(xref.TextRange.beg.obj);

should work.

Rick

function getPage (obj) {

    var frame = 0, cell = 0;

    var objType = "";

    while (obj) {

        frame = obj;

        objType = obj.constructor.name;

        switch (objType) {

            case "SubCol" :

                obj = obj.ParentTextFrame;

                break;

            case "Tbl" :

                obj = obj.FirstRowInTbl.FirstCellInRow;

                break;

            case "Row" :

                obj = obj.FirstCellInRow;

                break;

            case "Cell" :

            case "Pgf" :

            case "AFrame" :

                obj = obj.InTextFrame;

                break;

            case "TextLine" :

            case "TextFrame" :

            case "UnanchoredFrame" :

            case "Arc" :

            case "Ellipse" :

            case "Group" :

            case "Inset" :

            case "Line" :

            case "Math" :

            case "Polygon" :

            case "Polyline" :

            case "Rectangle" :

            case "RoundRect" :

                if (obj.FrameParent.ObjectValid()) {

                    obj = obj.FrameParent;

                } else {

                    obj = 0;

                }

                break;

            default:

                // Prevent endless loop if unknown object type is found.

                obj = 0;

                break;

        }

    }

    if (frame) {

        return frame.PageFramePage;

    } else {

        return 0;

    }

}

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
Mentor ,
Jan 10, 2013 Jan 10, 2013

Copy link to clipboard

Copied

Hi J,

I'm not a FrameScript user, but it appears that the FrameScript folks definitely simplified this operation for you. ES follows the FDK model, which is considerably less simple. Basically, you need to:

- Find the paragraph that the xref is in

- Find the text frame that the paragraph is in

- Find the top level parent frame for the text frame; that is, the page frame

- Find the page for the page frame

- Retrieve the page number from the page

Assuming you already have the xref object as 'xref', the following code should do it:

var tr = xref.TextRange;

var pageFrame;

var tempFrame = tr.beg.obj.InTextFrame;

while(tempFrame.ObjectValid())

{

    pageFrame = tempFrame;

    tempFrame = tempFrame.FrameParent;

}

var page = pageFrame.PageFramePage;

var pageNum = page.PageNumString;

alert(pageNum);

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 ,
Jan 10, 2013 Jan 10, 2013

Copy link to clipboard

Copied

There is a potential problem with Russ's and my code.

var pageFrame = 0;

var tr = xref.TextRange;

var pgf = tr.beg.obj;

This gets the paragraph object that contains the cross-reference. Technically, the paragraph could start on one page and the cross-reference could be on another page. Although this may be unlikely, it is probably best to account for it. Here is one way you might do it.

var doc = app.ActiveDoc;

var textLoc = xref.TextRange.beg;

var prop = doc.GetTextPropVal (textLoc, Constants.FP_InTextObj);

var textObj = prop.propVal.obj; // SubCol or Cell object.

if (textObj.constructor.name === 'SubCol') {

    var textFrame = textObj.ParentTextFrame;

} else { // Cell

    var textFrame = textObj.InTextFrame;

}

while(textFrame.ObjectValid())

{

    pageFrame = textFrame;

    textFrame = textFrame.FrameParent;

}

var page = pageFrame.PageFramePage;

var pageNum = page.PageNumString;

alert(pageNum);

When I get some time, I will update my getPage function to accomodate XRefs.

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 ,
Jan 10, 2013 Jan 10, 2013

Copy link to clipboard

Copied

Here is the modified function, which now requires a doc object as its second parameter.

function getPage (obj, doc) {

    var frame = 0, cell = 0;

    var objType = "", prop = 0;

    while (obj) {

        frame = obj;

        objType = obj.constructor.name;

        switch (objType) {

            case "SubCol" :

                obj = obj.ParentTextFrame;

                break;

            case "Tbl" :

                obj = obj.FirstRowInTbl.FirstCellInRow;

                break;

            case "Row" :

                obj = obj.FirstCellInRow;

                break;

            case "Cell" :

            case "Pgf" :

            case "AFrame" :

                obj = obj.InTextFrame;

                break;

            case "TextLine" :

            case "TextFrame" :

            case "UnanchoredFrame" :

            case "Arc" :

            case "Ellipse" :

            case "Group" :

            case "Inset" :

            case "Line" :

            case "Math" :

            case "Polygon" :

            case "Polyline" :

            case "Rectangle" :

            case "RoundRect" :

                if (obj.FrameParent.ObjectValid()) {

                    obj = obj.FrameParent;

                } else {

                    obj = 0;

                }

                break;

            case "XRef" :

                prop = doc.GetTextPropVal (obj.TextRange.beg, Constants.FP_InTextObj);

                var obj = prop.propVal.obj;

                break;           

            default:

                // Prevent endless loop if unknown object type is found.

                obj = 0;

                break;

        }

    }

    if (frame) {

        return frame.PageFramePage;

    } else {

        return 0;

    }

}

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
Mentor ,
Jan 10, 2013 Jan 10, 2013

Copy link to clipboard

Copied

Rick, good catch. When I added the ability to match elements based on page number to FrameSLT XPath, I had to do the same thing, since an element can span multiple pages.

Russ

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 ,
Jan 11, 2013 Jan 11, 2013

Copy link to clipboard

Copied

Thank you both for the replys, that really helped to solve my problem.

Jürgen

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 ,
Jan 11, 2013 Jan 11, 2013

Copy link to clipboard

Copied

LATEST

Hi Jürgen,

Please mark one of our answers as Correct and one as Helpful. Thank you very much.

Rick

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