2 Replies Latest reply on Aug 25, 2011 11:37 PM by gijinj1

    image anchoring using javascript

    gijinj1

      Hi,

       

      When I export an indesign document to ePub, all the images that are not anchored is moved to the end of the ePub. Can anybody suggest a solution for this? I have created a javascript to anchor all the images but it is anchoring to the top of the page only. I need a solution where we can anchor the image just below the paragraph that is above the image. Any help will be greatly appreciated.

        • 1. Re: image anchoring using javascript
          Laubender Adobe Community Professional & MVP

          Can you show us some code in regard of the anchoring properties of your script?
          (Please use the "Insert / Syntax Highlighting / Java"-Feature of the forum software; click the blue double arrow.)

           

          Uwe

          • 2. Re: image anchoring using javascript
            gijinj1 Level 1

            Thanks for the quick reply. Please find the code below

             

            main();
            function main()
            {
              var doc = app.activeDocument;
              
             var count=0;
             
             // loop through all pages
             for(i=0; i<doc.pages.length;i++)
             {
                    var page = doc.pages.item(i);
                     
                    if( page.textFrames.length<1) continue;
            
                    var textFrame = page.textFrames.item(0);
            
                    if(page.rectangles.length<1) continue;
            
                    // loop through all rectangles in the page
                    for(j=0;j<page.rectangles.length;j++)
                    {
                            var imageRect=page.rectangles[j];
                                            
                            if(imageRect.images.length<1) continue;
            
                            var myAnchoredFrame=anchorImage(doc, textFrame, imageRect);
                            var pos=[imageRect.geometricBounds[1],imageRect.geometricBounds[0]]
                            imageRect.remove();
                            j--;
                            textFrame.recompose();
              
                            var k=0;
                            
                            // Reposition the anchored image. This is done repeatedly because the first call not moves the  frame to the correct position
                            do
                            {
                                myAnchoredFrame.move(pos);
                                k++;
                             }
                            while(k !=5);
                        
                           count++;
                    }
            }
            
            alert("Fixed "+count+" images");
            }
            
            function anchorImage(doc, textFrame, imageRect)
            {
                    var myAnchoredFrame=CreateAnchor (doc, textFrame);
            
                    var imBounds = imageRect.geometricBounds;
                    var frBounds=textFrame.geometricBounds
            
                    myAnchoredFrame.geometricBounds =  [imBounds[0]-frBounds[0], imBounds[1]-frBounds[1], 
                                      imBounds[2]-frBounds[0], imBounds[3]-frBounds[1]];
            
                    // Copy image into the anchored frame. Didn't find a better way
                    var imagePath=imageRect.images[0].itemLink.filePath;
                    var image=imageRect.images[0];
                    var filePath="D:\\im";
                    image.exportFile(image.imageTypeName,filePath);
                    myAnchoredFrame.place(File(filePath));
                    
                    // Resize image
                    var newImBoundX=myAnchoredFrame.geometricBounds[1]-( imageRect.geometricBounds[1]-image.geometricBounds[1]);
                    var newImBoundY=myAnchoredFrame.geometricBounds[0]-( imageRect.geometricBounds[0]-image.geometricBounds[0]);
                    var newImBoundX1=newImBoundX+( image.geometricBounds[3]-image.geometricBounds[1]);
                    var newImBoundY1=newImBoundY+( image.geometricBounds[2]-image.geometricBounds[0]);
                    
                    myAnchoredFrame.images[0].geometricBounds=[newImBoundY,newImBoundX,newImBoundY1,newImBoundX1];
                    
                    //Set textWrapPreferences of the images
                    myAnchoredFrame.textWrapPreferences.textWrapMode=imageRect.textWrapPreferences.textWrapMode;
                    myAnchoredFrame.textWrapPreferences.textWrapOffset =imageRect.textWrapPreferences.textWrapOffset ;      
                 
                    return myAnchoredFrame;
             }
            
            function CreateAnchor( doc, textFrame)
            {
            
                    var inPoint=textFrame.insertionPoints[0];
                    var anchProps = doc.anchoredObjectDefaults.properties;
                    var anchCont = anchProps.anchorContent;
            
                    var myAO = inPoint.rectangles.add();
            
                    // Make new object with correct default settings
                    // Make new object right kind of object
                    myAO.contentType =ContentType.graphicType;
                    // Recompose parent story so geometricBounds make sense
                    inPoint.parentStory.recompose();
                    //save users measurement preferences
                    userHoriz = doc.viewPreferences.horizontalMeasurementUnits;
                    userVert = doc.viewPreferences.verticalMeasurementUnits;
                    doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
                    doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
            
                    doc.viewPreferences.horizontalMeasurementUnits = userHoriz;
                    doc.viewPreferences.verticalMeasurementUnits = userVert;
                    myAO.applyObjectStyle(anchProps.anchoredObjectStyle);
                    
                    if (anchProps.anchorContent == ContentType.textType) {
                        try { // might be null
                        myAO.parentStory.appliedParagraphStyle = anchProps.anchoredParagraphStyle;
                        } catch (e) {}
                    }
                
                    myAO.anchoredObjectSettings.properties = doc.anchoredObjectSettings.properties;
                    myAO.anchoredObjectSettings.anchoredPosition=AnchorPosition.anchored;
                    return myAO
               }