PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Generate rtf without closing stream
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3123
Page 1 of 1

Author:  sullyc [ Mon Jun 08, 2015 2:02 pm ]
Post subject:  Generate rtf without closing stream

I have a reporting class which returns a stream to the caller. This works with PDFs:

Code:
        protected Stream RenderToStream(Document document)
        {
            var renderer = new PdfDocumentRenderer(true, PdfFontEmbedding.Always) { Document = document };
            renderer.RenderDocument();

            var stream = new MemoryStream();
            renderer.PdfDocument.Save(stream, false);           

            stream.Seek(0, SeekOrigin.Begin);

            return stream;
        }

But if I try similar with rtfs:

Code:
        protected Stream RenderToStream(Document document)
        {
           
            //var renderer = new PdfDocumentRenderer(true, PdfFontEmbedding.Always) { Document = document };
            var renderer = new RtfDocumentRenderer();           
            var stream = new MemoryStream();
           
            renderer.Render(document, stream, WorkingPath);
                       
            //can't do this as stream is closed
            //stream.Seek(0, SeekOrigin.Begin);

            return stream;
        }

I get a stream closed error - does .Render close the stream? How do I work around so I can return a stream - I don't really want to refactor the code too much just for rtfs.

Ta.

Author:  Thomas Hoevel [ Mon Jun 08, 2015 4:06 pm ]
Post subject:  Re: Generate rtf without closing stream

Hi!

Yes, Render() closes the stream.

You could use RenderToString to write RTF to a string. Write that string to a MemoryStream and return the open stream.

I think the option to keep the stream open will be included in the next release. Until then use RenderToString or modify version 1.32 as needed.

Author:  sullyc [ Tue Jun 09, 2015 5:45 am ]
Post subject:  Re: Generate rtf without closing stream

Many thanks for the pointer.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/