不能在运行之间保留空间 [英] cannot preserve space between runs

查看:62
本文介绍了不能在运行之间保留空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个word文档作为输入,我有这个字符串开放包装约定",每个词都有不同的风格结果应该是开放包装约定

i want to generate a word document as an input i have this string "open packaging conventions" and each word will have a different style the result should be open packaging conventions

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(); 

结果是 open*packaging*conventions 单词之间没有空格有人可以帮我吗?!

and the result is open*packaging*conventions without space between words can some one help me please?!

推荐答案

处理 Space 属性的方法很好,但你需要这样做:

You're on good way by handling the Space property, but you need to do it like this:

t = new Text()
{
    Text = "your text with spaces ",
    Space = SpaceProcessingModeValues.Preserve
};

这篇关于不能在运行之间保留空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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