2 Replies Latest reply on Nov 27, 2015 11:44 PM by michaelt5330502

    C# Convert HTML Bold Style to InDesign Styles

    michaelt5330502

      Hi,

       

      I'm trying to create a function which would convert HTML "<b>" and "<strong>" tags to InDesign bold Character Styles.

      For example, the HTML code:

      <b>Bold Text</b> and another <strong>Bold Text</strong>
      
      
      
      
      

      Would result in displaying this in your browser:

      Bold Text and another Bold Text


      If I try set a paragraph to have the above text in InDesign, it would just render as-is without any bold effects.

      For example:

       

                  Application application = ActivateInDesign();//A custom function that activates InDesign
                  // Create new document
                  application.Documents.Add(true, application.DocumentPresets.FirstItem());
                  // Get active document and change some settings
                  Document document = application.ActiveDocument;
                  // Get first page (already created) and set margins
                  Page page = (Page)document.Pages[1];
                  // Add text frame object that holds texts.
                  TextFrame textFrame = page.TextFrames.Add(document.Layers.FirstItem(), idLocationOptions.idUnknown, page);
                  // Set the position and size (geometrics) of the text frame to be the whole size of the page up to the page.
                  textFrame.GeometricBounds = page.Bounds;
                  textFrame.Contents = "<b>Bold Text</b> and another <strong>Bold Text</strong>";
      
      
      
      

      Would give me this output:

      Capture.JPG

      When I really want is something like so:

      Capture.2JPG.JPG

       

      Thus I'm working on this function to convert HTML to InDesign.

      However, assuming after stripping out the tags "<b>" and "<strong>", and getting the indexes/position of the text to bold,

      I can't figure out how can I style a sub string of a paragraph in C#.

      Would anyone know how I may style a part of the paragraph?

        • 1. Re: C# Convert HTML Bold Style to InDesign Styles
          Chinnadk Level 4

          Try this,

           

          Application application = ActivateInDesign();//A custom function that activates InDesign 

          // Create new document 

          application.Documents.Add(true, application.DocumentPresets.FirstItem()); 

          // Get active document and change some settings 

          Document document = application.ActiveDocument; 

          // Get first page (already created) and set margins 

          Page page = (Page)document.Pages[1]; 

          // Add text frame object that holds texts. 

          TextFrame textFrame = page.TextFrames.Add(document.Layers.FirstItem(), idLocationOptions.idUnknown, page); 

          // Set the position and size (geometrics) of the text frame to be the whole size of the page up to the page. 

          textFrame.GeometricBounds = page.Bounds; 

          textFrame.Contents = "<b>Bold Text</b> and another <strong>Bold Text</strong>";

          application.FindGrepPreferences = application.ChangeGrepPreferences = idNothingEnum.idNothing;

          application.FindGrepPreferences.FindWhat = "(<.+?>)(.+?)(</.+?>)";

          application.ChangeGrepPreferences.ChangeTo = "$2";

          application.ChangeGrepPreferences.FontStyle = "Bold";

          document.ChangeGrep();

           

          Regards,

          Chinna

          • 2. Re: C# Convert HTML Bold Style to InDesign Styles
            michaelt5330502 Level 1

            Thanks,

            This was exactly what I needed.

             

            For anyone else with this issue, my final function looks like so:

             

                    public static void ConvertHtmlBoldToStyle(dynamic textObject, Document document, Application application, string boldCharStyleId = "CustomBold")
                    {
                        //Access the character styles, or create them with default settings if they don't exist.
                        CharacterStyle boldCharacterStyle;
                        try
                        {
                            boldCharacterStyle = document.CharacterStyles[boldCharStyleId];
                            //If the paragraph style does not exist, this will generate an exception.
                        }
                        catch (Exception ex)
                        {
                            //The paragraph style did not exist, so create it.
                            boldCharacterStyle = document.CharacterStyles.Add();
                            boldCharacterStyle.Name = boldCharStyleId;
                            boldCharacterStyle.AppliedFont = application.Fonts["Calibri"];
                            boldCharacterStyle.FontStyle = "Bold";
                        }
            
                        //Convert <b> and <strong> tags to InDesign bold character style
                        //Clear Grep preferences
                        application.FindGrepPreferences = application.ChangeGrepPreferences = idNothingEnum.idNothing;
                        //Set to only keep the 3rd capturing group, i.e. the text between the tags,
                        //stripping of the HTML tags.
                        application.ChangeGrepPreferences.ChangeTo = "$3";
                        application.ChangeGrepPreferences.AppliedCharacterStyle = boldCharacterStyle;
                        //Regex to find the tags to change
                        application.FindGrepPreferences.FindWhat = "(<strong(\\s.*)?>)(.+?)(</strong(\\s.*)?>)";
                        textObject.ChangeGrep();
            
                        application.FindGrepPreferences.FindWhat = "(<b(\\s.*)?>)(.+?)(</b(\\s.*)?>)";
                        textObject.ChangeGrep();
            
                        //Clear Grep preferences
                        application.FindGrepPreferences = application.ChangeGrepPreferences = idNothingEnum.idNothing;
                    }
            
            


            Usage:

            Application application = ActivateInDesign();//A custom function that activates InDesign 
            // Create new document 
            application.Documents.Add(true, application.DocumentPresets.FirstItem()); 
            // Get active document and change some settings 
            Document document = application.ActiveDocument; 
            // Get first page (already created) and set margins 
            Page page = (Page)document.Pages[1]; 
            // Add text frame object that holds texts. 
            TextFrame textFrame = page.TextFrames.Add(document.Layers.FirstItem(), idLocationOptions.idUnknown, page); 
            // Set the position and size (geometrics) of the text frame to be the whole size of the page up to the page. 
            textFrame.GeometricBounds = page.Bounds; 
            textFrame.Contents = "<b>Bold Text</b> and another <strong>Bold Text</strong>";
            
            ConvertHtmlBoldToStyle(textFrame, document, application);
            
            

            ConvertHtmlBoldToStyle