Skip to main content
mikes26326174
Inspiring
May 16, 2023
Answered

Handle overflow of anchored textframe

  • May 16, 2023
  • 2 replies
  • 582 views

Hi,

I apply an Extendscript to the attached file. The script resizes images ( function fitImages ) and bordered textframes to the width of the textframe ( function fitBorderTextFrames). When the content of the anchored textframe overflows, the whole content disappears.
Is there a way I could handle this so that the content does not disappear ?
That's the script:

var doc = app.activeDocument; 
app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;


fitBorderTextFrames();
fitImages();

// resizen von Rahmen und Tabellen
function fitBorderTextFrames()
{
    var textFramesArray = app.documents[0].stories.everyItem().textContainers;
   
    for ( var i = 0; i < textFramesArray.length; i++ )
    {
        var singleTextContainerArray = textFramesArray[i];
         for ( var a = 0; a <singleTextContainerArray.length; a++ )
         {
            var tf = singleTextContainerArray[a];
            // weil ein textContainer sowohl textFrames als auch textPaths enthalten kann
            if ( tf.constructor.name == 'TextFrame')
            {    
                var hasBorder =  tf.extractLabel("hasBorder"); 
                if ( hasBorder == 'true' )
                {    
                    tf.appliedObjectStyle = doc.objectStyles.itemByName("Rahmen");
                    
                    if ( tf.parent.constructor.name == "Character" )
                    {
                       surroundingElement = tf.parent.parentTextFrames[0];
                      
                            try {
                               var resizeWidth =getWidthForResizing (surroundingElement);
                               
                               if( surroundingElement != undefined ) {
                                   var borderedFrameWidthInmm  = resizeWidth + 'mm';
                                   setWidthHeight( tf, borderedFrameWidthInmm, '10mm');    
                               }
                      
                            }
                            catch( error ) {
                              
                            }  
                   }         
              }
            }
         }        
    }
};


function getTextFrameWidth( theFrame )
{
        try {
            var textFrameBounds = theFrame.geometricBounds;
            var myX = textFrameBounds[1];
            var textFrameWidth = textFrameBounds[3] - myX;
        }
        catch( error ) {
        }        
    return textFrameWidth;
}    


function setWidthHeight(o, w, h, useVisibleBounds)
{
    if ( !(o && 'resize' in o ) ) return;
    var CS_INNER = CoordinateSpaces.INNER_COORDINATES,
        BB = BoundingBoxLimits[
            (useVisibleBounds?'OUTER_STROKE':'GEOMETRIC_PATH')
            + '_BOUNDS'];
 
    var wPt = UnitValue(w).as('pt'),
        hPt = UnitValue(h).as('pt');
 
    tfHeight = 650;
    
    // Maximale Ausdehnung setzen
    if ( hPt > tfHeight )
    {
        hPt = tfHeight;
    }

    if ( 0 >= wPt || 0 >= hPt ) return;



    o.resize(
        [CS_INNER,BB],
        AnchorPoint.CENTER_ANCHOR,
        ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH,
        [wPt,hPt,CS_INNER]
        );
       
}


function getWidthForResizing(textFrameOfRectangle) {
       
        // besteht der Textrahmen aus mehreren Spalten ?      
        var columns = textFrameOfRectangle.textFramePreferences.textColumnCount;
 
        if( columns > 1 ) {
            var resizeWidth = textFrameOfRectangle.textFramePreferences.textColumnFixedWidth; 
        }
        else {
            var resizeWidth = getTextFrameWidth(textFrameOfRectangle);
        }       

    return resizeWidth;
}


