2 Replies Latest reply on Dec 15, 2013 11:16 PM by charanseeram

    Can we assign  html tags color/size/face to textFrame.contents of inDesign ?

    charanseeram Level 1

                                      var cellInstructionText:String = <TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Arial" SIZE="12" COLOR="#FF0000" LETTERSPACING="0" KERNING="0">tetst instu</FONT></P></TEXTFORMAT>

                                                                       var layerText:TextFrame;      
                                                                       layerText = myPage.textFrames.add();                                                                                            
                                                                       layerText.geometricBounds = [cell_y,cell_x,cell_y+cell_height,cell_x+cell_width]; // consider them as y1,x1,y2,x2

                                                                     //this below Reg expression gives only text content of above string

                                                            
                                                                       var tagRegExp:RegExp = new RegExp("<([^>\\s]+)(\\s[^>]+)*>", "g");
                                                                       layerText.contents = cellInstructionText.replace(tagRegExp, "");     // Out put: testst instu            


      Here , I have to apply it's 1)color, 2) size and 3)face of above string to the textFrame content of indesign document  , is this possible in any other way  apart from below Case _1 ?  coz , Case_1 way is getting complex when string holds multiple  colors/ fonts/face for single line of text and  am afraid what would be the case for multiple paragraph line of text ?.


      Is their any other indesign api classes are helpfull ?  in applying html tag contents of  colors/size/ to textframe contents of indesign ?


                                       Case_ 1:
                                                                     var content:String = cellInstructionText;
                                                                      var sizes:Array = new Array();
                                                                       sizes = content.match(/SIZE\s*=\s*["'](\d{1,})["']/);
                                                                       // array is null if no matches found:
                                                                       if ( sizes != null ) {
                                                                               size = sizes[1];
                                                                       }

                                                                       layerText.texts.item(0).pointSize = size;
                                                                      
                                                                      
                                                                       var faces:Array = new Array();
                                                                       faces= content.match(/FACE\s*=\s*["'](.[^"']*)["']/);
                                                                       // array is null if no matches found:
                                                                       if ( faces != null ) {
                                                                               face = faces[1];
                                                                       }

                                                                       layerText.texts.item(0).appliedFont = app.fonts.item("face");                                          
                                                                      
                                                                      
                                                                       var colors:Array = new Array();
                                                                       colors = content.match(/COLOR\s*=\s*["'](.[^"']*)["']/);
                                                                       // array is null if no matches found:
                                                                       if ( colors != null ) {
                                                                               color = colors[1];
                                                                       }
                                                                      
                                                                       var myColor:Object = myDocument.colors.item("color");
                                                                       try {
                                                                               layerText.texts.item(0).fillColor = myColor.name;
                                                                       }
                                                                       catch (myError){
                                                                               //The color did not exist, so create it.
                                                                               /*layerText.texts.item(0).fillColor = myDocument.colors.add({name:"color", model:ColorModel.process,
                                                                                       colorValue:[0,100,100,0]}); */
                                                                       }                                              
                                                                      

        • 1. Re: Can we assign  html tags color/size/face to textFrame.contents of inDesign ?
          charanseeram Level 1
          • Reg Expression couldn't able to handle the number of proboblity's were the TextFormat string contains , and i was doing some xml parsing has below ,

                  Please let  me know if i can do better than this .

           

           

           

          var xmlConvertedText:XMLList = XMLList(""+cellInstructionText+"");

          for(var i1:int=0;i1<xmlConvertedText.children().length();i1++)

               {

              var _rowOfXmlConvText:XML = xmlConvertedText.children()[i1];

              if(_rowOfXmlConvText != null)

                 {

                    for(var j1:int=0;j1<_rowOfXmlConvText.children().length();j1++)

                        {

                      var _dataOfRow:XML = _rowOfXmlConvText.children()[j1];

                      applyStyles(_dataOfRow,layerText);

                        }

                    }

              }

           

          public function applyStyles(_fontData:XML,textLayer:TextFrame):void{       

                          trace("NAME: "+_fontData.name());               

                          for each (var att:XML in _fontData) {

                              trace("Attribute: "+att.name()+" = "+att);

                              trace(_fontData.text());

                              if(_fontData != null && _fontData.nodeKind() != 'text'){

                                  var face:String  = _fontData.@FACE;                                           

                                  var size:String  = _fontData.@SIZE;                                           

                                  var strColor:String = _fontData.@COLOR;                                           

                                  var content:String = _fontData.text();                           

                                  //apply content

                                  if(content!=""){                                       

                                      textLayer.parentStory.insertionPoints.item(-1).texts.item(0).contents = content+" ";                   

                                      //Clear the find/change text preferences.

                                      app.findTextPreferences = NothingEnum.nothing as FindTextPreference;

                                      app.changeTextPreferences = NothingEnum.nothing as ChangeTextPreference;

                                      //Search the document for the string "Text".

                                      app.findTextPreferences.findWhat = content;                                           

                                      var mFound:Object = app.activeDocument.findText();                                           

                                      var len:Object = mFound.length;

                                      if(size!=""){

                                          mFound[0].pointSize = size;//     ">0" in case of nothing found

                                      }

                                      if(face!=""){

                                          mFound[0].appliedFont = app.fonts.item(face);

                                      }

                                      if(strColor!=""){

                                          mFound[0].fillColor = myDocument.colors.add({space:ColorSpace.RGB,colorValue:[parseInt(strColor.substr(1,2),16) , parseInt(strColor.substr(3,2),16), parseInt(strColor.substr(5,2),16)]});

                                      }                                       

                                      app.findTextPreferences = NothingEnum.nothing as FindTextPreference;

                                      app.changeTextPreferences = NothingEnum.nothing as ChangeTextPreference;

                                  }                                   

           

                              }

                          }               

                          if (_fontData.children()==_fontData) {

                              trace("VALUE: "+_fontData.children());

                              trace("-----------------");

                          } else {

                              trace("This node has children:");

                              for each (var item:XML in _fontData.children()) {

                                  applyStyles(item,textLayer);

                              }

                          }   

           

           

                  }

          • 2. Re: Can we assign  html tags color/size/face to textFrame.contents of inDesign ?
            charanseeram Level 1

            Now i have to figure out how i can fix the problem of <B>bold content  </B> and <I>italic content  </I> in above method of apply styles , is their any refrence links avilable ? please let me know .