Subversion Repositories DevTools

Rev

Rev 2104 | Rev 2116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2094 ghuddy 1
using System;
2
using Word;
3
using Microsoft.Office.Core;
4
 
5
 
6
namespace EA_DocGen
7
{
8
	/// <summary>
9
	/// Summary description for StyleContent.
10
	/// </summary>
11
	public class StyleContent
12
	{
2106 ghuddy 13
		public static void initialise()
2094 ghuddy 14
		{
15
		}
16
 
17
 
18
      /// <summary>
19
      /// Creates a wdStyleTypeParagraph based style
20
      /// </summary>
21
      /// <param name="newStyleName"></param>
22
      /// <param name="followingStyleName"></param>
23
      /// <param name="baseStyle"></param>
24
      /// <returns></returns>
2106 ghuddy 25
      private static Word.Style createParagrapghStyle(string newStyleName, string followingStyleName, Word.Style baseStyle)
2094 ghuddy 26
      {
27
         Word.Style s = getStyle(newStyleName);
28
         if (s == null)
29
         {
30
            object o_styleType = Word.WdStyleType.wdStyleTypeParagraph;
2106 ghuddy 31
            s = createWordDoc.WordDocument.Styles.Add(newStyleName, ref o_styleType);
2094 ghuddy 32
 
33
            object o_baseStyle = baseStyle.NameLocal;
34
            object o_followingStyle = followingStyleName;
35
            s.set_BaseStyle(ref o_baseStyle);
36
            s.set_NextParagraphStyle(ref o_followingStyle);
37
 
38
            s.Font = baseStyle.Font;
39
            s.ParagraphFormat = baseStyle.ParagraphFormat;
40
            s.LanguageID = Word.WdLanguageID.wdEnglishAUS;
41
            s.NoProofing = 0;
42
            s.Frame.Delete();
43
            s.NoSpaceBetweenParagraphsOfSameStyle = false;
44
 
45
         }
46
         return s;
47
      }
48
 
49
      /// <summary>
50
      /// Creates a wdStyleTypeCharacter based style
51
      /// </summary>
52
      /// <param name="newStyleName"></param>
53
      /// <param name="baseStyle"></param>
54
      /// <returns></returns>
2106 ghuddy 55
      private static Word.Style createCharacterStyle(string newStyleName, Word.Style baseStyle)
2094 ghuddy 56
      {
57
         Word.Style s = getStyle(newStyleName);
58
         if (s == null)
59
         {
60
            object o_styleType = Word.WdStyleType.wdStyleTypeCharacter;
2106 ghuddy 61
            s = createWordDoc.WordDocument.Styles.Add(newStyleName, ref o_styleType);
2094 ghuddy 62
            object o_baseStyle = baseStyle.NameLocal;
63
            s.set_BaseStyle(ref o_baseStyle);
64
            s.Font = baseStyle.Font;
65
            s.LanguageID = Word.WdLanguageID.wdEnglishAUS;
66
            s.NoProofing = 0;
67
         }
68
         return s;
69
      }
70
 
2104 ghuddy 71
      /// <summary>
72
      /// This method is designed to correct problems that have been reported in the styles
73
      /// contained in the input templates being used. These can be corrected programmatically
74
      /// much quicker than waiting for a fix to be done to the templates and having them re-deployed
75
      /// to the shared drive where MS-Word templates are retrieved from by most users.
76
      /// </summary>
2106 ghuddy 77
      public static void correctErrorsInTemplateStyles()
2104 ghuddy 78
      {
79
         // Go through the document's list levels and make sure any faults are fixed up
80
         int i = 0;
81
         int j = 0;
2106 ghuddy 82
         foreach (Word.ListTemplate lt in createWordDoc.WordDocument.ListTemplates)
2104 ghuddy 83
         {
84
            j = 0;
85
            foreach (Word.ListLevel lv in lt.ListLevels)
86
            {
87
               // An old ERG template had a flaw where the list numbering restarts were not 
88
               // configured correctly, so fix up if needed.
89
               if (lv.ResetOnHigher != j)
90
                  lv.ResetOnHigher = j;
91
               j++;
92
            }
93
            i++;
94
         }
2094 ghuddy 95
 
2104 ghuddy 96
         // Ensure heading levels 7 to 9 are bold
97
         Word.Style h7 = getStyle(EA_Constants.styleName_Heading7);
98
         if (h7 != null)
99
         {
100
            h7.Font.Bold = 1;
101
         }
102
         Word.Style h8 = getStyle(EA_Constants.styleName_Heading8);
103
         if (h8 != null)
104
         {
105
            h8.Font.Bold = 1;
106
         }
107
         Word.Style h9 = getStyle(EA_Constants.styleName_Heading9);
108
         if (h9 != null)
109
         {
110
            h9.Font.Bold = 1;
111
         }
112
      }
113
 
114
      /// <summary>
115
      /// This method creates a heading level style for heading levels 10 or above. Although MS-Word 
116
      /// does not support level numbering above 9, EA_DocGen can fake the heading number and use the
117
      /// styles created in this method for the fake headings.
118
      /// </summary>
2106 ghuddy 119
      public static void createAdditionalHeadingLevelStyles()
2104 ghuddy 120
      {
121
         Word.Style s_Body1 = getStyle(EA_Constants.styleName_Body1);
122
         if (s_Body1 != null)
123
         {
124
            Word.Style h10 = createParagrapghStyle(EA_Constants.styleName_Heading10OrAbove, EA_Constants.styleName_Body1, s_Body1);
125
            h10.Font.Size = 10;
126
            h10.Font.Bold = 1;
2106 ghuddy 127
            h10.ParagraphFormat.LeftIndent  = createWordDoc.WordApp.CentimetersToPoints((float)0.0);
2104 ghuddy 128
            h10.ParagraphFormat.SpaceAfter  = 6;
129
            h10.ParagraphFormat.SpaceBefore = 12;
130
         }
131
      }
132
 
133
      /// <summary>
134
      /// Create the styles used to display class element details
135
      /// </summary>
2106 ghuddy 136
      public static void createElementDetailsStyles()
2098 ghuddy 137
      {
138
         Word.Style s_Body1 = getStyle(EA_Constants.styleName_Body1);
139
         if (s_Body1 != null)
140
         {
141
            // A couple of indented body 1 styles
142
            Word.Style s_B1_3_5 = createParagrapghStyle(EA_Constants.styleName_Body1_Left_3_5cm, EA_Constants.styleName_Body1_Left_3_5cm, s_Body1);
2106 ghuddy 143
            s_B1_3_5.ParagraphFormat.LeftIndent = createWordDoc.WordApp.CentimetersToPoints((float)3.5);
2098 ghuddy 144
 
145
            Word.Style s_B1_4_5 = createParagrapghStyle(EA_Constants.styleName_Body1_Left_4_5cm, EA_Constants.styleName_Body1_Left_4_5cm, s_Body1);
2106 ghuddy 146
            s_B1_4_5.ParagraphFormat.LeftIndent = createWordDoc.WordApp.CentimetersToPoints((float)4.5);
2098 ghuddy 147
 
148
            // A couple of pseudo-heading styles
149
            Word.Style s_B1_2_5_Italic = createParagrapghStyle(EA_Constants.styleName_Body1_Left_2_5cm_Italic, EA_Constants.styleName_Body1_Left_3_5cm, s_Body1);
2106 ghuddy 150
            s_B1_2_5_Italic.ParagraphFormat.LeftIndent = createWordDoc.WordApp.CentimetersToPoints((float)2.5);
2098 ghuddy 151
            s_B1_2_5_Italic.Font.Italic = 1;
152
            s_B1_2_5_Italic.Font.Bold = 1;
153
 
154
            Word.Style s_B1_3_5_Italic = createParagrapghStyle(EA_Constants.styleName_Body1_Left_3_5cm_Italic, EA_Constants.styleName_Body1_Left_4_5cm, s_Body1);
2106 ghuddy 155
            s_B1_3_5_Italic.ParagraphFormat.LeftIndent = createWordDoc.WordApp.CentimetersToPoints((float)3.5);
2098 ghuddy 156
            s_B1_3_5_Italic.Font.Italic = 1;
157
            s_B1_3_5_Italic.Font.Bold = 1;
158
 
159
            // Style for use when detailing parameter descriptions
160
            Word.Style s_ParamDesc = createParagrapghStyle(EA_Constants.stylename_Parameter_Description, EA_Constants.styleName_Body1, s_Body1);
161
            s_ParamDesc.ParagraphFormat.TabStops.ClearAll();
162
            object alignment = Word.WdTabAlignment.wdAlignTabLeft;
163
            object leader = Word.WdTabLeader.wdTabLeaderSpaces;
2106 ghuddy 164
            s_ParamDesc.ParagraphFormat.LeftIndent = createWordDoc.WordApp.CentimetersToPoints((float)9.0);
165
            s_ParamDesc.ParagraphFormat.TabStops.Add( createWordDoc.WordApp.CentimetersToPoints((float)9.0), ref alignment, ref leader );
166
            s_ParamDesc.ParagraphFormat.FirstLineIndent = createWordDoc.WordApp.CentimetersToPoints((float)-5.5);
2098 ghuddy 167
         }
168
      }
169
 
2094 ghuddy 170
      /// <summary>
171
      /// Creates the styles needed for generating requirement documentation, if they do not
172
      /// already exist.
173
      /// </summary>
2106 ghuddy 174
      public static void createRequirementStyles()
2094 ghuddy 175
      {
176
         Word.Style s_Body1 = getStyle(EA_Constants.styleName_Body1);
177
         if (s_Body1 != null)
178
         {
179
            // APPROVED ///////////////////////////////////////////////////////////////////////////////////////////////
180
            Word.Style s_ReqAppBody = createParagrapghStyle(EA_Constants.styleName_ReqAppBody, EA_Constants.styleName_ReqAppBody, s_Body1);
181
            if (s_ReqAppBody != null)
182
            {
183
               // No changes needed - this is just a copy of Body 1
184
            }
185
 
186
            Word.Style s_ReqAppHdr = null;
187
            if (s_ReqAppBody != null)
188
            {
189
               s_ReqAppHdr = createParagrapghStyle(EA_Constants.styleName_ReqAppHdr, EA_Constants.styleName_ReqAppBody, s_Body1);
190
               if (s_ReqAppHdr != null)
191
               {
192
                  // Indent:Hanging: 2.5cm, Space After: 3pt, Keep with next, Tabs: 16.5cm, Right
2106 ghuddy 193
                  s_ReqAppHdr.ParagraphFormat.FirstLineIndent = createWordDoc.WordApp.CentimetersToPoints((float)-2.5);
2094 ghuddy 194
                  s_ReqAppHdr.ParagraphFormat.SpaceAfter = 3;
195
                  s_ReqAppHdr.ParagraphFormat.KeepWithNext = (int)MsoTriState.msoTrue;
196
                  s_ReqAppHdr.ParagraphFormat.TabStops.ClearAll();
197
                  object alignment = Word.WdTabAlignment.wdAlignTabRight;
198
                  object leader = Word.WdTabLeader.wdTabLeaderSpaces;
2106 ghuddy 199
                  s_ReqAppHdr.ParagraphFormat.TabStops.Add( createWordDoc.WordApp.CentimetersToPoints((float)16.5), ref alignment, ref leader );
2094 ghuddy 200
               }
201
            }
202
 
203
            // PROPOSED ///////////////////////////////////////////////////////////////////////////////////////////////
204
            Word.Style s_ReqPropBody = null;
205
            if (s_ReqAppBody != null)
206
            {
207
               s_ReqPropBody = createParagrapghStyle(EA_Constants.styleName_ReqPropBody, EA_Constants.styleName_ReqPropBody, s_ReqAppBody );
208
               if (s_ReqPropBody != null)
209
               {
210
                  // Font: Italic
211
                  s_ReqPropBody.Font.Italic = (int)MsoTriState.msoTrue;
212
               }
213
            }
214
 
215
            if (s_ReqAppHdr != null && s_ReqPropBody != null)
216
            {
217
               Word.Style s_ReqPropHdr = createParagrapghStyle(EA_Constants.styleName_ReqPropHdr, EA_Constants.styleName_ReqPropBody, s_ReqAppHdr );
218
               if (s_ReqPropHdr != null)
219
               {
220
                  // Font: Italic
221
                  s_ReqPropHdr.Font.Italic = (int)MsoTriState.msoTrue;
222
               }
223
            }
224
 
225
            // REJECTED ///////////////////////////////////////////////////////////////////////////////////////////////
226
            Word.Style s_ReqRejBody = null;
227
            if (s_ReqAppBody != null)
228
            {
229
               s_ReqRejBody = createParagrapghStyle(EA_Constants.styleName_ReqRejBody, EA_Constants.styleName_ReqRejBody, s_ReqAppBody );
230
               if (s_ReqRejBody != null)
231
               {
232
                  // Font: Italic
233
                  s_ReqRejBody.Font.StrikeThrough = (int)MsoTriState.msoTrue;
234
               }
235
            }
236
 
237
            if (s_ReqAppHdr != null && s_ReqRejBody != null)
238
            {
239
               Word.Style s_ReqRejHdr = createParagrapghStyle(EA_Constants.styleName_ReqRejHdr, EA_Constants.styleName_ReqRejBody, s_ReqAppHdr );
240
               if (s_ReqRejHdr != null)
241
               {
242
                  // Font: Italic
243
                  s_ReqRejHdr.Font.StrikeThrough = (int)MsoTriState.msoTrue;
244
               }
245
            }
246
 
247
            // REQUIREMENT NAME ///////////////////////////////////////////////////////////////////////////////////////////////
248
            Word.Style s_DefParaFont = getStyle("Default Paragraph Font");
249
            if (s_DefParaFont != null)
250
            {
251
               Word.Style s_ReqName = createCharacterStyle(EA_Constants.styleName_ReqName, s_DefParaFont );
252
               if (s_ReqName != null)
253
               {
254
                  s_ReqName.Font.Bold = (int)MsoTriState.msoTrue;
255
               }
256
            }
257
         }
258
      }
259
 
260
      /// <summary>
261
      /// This function was the only way I could figure out how to obtain a style object for
262
      /// the style name given. Everything else I tried, didn't work, even code from Visual Basic
263
      /// examples failed to work or even compile (once converted) in C#.
264
      /// </summary>
265
      /// <param name="styleText"></param>
266
      /// <returns></returns>
2106 ghuddy 267
      public static Word.Style getStyle(string styleText)
2094 ghuddy 268
      {
2106 ghuddy 269
         foreach(Word.Style aStyle in createWordDoc.WordDocument.Styles)
2094 ghuddy 270
         {
271
            //MessageBox.Show( aStyle.NameLocal );
2104 ghuddy 272
            if (aStyle.NameLocal.Equals(styleText))
2094 ghuddy 273
            {
274
               return aStyle;
275
            }
276
         }
277
         return null;
278
      }
279
 
280
 
281
 
282
	}
283
}