function fitImages()
{
    var _dok = app.activeDocument;
    var _alleBilder = _dok.allGraphics;
    for ( var i = 0; i < _alleBilder.length; i++ )
    {
        var _bild = _alleBilder[i];
        var _bildRahmen = _bild.parent;
        var imageFileName =  _bildRahmen.extractLabel("imagepath"); 
        
        if ( imageFileName.length > 0 )
        {
            var filenameParts = imageFileName.split('.');
            var fileType = filenameParts[ filenameParts.length -1 ].toLowerCase();
            
            // Masse des Bildes ermitteln:
            if ( fileType == 'pdf' )
            {
                thePlacedImage = _bildRahmen.pdfs[0]; 
            } 
            else if ( fileType == 'eps' )
            {
               thePlacedImage = _bildRahmen.epss[0]; 
            }
            else
            {
                thePlacedImage = _bildRahmen.images[0]; // das erste image des rectangles
            }
            
            if ( thePlacedImage.isValid )
            {    
                if ( _bildRahmen.parent.constructor.name == "Character" )
                {
                    
                    try {
                    
                    var textFrameOfRectangle = _bildRahmen.parent.parentTextFrames[0];
                    
                    // muss geprüft werden, weil sonst Fehler bei den Bildern des Deckblatts
                    if ( textFrameOfRectangle != undefined )
                    {     
                        
                     
                        textFrameWidth =getWidthForResizing (textFrameOfRectangle);
                
                        var textFrameWidthInmm  = textFrameWidth + 'mm'; 
                        
                        var imageBounds = thePlacedImage.geometricBounds;    
                        var imageY1 = imageBounds[0];
                        var imageY2 = imageBounds[2];
                        var imageX1 = imageBounds[1];
                        var imageX2 = imageBounds[3];              
                        
                        var imageHeight = imageY2 - imageY1;
                        var imageWidth = imageX2 - imageX1;                   
                        // Berechnen wie das Rectangle in der Höhe skaliert werden muss:
                        var verticalScaleFactor = textFrameWidth / imageWidth;
                        var scaledTextFrameHeight = imageHeight * verticalScaleFactor;
                        var scaledTextFrameHeightInmm = scaledTextFrameHeight + 'mm';
                        
                      
                        
                        // das Rectangle anpassen. Muss vor dem image-resize stattfinden
                        setWidthHeight(_bildRahmen, textFrameWidthInmm, scaledTextFrameHeightInmm);                     
                        //  wenn Bildauflösung nicht mehr hergibt:: keine Anpassungsoption, aber zentriert prüfen
                        if ( imageWidth < textFrameWidth )
                        {
                            //$.writeln ('image zu klein '  );
                            var imageCenter = _bildRahmen.extractLabel("center");
                            if ( imageCenter != '' )
                            {
                                _bildRahmen.fit (FitOptions.CENTER_CONTENT);
                            }
                        }
                    }
                
                 } // end try
                catch( error ) {
                    outputDebug('Fehler beim Ermitteln der Textrahmenbreite bei einem Bild' );
                }
                
                
                }              
                var imageFit =  _bildRahmen.extractLabel("fit").toString() ;  // die fit-option setzen
                
                // default für fit:
                if ( imageFit.length == 0 || imageFit == 'normal' )
                {
                    imageFit =  'FillProportionally';
                }                    
                 
                if ( imageFit == 'fit_in_context' )
                {
                    _bildRahmen.fit (FitOptions.CONTENT_TO_FRAME);
                }      
                else if ( imageFit == 'Proportionally')
                {
                    _bildRahmen.fit (FitOptions.PROPORTIONALLY);
                }
                else if ( imageFit == 'FillProportionally')
                {
                    _bildRahmen.fit (FitOptions.FILL_PROPORTIONALLY);
                }                
                
                var signet = _bildRahmen.extractLabel("signet"); 
                var level = _bildRahmen.extractLabel("level"); 
                // signet?
                
                try
                {
                    if ( signet == 'is_signet' )
                    {
                     
                        if ( level == 0 )
                        {
                           var signetFormatName = "Signet"; 
                        }
                        else
                        {
                            var signetFormatName = "Signet_" + level;
                        }

                         
                        if ( doc.objectStyles.itemByName(signetFormatName).isValid )
                        {
                            _bildRahmen.appliedObjectStyle = doc.objectStyles.itemByName(signetFormatName);
                            //$.writeln ('objektstil signet'  );
                        }
                        else if ( doc.objectStyles.itemByName("Signet").isValid )
                        {
                            _bildRahmen.appliedObjectStyle = doc.objectStyles.itemByName("Signet");
                        }
                        else
                        {
                            throw new Error("Objectstyle "+signetFormatName+" fehlt!");
                        }
                    } 
                    else if ( signet == 'is_event_image')
                    {
                        if ( doc.objectStyles.itemByName("Veranstaltungsbild").isValid )
                        {
                            _bildRahmen.appliedObjectStyle = doc.objectStyles.itemByName("Veranstaltungsbild");
                        }
                        else
                        {
                            throw new Error("Objectstyle Veranstaltungsbild fehlt!");
                        }                       
                    }   
                    else
                    {
                        if ( doc.objectStyles.itemByName("Bild").isValid )
                        {
                            _bildRahmen.appliedObjectStyle = doc.objectStyles.itemByName("Bild");
                            //$.writeln ('objektstil Bild'  );
                        }
                        else
                        {
                            throw new Error("Objectstyle Bild fehlt!");
                        }
                    }
                }
                catch ( error )
                {
                    outputDebug( error);
                }                
                
            }
            // image filename not valid
            else
            {
                $.writeln('imageFileName not valid: ' + imageFileName );
            }
        }
    }
}

Thanks
Mike

This topic has been closed for replies.
Correct answer Peter Kahrel

The inline frame can't be taller than the text frame it sits in.  If it gets taller than the text frame it oversets the frame/story. That's the problem. You'll have to add an overflow frame, which is a hassle. Maybe instead of an inline frame, place the text in the main story and add the box with paragraph shading.

 

P..

2 replies

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
May 19, 2023

The inline frame can't be taller than the text frame it sits in.  If it gets taller than the text frame it oversets the frame/story. That's the problem. You'll have to add an overflow frame, which is a hassle. Maybe instead of an inline frame, place the text in the main story and add the box with paragraph shading.

 

P..

mikes26326174
Inspiring
May 19, 2023

Ok, thanks a lot.

Peter Kahrel
Community Expert
Community Expert
May 17, 2023

When all the content suddenly disappears that's usually caused by a line that can't be broken (e.g. a long URL or over-enthusiastic noBreak applied) or keeps settings. Or another frame has wrap applied and partially overlaps with your frame. To avoid the latter, set the frame's ignoreWrap to true.

mikes26326174
Inspiring
May 19, 2023

Thanks Peter, I tried it with ignoreWrap, but it did not work for me.
Could you have a look at the attached Indesign document ?
On page 3 there is an article below 'Titelseite'. If you add some more words there, the content gets lost.