Subversion Repositories DevTools

Rev

Rev 2098 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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


namespace EA_DocGen
{
        /// <summary>
        /// Summary description for StyleContent.
        /// </summary>
        public class StyleContent
        {
      private Word.Application WordApp = null;
      private Word.Document WordDocument = null;

                public StyleContent(Word.Application wordApp, Word.Document wordDocument)
                {
         WordApp = wordApp;
         WordDocument = wordDocument;
                }

      public void AcceptWordAppAndDoc(Word.Application wordApp, Word.Document wordDocument)
      {
         WordApp = wordApp;
         WordDocument = wordDocument;
      }

      /// <summary>
      /// Creates a wdStyleTypeParagraph based style
      /// </summary>
      /// <param name="newStyleName"></param>
      /// <param name="followingStyleName"></param>
      /// <param name="baseStyle"></param>
      /// <returns></returns>
      public Word.Style createParagrapghStyle(string newStyleName, 
         string followingStyleName, 
         Word.Style baseStyle)
      {
         Word.Style s = getStyle(newStyleName);
         if (s == null)
         {
            object o_styleType = Word.WdStyleType.wdStyleTypeParagraph;
            s = 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 = Word.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>
      public Word.Style createCharacterStyle(string newStyleName, Word.Style baseStyle)
      {
         Word.Style s = getStyle(newStyleName);
         if (s == null)
         {
            object o_styleType = Word.WdStyleType.wdStyleTypeCharacter;
            s = WordDocument.Styles.Add(newStyleName, ref o_styleType);
            object o_baseStyle = baseStyle.NameLocal;
            s.set_BaseStyle(ref o_baseStyle);
            s.Font = baseStyle.Font;
            s.LanguageID = Word.WdLanguageID.wdEnglishAUS;
            s.NoProofing = 0;
         }
         return s;
      }


      /// <summary>
      /// Creates the styles needed for generating requirement documentation, if they do not
      /// already exist.
      /// </summary>
      public void createRequirementStylesIfNecessary()
      {
         Word.Style s_Body1 = getStyle(EA_Constants.styleName_Body1);
         if (s_Body1 != null)
         {
            // APPROVED ///////////////////////////////////////////////////////////////////////////////////////////////
            Word.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
            }
         
            Word.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 = WordApp.CentimetersToPoints((float)-2.5);
                  s_ReqAppHdr.ParagraphFormat.SpaceAfter = 3;
                  s_ReqAppHdr.ParagraphFormat.KeepWithNext = (int)MsoTriState.msoTrue;
                  s_ReqAppHdr.ParagraphFormat.TabStops.ClearAll();
                  object alignment = Word.WdTabAlignment.wdAlignTabRight;
                  object leader = Word.WdTabLeader.wdTabLeaderSpaces;
                  s_ReqAppHdr.ParagraphFormat.TabStops.Add( WordApp.CentimetersToPoints((float)16.5), ref alignment, ref leader );
               }
            }

            // PROPOSED ///////////////////////////////////////////////////////////////////////////////////////////////
            Word.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)
            {
               Word.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 ///////////////////////////////////////////////////////////////////////////////////////////////
            Word.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)
            {
               Word.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 ///////////////////////////////////////////////////////////////////////////////////////////////
            Word.Style s_DefParaFont = getStyle("Default Paragraph Font");
            if (s_DefParaFont != null)
            {
               Word.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 Word.Style getStyle(string styleText)
      {
         foreach(Word.Style aStyle in WordDocument.Styles)
         {
            //MessageBox.Show( aStyle.NameLocal );
            if (aStyle.NameLocal.StartsWith(styleText))
            {
               return aStyle;
            }
         }
         return null;
      }



        }
}