例子程序演示瞭如何以編程的方式向Word文件中添加樣式並應用於文字,運行該程序需要引用Open XML SDK, 可以從此處下載:http://www.microsoft.com/download/en/details.aspx?id=5124
添加如下兩個引用到項目中:
WindowsBase
DocumentFormat.OpenXml (installed by the Open XML SDK)
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
namespace OpenXmlWordCreateAddAddCharacterStyle
{
class Program
{
static void Main(string[] args)
{
string strDoc = @"CreateAndAddCharacterStyle.docx";
using (WordprocessingDocument doc = WordprocessingDocument.Open(strDoc, true))
{
// Get the Styles part for this document.
StyleDefinitionsPart part =
doc.MainDocumentPart.StyleDefinitionsPart;
// If the Styles part does not exist, add it.
if (part == null)
{
part = AddStylesPartToPackage(doc);
}
// Create and add the character style with the style id, style name, and
// aliases specified.
CreateAndAddCharacterStyle(part,
"OverdueAmountChar",
"Overdue Amount Char",
"Late Due, Late Amount");
// Add a paragraph with a run with some text.
Paragraph p =
new Paragraph(
new Run(
new Text("this is some text ") { Space = SpaceProcessingModeValues.Preserve }));
// Add another run with some text.
p.AppendChild<Run>(new Run(new Text("in a run ") { Space = SpaceProcessingModeValues.Preserve }));
// Add another run with some text.
p.AppendChild<Run>(new Run(new Text("in a paragraph.") { Space = SpaceProcessingModeValues.Preserve }));
// Add the paragraph as a child element of the w:body.
doc.MainDocumentPart.Document.Body.AppendChild(p);
// Get a reference to the second run (indexed starting with 0).
Run r = p.Descendants<Run>().ElementAtOrDefault(1);
// If the Run has no RunProperties object, create one.
if (r.Elements<RunProperties>().Count() == 0)
{
r.PrependChild<RunProperties>(new RunProperties());
}
// Get a reference to the RunProperties.
RunProperties rPr = r.RunProperties;
// Set the character style of the run.
if (rPr.RunStyle == null)
rPr.RunStyle = new RunStyle();
rPr.RunStyle.Val = "OverdueAmountChar";
}
}
private static void CreateAndAddCharacterStyle(StyleDefinitionsPart styleDefinitionsPart,
string styleid, string stylename, string aliases = "")
{
// Get access to the root element of the styles part.
Styles styles = styleDefinitionsPart.Styles;
// Create a new character style and specify some of the attributes.
Style style = new Style()
{
Type = StyleValues.Character,
StyleId = styleid,
CustomStyle = true
};
// Create and add the child elements (properties of the style).
Aliases aliases1 = new Aliases() { Val = aliases };
StyleName styleName1 = new StyleName() { Val = stylename };
LinkedStyle linkedStyle1 = new LinkedStyle() { Val = "OverdueAmountPara" };
if (aliases != "")
style.Append(aliases1);
style.Append(styleName1);
style.Append(linkedStyle1);
// Create the StyleRunProperties object and specify some of the run properties.
StyleRunProperties styleRunProperties1 = new StyleRunProperties();
Bold bold1 = new Bold();
Color color1 = new Color() { ThemeColor = ThemeColorValues.Accent2 };
RunFonts font1 = new RunFonts() { Ascii = "Tahoma" };
Italic italic1 = new Italic();
// Specify a 24 point size.
FontSize fontSize1 = new FontSize() { Val = "48" };
styleRunProperties1.Append(font1);
styleRunProperties1.Append(fontSize1);
styleRunProperties1.Append(color1);
styleRunProperties1.Append(bold1);
styleRunProperties1.Append(italic1);
// Add the run properties to the style.
style.Append(styleRunProperties1);
// Add the style to the styles part.
styles.Append(style);
}
private static StyleDefinitionsPart AddStylesPartToPackage(WordprocessingDocument doc)
{
StyleDefinitionsPart part;
part = doc.MainDocumentPart.AddNewPart<StyleDefinitionsPart>();
Styles root = new Styles();
root.Save(part);
return part;
}
}
}
運行效果: