Adding spacing and blank lines to Open Office XML

狂风中的少年 提交于 2019-12-13 05:57:54

问题


I am trying to output a word file using Open Office XML, but can't seem to get spacing correct.

Variable name:ATTEND
 Description:Student's attendance status during the 

I want the word file to be this (with spaces after the :):

Variable name: ATTEND
Description:Student's attendance status during the 

My code is as follow, and the spacing disappears:

start of my function
// Add a new main document part. 
            MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

            // Create the Document DOM.
            mainPart.Document = new Document();

            Body body = mainPart.Document.AppendChild(new Body());

            ParagraphProperties paragraphProperties = new ParagraphProperties
            (
                new ParagraphStyleId() { Val = "No Spacing" },
                new SpacingBetweenLines() { After = "0" }
            );

            Paragraph para = body.AppendChild(new Paragraph(paragraphProperties));
            Run run = para.AppendChild(new Run());

            RunProperties runProperties = run.AppendChild(new RunProperties(new Bold()));
            run.AppendChild(new Text("Variable name: "));

            run = para.AppendChild(new Run());
            run.AppendChild(new Text(" ATTEND"));

            para = body.AppendChild(new Paragraph());

            run = para.AppendChild(new Run());

            runProperties = run.AppendChild(new RunProperties(new Bold()));
            run.AppendChild(new Text("Description: "));

            run = para.AppendChild(new Run());
            run.AppendChild(new Text(" Student's attendance status during the "));


            // Save changes to the main document part. 
            wordDocument.MainDocumentPart.Document.Save();
        }

回答1:


Usually, OpenXML will trim every Text member. To preserve the spaces in each Text member, so you will have this test test instead of test test, set the special Space property of the Text member:

Text txt = new Text("text here ") { Space = SpaceProcessingModeValues.Preserve };



来源:https://stackoverflow.com/questions/11508277/adding-spacing-and-blank-lines-to-open-office-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!