Skip to main content
Participating Frequently
October 2, 2018
Answered

How to center the new string ?

  • October 2, 2018
  • 3 replies
  • 577 views

Hi forum.

I have got a function that delete a text from a texline  and set a new text in this one.

the new text is shorter and i would to center this text instead of set on the left side.

the currently uggly workaround i found is to put space to the front of the new text.

do you have smarter solution to center the text like managing the offset,LocX or adding the center property .

thank you

function ChangeText(TextLineFrame){

    // cette procedure remplace le texte de l'objet TEXTLINE

    // on défini le textrange

    // on recupére les proprietés du texte avant de le supprimer et de le remplacer.

  

     var DZGprops  

     var BegLoc = new TextLoc(), EndLoc = new TextLoc(); //create text location object

           

     BegLoc.obj = TextLineFrame;

     BegLoc.offset = 0; // insert at the start of the cell

     EndLoc.obj=TextLineFrame;

     EndLoc.offset=Constants.FV_OBJ_END_OFFSET;

    var textRange = new TextRange (BegLoc, EndLoc);

  

    DZGprops= app.ActiveDoc.GetTextProps(textRange.beg);

    app.ActiveDoc.DeleteText(textRange);

    app.ActiveDoc.AddText(BegLoc, "                                                    new text with front blank space"  );

    app.ActiveDoc.SetTextProps(textRange, DZGprops);  

    $.writeln("text has been change");

  

     }

This topic has been closed for replies.
Correct answer Russ Ward

Hi,

Sometimes formatting rules/properties do not re-apply fully when you alter a document. I suspect that when you toggle the page view, it is reapplying formatting properties and that is why the problem gets fixed. You might also consider trying a forced refresh, something like:

oDoc.Reformat()

and/or

oDoc.Redisplay()

Theoretically, these should not be necessary, but sometimes for unknown reasons they are.

Russ

3 replies

Participating Frequently
October 2, 2018

Thank you for response.

As the textline is on reference page i found an other solution.i just swap between body page and reference page and the text is automatically centered in his textline. Why i don't know but it works.

the code become:.

function ChangeText(TextLineFrame){

    // cette procedure remplace le texte de l'objet TEXTLINE

    // on défini le textrange

    // on recupére les proprietés du texte avant de le supprimer et de le remplacer.

     var oDoc = app.ActiveDoc;

     var DZGprops   

     var BegLoc = new TextLoc(), EndLoc = new TextLoc(); //create text location object

            

     BegLoc.obj = TextLineFrame;

     BegLoc.offset = 0; // insert at the start of the cell

     EndLoc.obj=TextLineFrame;

     EndLoc.offset=Constants.FV_OBJ_END_OFFSET;

    var textRange = new TextRange (BegLoc, EndLoc);

   

    DZGprops= app.ActiveDoc.GetTextProps(textRange.beg);

    app.ActiveDoc.DeleteText(textRange);

    app.ActiveDoc.AddText(BegLoc, "new text to insert"  );

     //TextLineFrame=Constants.FV_TEXTLINE_CENTER;

    app.ActiveDoc.SetTextProps(textRange, DZGprops);   

  

    // il faut switcher les pages pour que le nouveau texte soit centré pourquoi ????

     // switch body page and ref page to display the text centered ????

    oDoc.CurrentPage = oDoc.FirstBodyPageInDoc;

    oDoc.CurrentPage = oDoc.FirstRefPageInDoc;

    $.writeln("Le copyright à été changé");

   

     }

Russ WardCorrect answer
Legend
October 2, 2018

Hi,

Sometimes formatting rules/properties do not re-apply fully when you alter a document. I suspect that when you toggle the page view, it is reapplying formatting properties and that is why the problem gets fixed. You might also consider trying a forced refresh, something like:

oDoc.Reformat()

and/or

oDoc.Redisplay()

Theoretically, these should not be necessary, but sometimes for unknown reasons they are.

Russ

frameexpert
Community Expert
Community Expert
October 2, 2018

The alignment for a TextLine is controlled by its TextLineType property. Here are some of the values:

Constants.FV_TEXTLINE_LEFT

Constants.FV_TEXTLINE_CENTER

Constants.FV_TEXTLINE_RIGHT

www.frameexpert.com
Inspiring
October 2, 2018

Hi,

TextLines have no alignment, so you need to reposition the textline object.

Possible Algorithm:

1. Get Position (LocX) and width of the original TextLineFrame

2. Change text

3. Get Position (LocX) and width of the new TextLineFrame

4. Reposition changed TextLineFrame

var newX = oldX + oldWidth - newWidth;

Hope this helps

Markus