Subversion Repositories DevTools

Rev

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