public void Save( Stream stream ) { using ( SignatureStream signature = new SignatureStream ( "DWRT", stream ) ) { using ( DeflateStream deflate = new DeflateStream ( stream, CompressionLevel.Optimal, true ) ) { BinaryWriter writer = new BinaryWriter ( deflate ); writer.Write ( ( byte ) 0 ); writer.Write ( Title ); writer.Write ( Authors.Count ); foreach ( var author in Authors ) writer.Write ( author ); writer.Write ( Copyright ); writer.Write ( Pages.Count ); foreach ( var page in Pages ) { writer.Write ( page.Title ); writer.Write ( page.Created.ToString () ); writer.Write ( page.Modified.ToString () ); writer.Write ( ( byte ) 0 ); var textRange = new TextRange ( page.Text.ContentStart, page.Text.ContentEnd ); MemoryStream tempStream = new MemoryStream (); textRange.Save ( tempStream, DataFormats.Rtf ); string text = Convert.ToBase64String ( tempStream.ToArray () ); tempStream.Dispose (); writer.Write ( text ); } } } }
public void Load( Stream stream ) { using ( SignatureStream signature = new SignatureStream ( "DWRT", stream ) ) { using ( DeflateStream deflate = new DeflateStream ( stream, CompressionMode.Decompress, true ) ) { Initialize (); pages.Clear (); BinaryReader reader = new BinaryReader ( deflate ); switch ( reader.ReadByte () ) { case 0: Title = reader.ReadString (); int authorCount = reader.ReadInt32 (); for ( int i = 0; i < authorCount; ++i ) Authors.Add ( reader.ReadString () ); Copyright = reader.ReadString (); int pageCount = reader.ReadInt32 (); for ( int i = 0; i < pageCount; ++i ) { dWriterPage page = new dWriterPage (); page.Title = reader.ReadString (); page.Created = DateTime.Parse ( reader.ReadString () ); page.Modified = DateTime.Parse ( reader.ReadString () ); switch ( reader.ReadByte () ) { case 0: MemoryStream tempStream = new MemoryStream ( Convert.FromBase64String ( reader.ReadString () ) ); FlowDocument flowDocument = new FlowDocument (); var textRange = new TextRange ( flowDocument.ContentStart, flowDocument.ContentEnd ); textRange.Load ( tempStream, DataFormats.Rtf ); page.Text = flowDocument; tempStream.Dispose (); break; } pages.Add ( page ); } break; } } } }