2012-02-11 22 views
17

tôi muốn tạo ra một tài liệu từ như một đầu vào tôi có chuỗi này "mở ước bao bì" và mỗi từ sẽ có một phong cách khác nhau kết quả nên mở bao bì ướckhông thể duy trì khoảng cách giữa chạy

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

và kết quả là mở * bao bì * ước không có dấu cách giữa các từ một số ai có thể giúp tôi xin vui lòng ?!

+0

ai đó có thể giúp tôi không? –

Trả lời

37

Bạn đang trên đường tốt bằng cách xử lý Space bất động sản, nhưng bạn cần phải làm điều đó như thế này:

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

Cảm ơn sự quấy rầy, điều này phù hợp với tôi. –

+2

Cũng cho tôi. Điều này phải được đánh dấu là câu trả lời được chấp nhận. – evilfish

7

Đây là một cách khác để thiết lập các thuộc tính Space có thể được dùng để xác định SpaceProcessingMode.

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

Giá trị mặc định của thuộc tính là SpaceProcessingModeValues.Default.

Từ API Documentation:

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

Mặc dù có ba không gian trên mỗi bên của nội dung văn bản trong thời gian, khoảng trắng mà chưa được đánh dấu cụ thể như đáng kể, do đó nó là tùy thuộc vào việc bảo tồn không gian các quy tắc hiện được chỉ định trong phạm vi của hoạt động đó. end example] Các giá trị có thể cho thuộc tính này được định nghĩa bởi §2.10 của đặc tả XML 1.0.

Các vấn đề liên quan