6 Replies Latest reply on Mar 22, 2016 3:38 AM by K.Daube

    Re-apply format to paragraph

    K.Daube Level 1

      Dear all,

      Once again it seams that I "put the cart before the horse" to do the following:

      - In the current document i have a text selection and want to re-apply the pgf format from the catalogue to the current pgf.

      - Taking the properties from the catalogue and applying to the current pgf does nothing.

       

      function ReApplyPgfFormat (oDoc, tRange) {
      // oDoc        current document
      // tRange      current text range
        var currPgf = tRange.beg.obj;
        var pgfFormat = currPgf.Name;                   // most likely Footnote, but not guaranteed
        var pgfFmt = oDoc.GetNamedPgfFmt(pgfFormat);    
        var pgfProps = pgfFmt.GetProps();               // from catalogue
        currPgf.SetProps(pgfProps);                     // re-apply
      alert ("Reformatting done?");                     // re-apply did not happen
      }
      

      I was searching in the documentation (fdk reference, object reference) back and forth - but did not get a clue.

        • 1. Re: Re-apply format to paragraph
          frameexpert Level 4

          Here is a function I use to apply a paragraph format to a paragraph.

           

          function applyPgfFmt (pgf, name, doc) {
             
              var pgfFmt = 0;
             
              pgfFmt = doc.GetNamedPgfFmt(name);
              if (pgfFmt.ObjectValid()) {
                  var props = pgfFmt.GetProps();
                  pgf.SetProps(props);
              }
              else {
                  pgf.Name = name;
              }
          }
          

           

          In your case, you could call it like this:

           

          var doc = app.ActiveDoc;
          var pgf = doc.TextSelection.beg.obj;
          
          applyPgfFmt (pgf, pgf.Name, doc);
          

           

          -Rick

          • 2. Re: Re-apply format to paragraph
            K.Daube Level 1

            Thank You Rick,

            However, I had to insert line 4. Without this the format is not really applied (in this case). Maybe the Textselection is in a special state from the find/replace process. Probably my initial idea (not communicated) would also have worked with this.

            function ReApplyPgfFormat (oDoc, tRange) {
            // oDoc        current document
            // tRange      current text range
              oDoc.TextSelection = tRange;                    // without this the re-apply don't work
              var currPgf = tRange.beg.obj;
              applyPgfFmt (currPgf, currPgf.Name, oDoc);      // re-apply 
            }
            
            
            • 3. Re: Re-apply format to paragraph
              K.Daube Level 1

              Dear all,

              I was to fast in accepting the solution... Further tests with other fm-documents revealed my misconception: The ¶-format is not re-applied - it just looks so:

              • What's copied during the find/replace has a font size of 12pt (coming from the RTF file).
              • The reciving ¶-format has various font-sizes (due to various formats).
              • I had tested initially with files using 12pt everywhere...
              • Olthough the paste operation inserts something with different format, the information filed bottom left does not indicate a modified ¶-format. There is not * after the format-name.
              • In the script line 04 reports Undefined and hence lines 8 and 9 are not executed.

               

              function ReApplyPgfFormat (oDoc, tRange) {
                oDoc.TextSelection = tRange;
                var currPgf = oDoc.TextSelection.beg.obj;
              Alert ("currPgf.name = " + currPgf.name);        // => undefined
                var pgfFmt = 0;  
                pgfFmt = oDoc.GetNamedPgfFmt(currPgf.name);    // take properties from catalogue  
                if (pgfFmt.ObjectValid()) { 
                  var props = pgfFmt.GetProps();  
                  currPgf.SetProps(props);  
                }                                              // do nothing, if not in catalogue
                giLvlTrace -= 1;
              } // --- end ReApplyPgfFormat
              

               

              What can I do to get the current ¶ from the text range?

              • 4. Re: Re-apply format to paragraph
                Klaus Göbel Level 3

                Hi Klaus,

                 

                the problem is definitely the undefined Pgf (line 4);

                 

                There are some points to look at:

                - try currPgf.Name instead of currPgf.name

                - how do you define your selection?


                The variable oDoc looks like a global one. But you use it as an argument in your function. I don't know, if this can cause a problem (same with tRange).


                 

                • 5. Re: Re-apply format to paragraph
                  K.Daube Level 1

                  Wow, Klaus, again capitalisation hit me!

                  «try currPgf.Name instead of currPgf.name»

                  This works as intended.

                   

                  I have not yet found a clear list of syntax rules - and there are differences between JS and ES object and methods ...

                  • 6. Re: Re-apply format to paragraph
                    K.Daube Level 1

                    This is the final solution to my problem which is not just re-apply the current ¶-format:

                    • Although the paste operation inserts something with different format, the information field bottom left does not indicate a modified ¶-format. There is no * after the format-name.
                    • After inserting (replacing) the temp. citation by a biblographic record the ¶ shows mixed fonts and sizes. Only part of the ¶ is selected (that part where the found temp. citation was).

                    What I ned to do in my case is this:

                    • Get only font-family and font-size from the catalogue
                    • Apply only these properties to the completely selected ¶

                     

                    function ReApplyFontAndSize (oDoc, tRange) {
                      oDoc.TextSelection = tRange;                    // current selection is only part of ¶
                      var currPgf = oDoc.TextSelection.beg.obj;
                      var pgfFmt = 0, propsPgf; 
                      pgfFmt = oDoc.GetNamedPgfFmt(currPgf.Name);    // get properties from catalogue  
                    
                      if (!pgfFmt.ObjectValid()) {                   // pgf fmt is not in catalogue
                        return;
                      }
                      propsPgf = AllocatePropVals(2);                // for font-family and -size
                      propsCatlg = pgfFmt.GetProps();                // get properties from catalogue
                      j =  GetPropIndex(propsCatlg, Constants.FP_FontFamily);
                      propsPgf[0] = propsCatlg [j]; 
                      j =  GetPropIndex(propsCatlg, Constants.FP_FontSize);
                      propsPgf[1] = propsCatlg [j]; 
                    
                      tRange.beg.obj = currPgf;                      // select the whole paragraph
                      tRange.beg.offset = 0;
                      tRange.end.obj = currPgf;
                      tRange.end.offset = Constants.FV_OBJ_END_OFFSET;
                      oDoc.TextSelection = tRange; 
                      oDoc.SetTextProps (tRange, propsPgf);          // apply font family and size
                    } // --- end ReApplyFontAndSize
                    
                    

                     

                    This may be streamlined - but it does what it should.