I have the following program that generates an example word document:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace OpenXmlExample
{
class Program
{
static void Main(string[] args)
{
string filePath = @"C:\Users\[Redacted]\Desktop\OpenXmlExample.docx";
using (WordprocessingDocument document = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document))
{
// Create main part and body of document
MainDocumentPart mainPart = document.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
// Prototype code for generation of the document title
Paragraph paragraph = new Paragraph();
ParagraphProperties pPr = new ParagraphProperties
{
Justification = new Justification { Val = JustificationValues.Center }
};
RunProperties rPr = new RunProperties
{
RunFonts = new RunFonts { ComplexScript = new StringValue("Arial") },
Bold = new Bold { Val = true },
Caps = new Caps { Val = true },
FontSize = new FontSize { Val = "32" },
FontSizeComplexScript = new FontSizeComplexScript { Val = "36" }
};
Text t = new Text("Example Document");
Run r = new Run();
r.Append((OpenXmlElement) rPr.Clone());
r.Append(t);
pPr.Append((OpenXmlElement) rPr.Clone());
paragraph.Append(pPr);
paragraph.Append(r);
body.Append(paragraph);
}
}
}
}
It seems to be working just fine. "EXAMPLE DOCUMENT" is rendered properly, bold and uppercase, with 16pt font. However, the ComplexScript = new StringValue("Arial")
part doesn't seem to be working. The text is just rendered in the default font. Can anyone help me to understand if I'm doing something incorrectly? It seems to work if I set the Ascii
property to Arial, but I would like it to be generated as a <w:rFonts w:cs="Arial"/>
tag.
For a single run, 4 different fonts can be configured.
The use of each of these fonts shall be determined by the Unicode character values of the run content.
ASCII for characters in the Unicode range (U+0000-U+007F).
Complex Script for characters in a complex script Unicode range, eg. Arabic text.
East Asian for characters in an East Asian Unicode range, eg. Japanese.
High ANSI for characters in a Unicode range which does not fall into one of the other categories.
The characters in 'EXAMPLE DOCUMENT' all fall within the ASCII group and will therefore have font of the corresponding group applied.
More details at MSDN and Office Open XML.