2012-02-11 58 views
17

我要生成作爲輸入的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

有人可以幫我嗎? – 2012-02-15 12:26:27

回答

37

你對處理Space性能好辦法,但你需要做的是這樣的:

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

感謝cruster,這對我有用。 – 2013-04-30 20:11:41

+2

也適合我。這應該被標記爲接受的答案。 – evilfish 2013-12-09 12:45:17

7

這裏是另一種方式來設置可用於指定屬性空間 SpaceProcessingMode

t = new Text("This is some text"); 
t.Space = SpaceProcessingModeValues.Preserve; 

屬性的默認值是SpaceProcessingModeValues.Default

API Documentation

<w:r> 
<w:t> significant whitespace </w:t> 
</w:r> 

儘管對在運行的文本內容的每一側三個空格,該空格沒有被明確標記爲顯著,因此它受空間保存當前在該運行範圍中指定的規則。結束示例] 此屬性的可能值由XML 1.0規範的§2.10定義。