简体   繁体   中英

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

AlternativeFormatImportPartType.TextPlain

This is my Code, but I can´t figure out how to do this at all (even Google doesn´t help)

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"

UPDATE

This is the working Solution I came up with:

Convert Plaintext to RTF, change the Fontfamily/size and apply it to the 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]

I am afraid this is NOT possible, in any case, not with OpenXml SDK.

Why?

altChunk (Anchor for Imported External Content) object is further designed for importing content in the document. 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.

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.

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).

And I can confirm that Word creates by default a style with "Courier New 10,5" to display the content of the file. I just tested.

What can I do?

Apply style after the document has been open/saved with 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. This link could help to achieve this: How to: Apply a style to a paragraph in a word processing document (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. 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.

I totally agreed. Just note when he says apply StyleInformation , it means at rtf level.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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