2012-04-02 2 views
0

우리는 Aspose.Words for .NET을 사용하여 응용 프로그램에서 Word 문서를 내보내고 있습니다. 이제 RichText 컨텐트 (실제로는 FlowDocument)도 내 보낸 문서에 포함시켜야합니다. 내보내려면 IMailMergeDataSource 인터페이스를 구현해야합니다. 이 IMailMergeDataSource 구현의을 getValue 기능은 Aspose 라이브러리를 호출하고,이 함수는 다음과 같습니다있다 : 나는 설정해야Aspose.Words for .NET으로 서식있는 텍스트를 Word에 저장하는 방법은 무엇입니까?

public override bool GetValue(string fieldName, out object fieldValue) { ... } 

그래서 내가 말씀 템플릿의 현재 필드의 필드 이름을 얻고, fieldValue의 문자열을 Word 문서에 표시 할 수 있도록 문자열에 fieldValue를 지정합니다.

는하지만이 FlowDocument에 fieldValue의 설정 예를 들어, 결과는

답변

1

나는 당신이 fieldValue의에서 서식있는 텍스트를 통과 할 것을 제안 XML 문자열합니다 (FlowDocument 객체의 toString 표현)입니다. (FieldMerging 이벤트 내에서) 다음과 같이 Aspose.Words 문서 객체로이 서식있는 텍스트를로드

string rtfStr = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang3079{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}{\\colortbl ;\\red255\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;}\\viewkind4\\uc1\\pard\\cf1\\f0\\fs17 Rot.\\cf0\\fs17 \\cf2\\fs17 Gr\\'fcn.\\cf0\\fs17 \\cf3\\fs17 Blau.\\cf0\\fs17 \\i\\fs17 Kursiv.\\i0\\fs17 \\strike\\fs17 Durchgestrichen. \\ul\\strike0 Unterstrichen.\\ulnone\\fs17 \\b\\fs17 Fett.\\b0\\fs17\\par}"; 

System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); 
byte[] dataBytes = encoding.GetBytes(rtfStr); 
MemoryStream stream = new MemoryStream(dataBytes); 

LoadOptions loadOptions = new LoadOptions(); 
loadOptions.LoadFormat = LoadFormat.Rtf; 

Document doc = new Document(stream, loadOptions); 

당신은 편지 병합 작업을 수행하는 동안 병합 필드에 삽입하는 방법을 데이터를 제어 할 수 IFieldMergingCallback 인터페이스를 구현해야합니다.

private class HandleMergeFields : IFieldMergingCallback 
{ 
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) 
    { 
     DocumentBuilder builder = new DocumentBuilder(e.Document); 

     builder.MoveToMergeField("fieldName"); 
     Node node = builder.CurrentNode; 

     // doc is an RTF document we created from RTF string 
     InsertDocument(node, doc); 

이 시나리오에서 도움이되기를 바랍니다. 도움이되지 않으면 알려주세요.

+1

작동합니다! 감사! FlowDocument 문자열을 RTF 문자열로 변환하면됩니다. – asdfghjkl

+0

var xamlString = "..."; var flowDocument = FlowDocumentService.GetFlowDocument (xamlString); string dataFormat = DataFormats.Rtf; var documentTextRange = new TextRange (flowDocument.ContentStart, flowDocument.ContentEnd); var stream = 새 MemoryStream(); documentTextRange.Save (stream, dataFormat); LoadOptions loadOptions = new LoadOptions(); loadOptions.LoadFormat = LoadFormat.Rtf; fieldValue = 새 문서 (stream, loadOptions); – asdfghjkl

관련 문제