Export FlowDocument with UIElement to rtf

末鹿安然 提交于 2020-01-24 17:13:11

问题


I am trying to export a FlowDocument which contains a grid to rtf. I used the following code

using (FileStream fs = new FileStream(@"C:\demo.rtf", FileMode.OpenOrCreate, FileAccess.Write))
{
    TextRange textRange = new TextRange(doc.ContentStart, doc.ContentEnd);
    textRange.Save(fs, DataFormats.Rtf);
}

However I am getting a blank document. How can this be solved?


回答1:


I had a similar issue recently and the culprit turned out to be the

FileMode.OpenOrCreate

It should have been

FileMode.Create 

instead. When you use OpenOrCreate and the file already exists and has more content than you are writing into it you will end up with the end of the old file after the end of the new content. Word or WordPad or whatever you are trying to open it in may not be able to interpret it correctly but makes an attempt to show you what it can which may be in your case a blank page.

The second issue that may be part of the problem is the viewer you use to open it and the FlowDocument you use to write it may not be on the same wave length to put it mildly. You may notice that WordPad for example displays the same rtf file differently than Word. They also produce very different files when you save them. Same goes for the FlowDocument - it may be saving something that for example WordPad or even Word (though this is less likely) is not able to display correctly (or at all).



来源:https://stackoverflow.com/questions/17789052/export-flowdocument-with-uielement-to-rtf

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