我想生成一个word文档 作为一个输入我有这个字符串“打开包装约定”,每个单词将有不同的风格 结果应为打开打包约定
WordprocessingDocument document = WordprocessingDocument.Create(
@"C:\test PFE.docx",
WordprocessingDocumentType.Document
);
MainDocumentPart mainDocumentPart = document.AddMainDocumentPart();
mainDocumentPart.Document = new Document();
mainDocumentPart.Document.AddNamespaceDeclaration("ve", "http://schemas.openxmlformats.org/markup-compatibility/2006");
mainDocumentPart.Document.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
mainDocumentPart.Document.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
mainDocumentPart.Document.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
mainDocumentPart.Document.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
mainDocumentPart.Document.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
mainDocumentPart.Document.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
mainDocumentPart.Document.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
mainDocumentPart.Document.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
Body documentBody = new Body();
mainDocumentPart.Document.Append(documentBody);
StyleDefinitionsPart styleDefinitionsPart =
mainDocumentPart.AddNewPart<StyleDefinitionsPart>();
FileStream stylesTemplate =
new FileStream("styles.xml", FileMode.Open, FileAccess.Read);
styleDefinitionsPart.FeedData(stylesTemplate);
styleDefinitionsPart.Styles.Save();
#region Titre du document
Paragraph titleParagraphe = new Paragraph() { RsidParagraphAddition = "00AF4948", RsidParagraphProperties = "00625634", RsidRunAdditionDefault = "00625634" }; ;
Run run = new Run();
RunProperties rpr = new RunProperties();
RunStyle rstylr = new RunStyle { Val = "style1" };
run.Append(rpr);
Text t = new Text("open");
run.Append(t);
titleParagraphe.Append(run);
run = new Run();
rpr = new RunProperties();
rstylr = new RunStyle { Val = "style2" };
run.Append(rpr);
t = new Text("packaging")
{
Space = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.SpaceProcessingModeValues> { InnerText = "preserve" }
};
run.Append(t);
titleParagraphe.Append(run);
run = new Run();
rpr = new RunProperties();
rstylr = new RunStyle { Val = "style1" };
run.Append(rpr);
t = new Text("conventions")
{
Space = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.SpaceProcessingModeValues> { InnerText = "preserve" }
};
run.Append(t);
titleParagraphe.Append(run);
documentBody.Append(titleParagraphe);
document.MainDocumentPart.Document.Save();
document.Dispose();
结果是打开* 打包 *约定,单词之间没有空格 有人可以帮我吗?!
答案 0 :(得分:42)
通过处理Space
属性,您处于良好状态,但您需要这样做:
t = new Text()
{
Text = "your text with spaces ",
Space = SpaceProcessingModeValues.Preserve
};
答案 1 :(得分:7)
以下是另一种设置属性空间的方法,可用于指定 SpaceProcessingMode 。
t = new Text("This is some text");
t.Space = SpaceProcessingModeValues.Preserve;
该属性的默认值为 SpaceProcessingModeValues.Default 。
<w:r>
<w:t> significant whitespace </w:t>
</w:r>
虽然运行中文本内容的每一侧都有三个空格,但该空格尚未明确标记为重要空格,因此它受当前运行范围中指定的空间保留规则的约束。最后的例子] 此属性的可能值由XML 1.0规范的第2.10节定义。