简体   繁体   English

OpenXml-SDK:如何将FontFamily / Size应用于类型为[TextPlain]的AltChunk

[英]OpenXml-SDK: How to apply FontFamily/Size to AltChunk of Type [TextPlain]

Can anybody show me how to apply Fontfamily/size to an AltChunk of Type 谁能告诉我如何将Fontfamily / size应用于Type的AltChunk

AlternativeFormatImportPartType.TextPlain

This is my Code, but I can´t figure out how to do this at all (even Google doesn´t help) 这是我的代码,但我根本不知道如何执行此操作(即使Google也不起作用)

MainDocumentPart main = doc.MainDocumentPart;
string altChunkId = "AltChunkId" + Guid.NewGuid().ToString().Replace("-", "");
var chunk = main.AddAlternativeFormatImportPart
    (AlternativeFormatImportPartType.TextPlain, altChunkId);

using (var mStream = new MemoryStream())
{
    using (var writer = new StreamWriter(mStream))
    {
        writer.Write(value);
        writer.Flush();
        mStream.Position = 0;
        chunk.FeedData(mStream);
    }
}

var altChunk = new AltChunk();
altChunk.Id = altChunkId;

OpenXmlElement afterThat = null;
foreach (var para in main.Document.Body.Descendants<Paragraph>())
{
    if (para.InnerText.Equals("Notizen:"))
    {
        afterThat = para;
    }
}

main.Document.Body.InsertAfter(altChunk, afterThat);

if I do it this way I get "Courier New" with a Size of "10,5" 如果以这种方式进行操作,我将获得大小为“ 10,5”的“ Courier New”

UPDATE UPDATE

This is the working Solution I came up with: 这是我想出的有效解决方案:

Convert Plaintext to RTF, change the Fontfamily/size and apply it to the WordProcessingDocument! 将纯文本转换为RTF,更改Fontfamily /大小并将其应用于WordProcessingDocument!

public static string PlainToRtf(string value)
{
    using (var rtf = new System.Windows.Forms.RichTextBox())
    {
        rtf.Text = value;
        rtf.SelectAll();
        rtf.SelectionFont = new System.Drawing.Font("Calibri", 10);

        return rtf.Rtf;
    }
}

var chunk = main.AddAlternativeFormatImportPart
        (AlternativeFormatImportPartType.Rtf, altChunkId);

using (var mStream = new MemoryStream())
{
    using (var writer = new StreamWriter(mStream))
    {
        var rtf = PlainToRtf(value);
        writer.Write(rtf);
        writer.Flush();
        mStream.Position = 0;
        chunk.FeedData(mStream);
    }
}
//proceed with creating AltChunk and inserting it to the Document...

How to apply FontFamily/Size to AltChunk of Type [TextPlain] 如何将FontFamily / Size应用于类型为[TextPlain]的AltChunk

I am afraid this is NOT possible, in any case, not with OpenXml SDK. 恐怕在任何情况下,OpenXml SDK都无法做到这一点。

Why? 为什么?

altChunk (Anchor for Imported External Content) object is further designed for importing content in the document. altChunk (导入的外部内容的锚点)对象还被设计用于导入文档中的内容。 They are 'temporary' objects: it is a just a reference to an external content, that is incorporated "as is" in the document, and then, when the document will be opened and saved with Word, Word converts this external content in valid OpenXml content. 它们是“临时”对象:它只是对外部内容的引用,即按原样合并到文档中,然后,当使用Word打开和保存文档时,Word会将该外部内容转换为有效OpenXml内容。

So you can't, for a newly created document, loop into the paragraphs in order to retrieve it and apply a style. 因此,对于新创建的文档,您不能循环到段落中以检索它并应用样式。

If you import rtf content for example, the style must be applied to rtf before importing it. 例如,如果导入rtf内容,则必须在导入之前将样式应用于rtf

In case of plain text TextPlain (= Text file .txt ), there is no style conversion (there is no style attached to the text file, you can change the font in NotePad, it will apply to all documents, this is an Application Level property). 如果是纯文本TextPlain(=文本文件.txt ),则没有样式转换(文本文件没有样式附加,您可以在NotePad中更改字体,它将应用于所有文档,这是应用程序级别属性)。

And I can confirm that Word creates by default a style with "Courier New 10,5" to display the content of the file. 而且我可以确认Word默认情况下使用"Courier New 10,5"创建样式以显示文件的内容。 I just tested. 我刚刚测试。

What can I do? 我能做什么?

Apply style after the document has been open/saved with Word. 用Word打开/保存文档应用样式。 Note you will have to retreive the paragrap(s), or you could try to retrieve the style created in the document and change the font here. 请注意,您将不得不获取paragrap,或者您可以尝试检索在文档中创建的样式并在此处更改字体。 This link could help to achieve this: How to: Apply a style to a paragraph in a word processing document (Open XML SDK) . 该链接可以帮助实现以下目的: 如何:将样式应用于文字处理文档(Open XML SDK)中的段落

Or maybe it exists(?) a registry key something Like this that you can change to change Word's default behavior on your computer. 也许它存在(?)注册表项之类的东西,您可以更改它以更改计算机上Word的默认行为。 And even if it is, it doesn't solve the problem for newly created document which is opened the first time on the client. 即使是这样,它也无法解决第一次在客户端上打开的新创建文档的问题。

Note from the OP: 操作说明:

I think a possible Solution to the Problem could be, converting the PlainText to RTF apply StyleInformation and then append it to WordProcessingDocument as AltChunk. 我认为该问题的可能解决方案可能是将PlainText转换为RTF,然后应用StyleInformation,然后将其作为AltChunk附加到WordProcessingDocument。

I totally agreed. 我完全同意。 Just note when he says apply StyleInformation , it means at rtf level. 请注意,当他说应用StyleInformation时 ,这意味着在rtf级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM