我试图保留我从另一文档中提取的文本中的空白。该文档中内置了空格。当我查看Text对象的innerxml时,空白看起来像在那里,但是当它被保存时,docx会删除空白。我已经提供了示例代码
class Program
{
static void Main(string[] args)
{
string fileName = @"c:\users\public\documents\TextEx.docx";
// Create a wordprocessing document with the specified file name.
WordprocessingDocument wordprocessingDocument =
WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document);
// Add a MainDocumentPart object.
MainDocumentPart mainDocumentPart =
wordprocessingDocument.AddMainDocumentPart();
// Creatre the document structure.
mainDocumentPart.Document = new Document();
Document document = mainDocumentPart.Document;
Body body = document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
// Write some text to the file.
var test = @"This is a test,
I would like this to save the space but it doesnt
{some more space between}
Even after using the preserve white space
";
run.AppendChild( new Text(test) {Space=SpaceProcessingModeValues.Preserve });
mainDocumentPart.Document.Save();
wordprocessingDocument.Close();
Console.WriteLine("The document has been created.\nPress a key");
Console.ReadKey();
}
}
产生的物品是:
这是一个测试,我想节省空间,但是没有 {即使在使用保留的空白之后,之间还有更多空间}
我需要的是
这是一项测试,我想以此节省空间,但并非如此
{之间还有一些空间
即使在使用保留空白之后
谢谢您的帮助。