6 Replies Latest reply on Nov 25, 2008 3:09 PM by (elearning_developer2)

    inserting image at position of xmlelement

      To insert an image at the location of an xml tag's content, I tried using code similar to how one does it using a text frame's insertion point:

      var myGraphic = myXMLElement.insertionPoints.item(0).place ( myFile );

      This does not work. Tom77 posted a related question on October 10th that remains unanswered, here:


      Does anyone know how to either
      - access the insertion point of an xml element, or what might be limitations
      - or, use an alternate, simple method to find the correct insertion point after placing xml content using an xml rule

        • 1. Re: inserting image at position of xmlelement
          Level 1

          It is a matter of process sequence: The XML elements must be placed in the document first (in actual text frames) to have the insertionPoint array populate with data. Much processing can be done on xml elements first, but the insertion points will not exist until the content is placed in the document on something other than an xml layer.
          • 2. Re: inserting image at position of xmlelement
            Level 1
            Hi eLearning Developer,

            re: "...but the insertion points will not exist until the content is placed in the document on something other than an xml layer."

            It's true that you can't place a graphic into an insertion point in an unplaced XML text element, but the insertion points *do* exist--take a look at the XMLStory.jsx example script.

            In any case, you can place graphics in uplaced content by adding an href attribute, as shown in the following:

            //An InDesign CS4 JavaScript
            //Shows how to place a graphic in unplaced XML content.
            function main(){
            function mySetup(){
            function mySnippet(){
            var myDocument = app.documents.add();
            var myRootXMLElement = myDocument.xmlElements.item(0);
            var myXMLTag = myDocument.xmlTags.add("xml_element");
            var myXMLGraphicTag = myDocument.xmlTags.add("xml_graphic");
            var myXMLElementA = myRootXMLElement.xmlElements.add(myXMLTag);
            myXMLElementA.contents = "This is a paragraph in an XML story.";
            var myXMLGraphicElement = myRootXMLElement.xmlElements.add(myDocument.xmlTags.item("xml_graphic"));
            var myGraphicAttribute = myXMLGraphicElement.xmlAttributes.add("href", "file:///C:/test.jpg");
            var myTextFrame = myDocument.pages.item(0).textFrames.add({geometricBounds:myGetBounds(myDocument, myDocument.pages.item(0))});
            //Place the XML element in the layout to see the result.
            function myGetBounds(myDocument, myPage){
            var myPageWidth = myDocument.documentPreferences.pageWidth;
            var myPageHeight = myDocument.documentPreferences.pageHeight
            if(myPage.side == PageSideOptions.leftHand){
              var myX2 = myPage.marginPreferences.left;
              var myX1 = myPage.marginPreferences.right;
              var myX1 = myPage.marginPreferences.left;
              var myX2 = myPage.marginPreferences.right;
            var myY1 = myPage.marginPreferences.top;
            var myX2 = myPageWidth - myX2;
            var myY2 = myPageHeight - myPage.marginPreferences.bottom;
            return [myY1, myX1, myY2, myX2];


            • 3. Re: inserting image at position of xmlelement
              Level 1
              Exellent - thank you. I will work with that technique.

              Presently, I have another challenge I am making no progress on. I would like to place images on the left margin (left of the text frame) at points along the text, associated with a certain xml tag.

              To do this, I am first place()ing the images in tag insertion points during Rule processing as discussed; next, I am looping through the images in the document and using the move() function to move the images to the left.

              Unfortunately, the following problem arises: only the image moves, and some type of box (a bounding box? frame?) does not, so all or part of the image is not visible, depending on how far it is moved. I am not even sure if it is possible to move the image left of the text frame were I able to move the outside box, since the image is placed in an element in the text frame.

              One solution might be to determine the position of the image so placed (or, better, the xml element if such a point position is accessible - I could not find such position properties) and using that to place a new image on the document directly, to the left of the text frame where appropriate.

              Whatever the options, I cannot easily find one that works. If anyone has the time and knowledge, help is appreciated.
              • 4. Re: inserting image at position of xmlelement
                Level 1
                Hi eLearning Developer,

                The image is always within a frame--which is the parent of the image. Just move the frame, and the image will move with it. If you need to move the image frame outside the text frame, you'll be better served looking at anchored object options than by using the move method.


                • 5. Re: inserting image at position of xmlelement
                  Level 1
                  For what it is worth - my solution:

                  The image frames were bound within a small region around where they were inserted - presumably because the images were inserted from XMLElement insertionPoints. That is why moving a frame did not work, as described above - manually, they would only move a bit, and, programatically, they would throw an error when moved out of this area.

                  The only solution I found, after much effort, was to loop through all images inserted so, grab their pasteboard position and associated page number, and create a new image in the same position on the correct pages. These new images (directly over the originals) are in the same position as the original, but are not inline (if that is the correct term), and so can be moved outside the containing element and text frame...(I then looped through and deleted the inline images.)

                  If anyone can tell me a method to place the images freely the first time through - for example, by determining a precise position of a tag on a page, and using those coordinates, I will be grateful.

                  Thank you.
                  • 6. Re: inserting image at position of xmlelement
                    Level 1
                    Olav or anyone else - regarding this old thread, I had a solution, but I had not yet applied a key functionality - to apply the images according the content of the associated xml elemtent.

                    In the original pass, I tried storing this info in an array, then, while going through allGraphics in my document, I applied the information sequentially (I thought allGraphics would return images in the sequence in which I had stored them.)

                    Please visit my thread here, and answer here or there if you can:


                    Thanks - if I find a solution, I will post...