Subversion Repositories DevTools

Rev

Rev 2132 | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;


namespace EA_DocGen
{
        /// <summary>
        /// Summary description for StyleContent.
        /// </summary>
        public class StyleContent
        {
                public static void initialise()
                {
                }


      /// <summary>
      /// Creates a wdStyleTypeParagraph based style
      /// </summary>
      /// <param name="newStyleName"></param>
      /// <param name="followingStyleName"></param>
      /// <param name="baseStyle"></param>
      /// <returns></returns>
      private static Style createParagrapghStyle(string newStyleName, string followingStyleName, Style baseStyle)
      {
         Style s = getStyle(newStyleName);
         if (s == null)
         {
            object o_styleType = WdStyleType.wdStyleTypeParagraph;
            s = createWordDoc.WordDocument.Styles.Add(newStyleName, ref o_styleType);

            object o_baseStyle = baseStyle.NameLocal;
            object o_followingStyle = followingStyleName;
            s.set_BaseStyle(ref o_baseStyle);
            s.set_NextParagraphStyle(ref o_followingStyle);

            s.Font = baseStyle.Font;
            s.ParagraphFormat = baseStyle.ParagraphFormat;
            s.LanguageID = WdLanguageID.wdEnglishAUS;
            s.NoProofing = 0;
            s.Frame.Delete();
            s.NoSpaceBetweenParagraphsOfSameStyle = false;
            
         }
         return s;
      }

      /// <summary>
      /// Creates a wdStyleTypeCharacter based style
      /// </summary>
      /// <param name="newStyleName"></param>
      /// <param name="baseStyle"></param>
      /// <returns></returns>
        private static Style createCharacterStyle(string newStyleName, Style baseStyle)
      {
         Style s = getStyle(newStyleName);
         if (s == null)
         {
            object o_styleType = WdStyleType.wdStyleTypeCharacter;
            s = createWordDoc.WordDocument.Styles.Add(newStyleName, ref o_styleType);
            object o_baseStyle = baseStyle.NameLocal;
            s.set_BaseStyle(ref o_baseStyle);
            s.Font = baseStyle.Font;
            s.LanguageID = WdLanguageID.wdEnglishAUS;
            s.NoProofing = 0;
         }
         return s;
      }

      public static void fixupNumParaStyles()
      {
         // Remove all NumPara styles, some of which are known to be faulty.
         Style np = getStyle(EA_Constants.styleName_NumPara1);
         if (np != null) np.Delete();
         np = getStyle(EA_Constants.styleName_NumPara2);
         if (np != null) np.Delete();
         np = getStyle(EA_Constants.styleName_NumPara3);
         if (np != null) np.Delete();
         np = getStyle(EA_Constants.styleName_NumPara4);
         if (np != null) np.Delete();
         np = getStyle(EA_Constants.styleName_NumPara5);
         if (np != null) np.Delete();
         np = getStyle(EA_Constants.styleName_NumPara6);
         if (np != null) np.Delete();
         np = getStyle(EA_Constants.styleName_NumPara7);
         if (np != null) np.Delete();
         np = getStyle(EA_Constants.styleName_NumPara8);
         if (np != null) np.Delete();
         np = getStyle(EA_Constants.styleName_NumPara9);
         if (np != null) np.Delete();
         np = getStyle("Numpara 6");   // ERG template has this one too - Note the lower case p
         if (np != null) np.Delete();

         // Re-Create Good Num Para styles based on the now good Heading styles
         Style baseStyle;
         baseStyle = getStyle(EA_Constants.styleName_Heading1);
         if (baseStyle != null)
         {
            np = createParagrapghStyle(EA_Constants.styleName_NumPara1, EA_Constants.styleName_Body1, baseStyle);
            np.Font.Size = 10;
            np.Font.Bold = 0;
         }
         baseStyle = getStyle(EA_Constants.styleName_Heading2);
         if (baseStyle != null)
         {
            np = createParagrapghStyle(EA_Constants.styleName_NumPara2, EA_Constants.styleName_Body1, baseStyle);
            np.Font.Size = 10;
            np.Font.Bold = 0;
         }
         baseStyle = getStyle(EA_Constants.styleName_Heading3);
         if (baseStyle != null)
         {
            np = createParagrapghStyle(EA_Constants.styleName_NumPara3, EA_Constants.styleName_Body1, baseStyle);
            np.Font.Size = 10;
            np.Font.Bold = 0;
         }
         baseStyle = getStyle(EA_Constants.styleName_Heading4);
         if (baseStyle != null)
         {
            np = createParagrapghStyle(EA_Constants.styleName_NumPara4, EA_Constants.styleName_Body1, baseStyle);
            np.Font.Size = 10;
            np.Font.Bold = 0;
         }
         baseStyle = getStyle(EA_Constants.styleName_Heading5);
         if (baseStyle != null)
         {
            np = createParagrapghStyle(EA_Constants.styleName_NumPara5, EA_Constants.styleName_Body1, baseStyle);
            np.Font.Size = 10;
            np.Font.Bold = 0;
         }
         baseStyle = getStyle(EA_Constants.styleName_Heading6);
         if (baseStyle != null)
         {
            np = createParagrapghStyle(EA_Constants.styleName_NumPara6, EA_Constants.styleName_Body1, baseStyle);
            np.Font.Size = 10;
            np.Font.Bold = 0;
         }
         baseStyle = getStyle(EA_Constants.styleName_Heading7);
         if (baseStyle != null)
         {
            np = createParagrapghStyle(EA_Constants.styleName_NumPara7, EA_Constants.styleName_Body1, baseStyle);
            np.Font.Size = 10;
            np.Font.Bold = 0;
         }
         baseStyle = getStyle(EA_Constants.styleName_Heading8);
         if (baseStyle != null)
         {
            np = createParagrapghStyle(EA_Constants.styleName_NumPara8, EA_Constants.styleName_Body1, baseStyle);
            np.Font.Size = 10;
            np.Font.Bold = 0;
         }
         baseStyle = getStyle(EA_Constants.styleName_Heading9);
         if (baseStyle != null)
         {
            np = createParagrapghStyle(EA_Constants.styleName_NumPara9, EA_Constants.styleName_Body1, baseStyle);
            np.Font.Size = 10;
            np.Font.Bold = 0;
         }
      }

