无法在 Open Xml 中使用现有的段落样式 [英] Unable to use existing paragraph styles in Open Xml

查看:22
本文介绍了无法在 Open Xml 中使用现有的段落样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 HTML 文件导出到 Open XML wordfile.如果在 HTML 中使用 <h1> ,我想向该部分添加 Heading1 样式.但不知何故,当我在 Microsoft Word 2010 中打开文档时,没有应用样式.

I'm working on an export of an HTML file to a Open XML wordfile. If in the HTML <h1> is used, I want to add a Heading1 style to that part. But somehow when I open the document in Microsoft Word 2010, the Style isn't applied.

如果我在 Libre Office 中打开创建的文档,就会应用一些样式.

If I open the created document in Libre Office, there is some style applied.

我自己也定义了一些样式,如果我使用其中一种样式,Word 和 Libre Office 中的一切都会很顺利.

I also defined some styles myself, and if I use one of those styles, everything went well in Word and Libre Office.

我打开了 Open XML SDK 2.5 Productivity Tool for Microsoft Office,当我查看它提供的示例代码时,它提示:

I opened the Open XML SDK 2.5 Productivity Tool for Microsoft Office and when I look in the example code it provides, it suggests:

ParagraphStyleIdparagraphStyleId1 = new ParagraphStyleId(){ Val = "Kop1" };

Kop1(而不是 Heading1)是因为我的 Word 是荷兰语,所以我的模板是荷兰语,但这并没有解决问题.

Kop1 (instead of Heading1) is because my Word is in Dutch so my template is in Dutch, but this didn't solve the problem.

在此代码示例中,我创建了段落并为其添加了样式和文本:

In this code sample I create the paragraph and add style and text to it:

using (wordDocument = WordprocessingDocument.Open(documentStream, true)) 
{
    MainDocumentPart mainPart = wordDocument.MainDocumentPart;
    WP.Body body = wordDocument.MainDocumentPart.Document.Body;
    WP.Paragraph para = body.AppendChild(new WP.Paragraph());
    StyleDefinitionsPart part = wordDocument.MainDocumentPart.StyleDefinitionsPart;
    if (part != null)
    {
        WP.ParagraphProperties pPr = new WP.ParagraphProperties();
        WP.ParagraphStyleId paragraphStyleId1 = new WP.ParagraphStyleId() { Val = "Heading1" };
        pPr.Append(paragraphStyleId1);

        para.Append(pPr);
    }

    WP.Run run = para.AppendChild(new WP.Run());
    run.AppendChild(new WP.Text(value));
}

它定义了样式,因为我使用的是模板,所以代码会出现在 if 语句中.

It has styles defined, because I work with a template, so the code will come inside the if-statement.

我也用title(荷兰语title)样式尝试过,两种都试过,但都不起作用......

I also tried it with the style title (in Dutch titel), tried both but both didn't work...

我真的不明白出了什么问题以及为什么我可以使用我自己创建的样式但不能使用 Word 的一种预定义样式,而我没有使用不在模板中删除.

I really don't understand what's wrong and why I can use my own created styles but can't use one of the predefined styles of Word, which I didn't remove in the template.

不知何故无法识别预定义的样式?

It somehow doesn't recognize the predefined styles?

编辑/添加

我让它工作了,但不是我想要的方式,我必须点击模板文档中的预定义样式,然后保存文档.现在以某种方式单击它们并保存这些样式后,这些样式会添加到文档中,因为如果我现在使用模板生成最终文档,我可以使用这些样式.但是不先点击文档中的样式,我不能使用它们,因为它们没有保存?

I got it working, but not the way I want, I have to click on the predefined styles in the template-document and then save the document. Somehow now after clicking them and saving these styles are added to the document, because if I now generate my final document using the template, I can use the styles. But without clicking the styles in the document first, I can't use them becuase they aren't saved?

推荐答案

