【问题标题】:Add existent style to a Paragraph in OpenXML将现有样式添加到 OpenXML 中的段落
【发布时间】:2017-06-07 06:58:49
【问题描述】:

我需要使用 OpenXML 为段落添加样式。我有以下代码,它正在运行,但样式不是我要添加的样式。

var file = WordprocessingDocument.Open(fileName, true); 

var text = new Text("Hello world");
var run = new Run(text);
var paragraph = new Paragraph(run);
paragraph.ParagraphProperties = new ParagraphProperties(
new ParagraphStyleId() { Val = "Body Text" });            

file.MainDocumentPart.Document.Body.AppendChild(paragraph);
file.MainDocumentPart.Document.Save();

有什么我做错了吗??? 如何使用 OpenXML 将一些现有样式添加到段落中。

【问题讨论】:

  • 如何定义"Body Text"模板?
  • 我没有正文文本的定义,我认为样式就像字体一样,您只需输入所需字体的名称即可。我在 Word 中有一个设计,我需要创建具有相同格式的代码,所以我试图在某些段落中使用该段落在原始文档中的样式。
  • 查看post
  • This post 可能对你也有用。

标签: .net ms-word openxml paragraph


【解决方案1】:

如图here

private static void AddNewStyle(StyleDefinitionsPart styleDefinitionsPart, 
    string styleid, string stylename)
{
    // Get access to the root element of the styles part.
    Styles styles = styleDefinitionsPart.Styles;

    // Create a new paragraph style and specify some of the properties.
    Style style = new Style() { Type = StyleValues.Paragraph, 
        StyleId = styleid, 
        CustomStyle = true };
    StyleName styleName1 = new StyleName() { Val = stylename };
    BasedOn basedOn1 = new BasedOn() { Val = "Normal" };
    NextParagraphStyle nextParagraphStyle1 = new NextParagraphStyle() { Val = "Normal" };
    style.Append(styleName1);
    style.Append(basedOn1);
    style.Append(nextParagraphStyle1);

    // 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 = "Lucida Console" };
    Italic italic1 = new Italic();
    // Specify a 12 point size.
    FontSize fontSize1 = new FontSize() { Val = "24" };
    styleRunProperties1.Append(bold1);
    styleRunProperties1.Append(color1);
    styleRunProperties1.Append(font1);
    styleRunProperties1.Append(fontSize1);
    styleRunProperties1.Append(italic1);

    // Add the run properties to the style.
    style.Append(styleRunProperties1);

    // Add the style to the styles part.
    styles.Append(style);
}

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 2016-01-19
    相关资源
    最近更新 更多