简体   繁体   中英

Export FlowDocument with UIElement to rtf

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM