问题
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