7 Replies Latest reply on Jan 11, 2013 2:48 AM by frameexpert

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

    J.Kahl

      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.

        • 1. Re: How can I find out on which page of a document an Xref can be found using ExtendScript?
          frameexpert Level 4

          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;
              }
          }
          
          • 2. Re: How can I find out on which page of a document an Xref can be found using ExtendScript?
            Russ Ward Level 4

            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);

            • 3. Re: How can I find out on which page of a document an Xref can be found using ExtendScript?
              frameexpert Level 4

              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.

              • 4. Re: How can I find out on which page of a document an Xref can be found using ExtendScript?
                frameexpert Level 4

                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;
                    }
                }
                
                • 5. Re: How can I find out on which page of a document an Xref can be found using ExtendScript?
                  Russ Ward Level 4

                  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

                  • 6. Re: How can I find out on which page of a document an Xref can be found using ExtendScript?
                    J.Kahl Level 1

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

                     

                    Jürgen

                    • 7. Re: How can I find out on which page of a document an Xref can be found using ExtendScript?
                      frameexpert Level 4

                      Hi Jürgen,

                       

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

                       

                      Rick