不幸的是,发生这种情况是因为默认情况下未将样式写入文档(即使它内置"到 Word 中)因此未应用样式.

Unfortunately this happens because the Style is not written to the document by default (even though it is "built-in" to Word) therefore the style is not applied.

如果您通过代码创建一个文档,然后通过应用了 Heading1 样式的 Word 创建另一个文档,然后比较 wordstyles.xml 中的 XML,您将看到 Word 生成的 XML 中有一个附加部分:

If you create a document via your code and then another via Word with the Heading1 style applied then compare the XML in wordstyles.xml you will see there is an additional section in the Word generated XML:

您可以使用以下代码生成自己的样式信息.我不知道有什么方法可以从硬编码以外的地方获取这些样式;也许它们可以在某个地方的 dotx 文件中找到?有关此内容的更多信息,请参阅下面的编辑

You can generate your own style information using the following code. I don't know of a way to grab these styles from somewhere other than hardcoding; perhaps they could be found in a dotx file somewhere? See edit below for more on this

using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(documentStream, true))
{
    MainDocumentPart mainPart = wordDocument.MainDocumentPart;
    Body body = wordDocument.MainDocumentPart.Document.Body;
    Paragraph para = body.AppendChild(new Paragraph());
    StyleDefinitionsPart part = wordDocument.MainDocumentPart.StyleDefinitionsPart;
    if (part != null)
    {
        Style style = new Style()
        {
            Type = StyleValues.Paragraph,
            StyleId = "Heading1",
            BasedOn = new BasedOn() { Val = "Normal" },
            NextParagraphStyle = new NextParagraphStyle() { Val = "Normal" }
        };

        StyleName styleName1 = new StyleName() { Val = "heading 1" };
        style.Append(styleName1);
        style.Append(new PrimaryStyle());
        StyleRunProperties styleRunProperties1 = new StyleRunProperties();
        styleRunProperties1.Append(new Bold());
        styleRunProperties1.Append(new RunFonts() 
            {
                ComplexScriptTheme=ThemeFontValues.MajorBidi, 
                HighAnsiTheme=ThemeFontValues.MajorHighAnsi,
                EastAsiaTheme=ThemeFontValues.MajorEastAsia, 
                AsciiTheme=ThemeFontValues.MajorAscii 
            });
        styleRunProperties1.Append(new FontSize() { Val = "28" });
        styleRunProperties1.Color = new Color() 
            {
                Val = "365F91",
                ThemeShade = "BF",
                ThemeColor = ThemeColorValues.Accent1
            };
        style.Append(styleRunProperties1);
        part.Styles.Append(style);

        ParagraphProperties pPr = new ParagraphProperties();
        ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Heading1" };
        pPr.Append(paragraphStyleId1);
        para.Append(pPr);
    }

    Run run = para.AppendChild(new Run());
    run.AppendChild(new Text("This is a heading"));
}

编辑

我看了一下这个,发现 Word 默认样式确实存储在 .dotx 文件中,正如我上面猜测的那样.这些文件也可以通过 OpenXML API 读取,因此可以将样式从那里复制到您的文档中.此代码与我们已有的代码非常相似,只是添加了读取 Default.dotx 文件并从那里获取样式.

I had a look at this and found that the Word default styles are indeed stored in .dotx files as I guessed above. Those files are readable via the OpenXML API as well so it's possible to copy the styles from there into your document. The code for this is very similar to the code we already have with the addition of reading the Default.dotx file and grabbing the styles from there.

在我的系统上,dotx 文件位于 C:Program FilesMicrosoft OfficeOffice141033QuickStyles 中,我在下面对其进行了硬编码.我不知道是否有可以动态获取的地方(也许是注册表),但如果没有,我想配置文件就足够了.

On my system the dotx files are in C:Program FilesMicrosoft OfficeOffice141033QuickStyles which I've hard coded below. I don't know if there is somewhere this can be picked up dynamically (the registry perhaps) but if not I guess a config file will have to suffice.

using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(documentStream, true))
{
    MainDocumentPart mainPart = wordDocument.MainDocumentPart;
    Body body = wordDocument.MainDocumentPart.Document.Body;
    Paragraph para = body.AppendChild(new Paragraph());
    Paragraph para2 = body.AppendChild(new Paragraph());
    Paragraph para3 = body.AppendChild(new Paragraph());

    StyleDefinitionsPart part = wordDocument.MainDocumentPart.StyleDefinitionsPart;
    if (part != null)
    {
        //I'm looping the styles and adding them here
        //you could clone the whole StyleDefinitionsPart
        //but then you'd lose custom styles in your source doc
        using (WordprocessingDocument wordTemplate = WordprocessingDocument.Open(@"C:Program FilesMicrosoft OfficeOffice141033QuickStylesdefault.dotx", false))
        {
            foreach (var templateStyle in wordTemplate.MainDocumentPart.StyleDefinitionsPart.Styles)
            {
                part.Styles.Append(templateStyle.CloneNode(true));
            }
        }

        //I can now use any built in style 
        //I'm using Heading1, Title and IntenseQuote as examples
        //You may need to do a language conversion here as 
        //you mentione your docx is in Dutch
        ParagraphProperties pPr = new ParagraphProperties();
        ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Heading1" };
        pPr.Append(paragraphStyleId1);
        para.Append(pPr);

        Run run = para.AppendChild(new Run());
        run.AppendChild(new Text("This is a heading"));

        ParagraphProperties pPr2 = new ParagraphProperties();
        ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId() { Val = "Title" };
        pPr2.Append(paragraphStyleId2);
        para2.Append(pPr2);

        Run run2 = para2.AppendChild(new Run());
        run2.AppendChild(new Text("This is a title"));

        ParagraphProperties pPr3 = new ParagraphProperties();
        ParagraphStyleId paragraphStyleId3 = new ParagraphStyleId() { Val = "IntenseQuote" };
        pPr3.Append(paragraphStyleId3);
        para3.Append(pPr3);

        Run run3 = para3.AppendChild(new Run());
        run3.AppendChild(new Text("This is an intense quote"));
    }
}

生成的文件如下所示:

这篇关于无法在 Open Xml 中使用现有的段落样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