      public static void forceHeadingsToUse2_5cmTabStops()
      {
         object alignment = WdTabAlignment.wdAlignTabLeft;
         object leader = WdTabLeader.wdTabLeaderSpaces;
         Style h = getStyle(EA_Constants.styleName_Heading6);
         if (h != null)
         {
            h.ParagraphFormat.TabStops.ClearAll();
            h.ParagraphFormat.TabStops.Add((float)2.5, ref alignment, ref leader);
         }
         h = getStyle(EA_Constants.styleName_Heading7);
         if (h != null)
         {
            h.ParagraphFormat.TabStops.ClearAll();
            h.ParagraphFormat.TabStops.Add((float)2.5, ref alignment, ref leader);
         }
         h = getStyle(EA_Constants.styleName_Heading8);
         if (h != null)
         {
            h.ParagraphFormat.TabStops.ClearAll();
            h.ParagraphFormat.TabStops.Add((float)2.5, ref alignment, ref leader);
         }
         h = getStyle(EA_Constants.styleName_Heading9);
         if (h != null)
         {
            h.ParagraphFormat.TabStops.ClearAll();
            h.ParagraphFormat.TabStops.Add((float)2.5, ref alignment, ref leader);
         }
      }

      /// <summary>
      /// This method is designed to correct problems that have been reported in the styles
      /// contained in the input templates being used. These can be corrected programmatically
      /// much quicker than waiting for a fix to be done to the templates and having them re-deployed
      /// to the shared drive where MS-Word templates are retrieved from by most users.
      /// </summary>
      public static void correctErrorsInTemplateStyles()
      {
         // Go through the document's list levels and make sure any faults are fixed up
         int i = 0;
         int j = 0;
         foreach (ListTemplate lt in createWordDoc.WordDocument.ListTemplates)
         {
            j = 0;
            foreach (ListLevel lv in lt.ListLevels)
            {
               // An old ERG template had a flaw where the list numbering restarts were not 
               // configured correctly, so fix up if needed.
               if (lv.ResetOnHigher != j)
                  lv.ResetOnHigher = j;
               j++;
            }
            i++;
         }

         // Ensure heading levels 7 to 9 are bold
         Style h7 = getStyle(EA_Constants.styleName_Heading7);
         if (h7 != null)
         {
            h7.Font.Bold = 1;
         }
         Style h8 = getStyle(EA_Constants.styleName_Heading8);
         if (h8 != null)
         {
            h8.Font.Bold = 1;
         }
         Style h9 = getStyle(EA_Constants.styleName_Heading9);
         if (h9 != null)
         {
            h9.Font.Bold = 1;
         }

         fixupNumParaStyles();
      }

      /// <summary>
      /// This method creates a heading level style for heading levels 10 or above. Although MS-Word 
      /// does not support level numbering above 9, EA_DocGen can fake the heading number and use the
      /// styles created in this method for the fake headings.
      /// </summary>
      public static void createAdditionalHeadingLevelStyles()
      {
         Style s_Body1 = getStyle(EA_Constants.styleName_Body1);
         if (s_Body1 != null)
         {
            // Heading 10 or above
            Style h10 = createParagrapghStyle(EA_Constants.styleName_Heading10OrAbove, EA_Constants.styleName_Body1, s_Body1);
            h10.Font.Size = 10;
            h10.Font.Bold = 1;
            h10.ParagraphFormat.LeftIndent  = createWordDoc.WordApp.CentimetersToPoints((float)0.0);
            h10.ParagraphFormat.SpaceAfter  = 6;
            h10.ParagraphFormat.SpaceBefore = 12;

            // NumPara 10 or above
            Style np10 = createParagrapghStyle(EA_Constants.styleName_NumPara10OrAbove, EA_Constants.styleName_Body1, s_Body1);
            h10.Font.Size = 10;
            h10.Font.Bold = 0;
            h10.ParagraphFormat.LeftIndent  = createWordDoc.WordApp.CentimetersToPoints((float)0.0);
            h10.ParagraphFormat.SpaceAfter  = 6;
            h10.ParagraphFormat.SpaceBefore = 12;
         }
      }

      /// <summary>
      /// Create the styles used to display class element details
      /// </summary>
      public static void createElementDetailsStyles()
      {
         Style s_Body1 = getStyle(EA_Constants.styleName_Body1);
         if (s_Body1 != null)
         {
            // A couple of indented body 1 styles
            Style s_B1_3_5 = createParagrapghStyle(EA_Constants.styleName_Body1_Left_3_5cm, EA_Constants.styleName_Body1_Left_3_5cm, s_Body1);
            s_B1_3_5.ParagraphFormat.LeftIndent = createWordDoc.WordApp.CentimetersToPoints((float)3.5);

            Style s_B1_4_5 = createParagrapghStyle(EA_Constants.styleName_Body1_Left_4_5cm, EA_Constants.styleName_Body1_Left_4_5cm, s_Body1);
            s_B1_4_5.ParagraphFormat.LeftIndent = createWordDoc.WordApp.CentimetersToPoints((float)4.5);

            // A couple of pseudo-heading styles
            Style s_B1_2_5_Italic = createParagrapghStyle(EA_Constants.styleName_Body1_Left_2_5cm_Italic, EA_Constants.styleName_Body1_Left_3_5cm, s_Body1);
            s_B1_2_5_Italic.ParagraphFormat.LeftIndent = createWordDoc.WordApp.CentimetersToPoints((float)2.5);
            s_B1_2_5_Italic.Font.Italic = 1;
            s_B1_2_5_Italic.Font.Bold = 1;

            Style s_B1_3_5_Italic = createParagrapghStyle(EA_Constants.styleName_Body1_Left_3_5cm_Italic, EA_Constants.styleName_Body1_Left_4_5cm, s_Body1);
            s_B1_3_5_Italic.ParagraphFormat.LeftIndent = createWordDoc.WordApp.CentimetersToPoints((float)3.5);
            s_B1_3_5_Italic.Font.Italic = 1;
            s_B1_3_5_Italic.Font.Bold = 1;

            // Style for use when detailing parameter descriptions
            Style s_ParamDesc = createParagrapghStyle(EA_Constants.stylename_Parameter_Description, EA_Constants.styleName_Body1, s_Body1);
            s_ParamDesc.ParagraphFormat.TabStops.ClearAll();
            object alignment = WdTabAlignment.wdAlignTabLeft;
            object leader = WdTabLeader.wdTabLeaderSpaces;
            s_ParamDesc.ParagraphFormat.LeftIndent = createWordDoc.WordApp.CentimetersToPoints((float)9.0);
            s_ParamDesc.ParagraphFormat.TabStops.Add( createWordDoc.WordApp.CentimetersToPoints((float)9.0), ref alignment, ref leader );
            s_ParamDesc.ParagraphFormat.FirstLineIndent = createWordDoc.WordApp.CentimetersToPoints((float)-5.5);
         }
      }

      /// <summary>
      /// Creates the styles needed for generating requirement documentation, if they do not
      /// already exist.
      /// </summary>
      public static void createRequirementStyles()
      {
         Style s_Body1 = getStyle(EA_Constants.styleName_Body1);
         if (s_Body1 != null)
         {
            // APPROVED ///////////////////////////////////////////////////////////////////////////////////////////////
            Style s_ReqAppBody = createParagrapghStyle(EA_Constants.styleName_ReqAppBody, EA_Constants.styleName_ReqAppBody, s_Body1);
            if (s_ReqAppBody != null)
            {
               // No changes needed - this is just a copy of Body 1
            }
         
            Style s_ReqAppHdr = null;
            if (s_ReqAppBody != null)
            {
               s_ReqAppHdr = createParagrapghStyle(EA_Constants.styleName_ReqAppHdr, EA_Constants.styleName_ReqAppBody, s_Body1);
               if (s_ReqAppHdr != null)
               {
                  // Indent:Hanging: 2.5cm, Space After: 3pt, Keep with next, Tabs: 16.5cm, Right
                  s_ReqAppHdr.ParagraphFormat.FirstLineIndent = createWordDoc.WordApp.CentimetersToPoints((float)-2.5);
                  s_ReqAppHdr.ParagraphFormat.SpaceAfter = 3;
                  s_ReqAppHdr.ParagraphFormat.KeepWithNext = (int)MsoTriState.msoTrue;
                  s_ReqAppHdr.ParagraphFormat.TabStops.ClearAll();
                  object alignment = WdTabAlignment.wdAlignTabRight;
                  object leader = WdTabLeader.wdTabLeaderSpaces;
                  s_ReqAppHdr.ParagraphFormat.TabStops.Add( createWordDoc.WordApp.CentimetersToPoints((float)16.5), ref alignment, ref leader );
               }
            }

            // PROPOSED ///////////////////////////////////////////////////////////////////////////////////////////////
            Style s_ReqPropBody = null;
            if (s_ReqAppBody != null)
            {
               s_ReqPropBody = createParagrapghStyle(EA_Constants.styleName_ReqPropBody, EA_Constants.styleName_ReqPropBody, s_ReqAppBody );
               if (s_ReqPropBody != null)
               {
                  // Font: Italic
                  s_ReqPropBody.Font.Italic = (int)MsoTriState.msoTrue;
               }
            }

            if (s_ReqAppHdr != null && s_ReqPropBody != null)
            {
               Style s_ReqPropHdr = createParagrapghStyle(EA_Constants.styleName_ReqPropHdr, EA_Constants.styleName_ReqPropBody, s_ReqAppHdr );
               if (s_ReqPropHdr != null)
               {
                  // Font: Italic
                  s_ReqPropHdr.Font.Italic = (int)MsoTriState.msoTrue;
               }
            }

            // REJECTED ///////////////////////////////////////////////////////////////////////////////////////////////
            Style s_ReqRejBody = null;
            if (s_ReqAppBody != null)
            {
               s_ReqRejBody = createParagrapghStyle(EA_Constants.styleName_ReqRejBody, EA_Constants.styleName_ReqRejBody, s_ReqAppBody );
               if (s_ReqRejBody != null)
               {
                  // Font: Italic
                  s_ReqRejBody.Font.StrikeThrough = (int)MsoTriState.msoTrue;
               }
            }

            if (s_ReqAppHdr != null && s_ReqRejBody != null)
            {
               Style s_ReqRejHdr = createParagrapghStyle(EA_Constants.styleName_ReqRejHdr, EA_Constants.styleName_ReqRejBody, s_ReqAppHdr );
               if (s_ReqRejHdr != null)
               {
                  // Font: Italic
                  s_ReqRejHdr.Font.StrikeThrough = (int)MsoTriState.msoTrue;
               }
            }

            // REQUIREMENT NAME ///////////////////////////////////////////////////////////////////////////////////////////////
            Style s_DefParaFont = getStyle("Default Paragraph Font");
            if (s_DefParaFont != null)
            {
               Style s_ReqName = createCharacterStyle(EA_Constants.styleName_ReqName, s_DefParaFont );
               if (s_ReqName != null)
               {
                  s_ReqName.Font.Bold = (int)MsoTriState.msoTrue;
               }
            }
         }
      }

      /// <summary>
      /// This function was the only way I could figure out how to obtain a style object for
      /// the style name given. Everything else I tried, didn't work, even code from Visual Basic
      /// examples failed to work or even compile (once converted) in C#.
      /// </summary>
      /// <param name="styleText"></param>
      /// <returns></returns>
      public static Style getStyle(string styleText)
      {
         foreach(Style aStyle in createWordDoc.WordDocument.Styles)
         {
            //MessageBox.Show( aStyle.NameLocal );
            if (aStyle.NameLocal.Equals(styleText))
            {
               return aStyle;
            }
         }
         return null;
      }



        }
}