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

Bug in PdfDocument.Save(Stream)
https://forum.pdfsharp.net/viewtopic.php?f=3&t=923
Page 1 of 1

Author:  Xcalibur [ Mon Nov 02, 2009 10:01 am ]
Post subject:  Bug in PdfDocument.Save(Stream)

When the PDF is saved to a stream, the position is not reset to 0. This means subsequent operations (such as reading from the stream) effectively fail because there is no more data to read.

Generally, most .NET functions will reset a stream position (stream.Position = 0) before returning it so the stream can be used immediately as expected

Author:  Soldier-B [ Thu Nov 12, 2009 2:37 pm ]
Post subject:  Re: Bug in PdfDocument.Save(Stream)

When using the PdfDocument.Save function there are 2 different overloads that write to streams. The first only take a stream parameter, the second takes a stream and a boolean. The boolean value flags whether the save function should keep the stream open or not (false = keep stream open, true = close stream). If using the stream only overload, by default, it will automatically close the stream and all operations from there on out will fail.

Author:  Xcalibur [ Fri Nov 13, 2009 2:15 am ]
Post subject:  Re: Bug in PdfDocument.Save(Stream)

I realise that ....

Let me be more explicit - when using PdfDocument.Save(Stream, boolean = false), the stream position is not reset and all subsequent operations fail. This method should reset the position to 0, as is the expected behaviour when handling streams.

Author:  Thomas Hoevel [ Mon Nov 16, 2009 10:35 am ]
Post subject:  Re: Bug in PdfDocument.Save(Stream)

Hi!
Xcalibur wrote:
When the PDF is saved to a stream, the position is not reset to 0.

Thanks for your feedback.
PDFsharp does now reset the stream position.

In our samples, we always use stream.ToArray() to get a byte[] from the stream - therefore we never had to reset the position.

Author:  indula [ Fri Jan 15, 2010 1:01 am ]
Post subject:  Re: Bug in PdfDocument.Save(Stream)

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;

using MigraDoc;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using MigraDoc.RtfRendering;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.DocumentObjectModel.Shapes;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;



namespace PdfSharpTest
{
    public partial class _Default : System.Web.UI.Page
    {
        public static string FillerText = "filler text";

        protected void Page_Load(object sender, EventArgs e)
        {
            // Create a MigraDoc document
            Document document = CreateDocument();
            document.Info.Title = "title";

            //string ddl = MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToString(document);
            //MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(document, "MigraDoc.mdddl");

            PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
            renderer.Document = document;
            renderer.RenderDocument();

             // Send PDF to browser
            MemoryStream stream = new MemoryStream();

            renderer.Save(stream, false); // or renderer.PdfDocument.Save(stream, false);

            Response.Clear();
            // Response.AddHeader("Content-Disposition", "attachment; filename=filename.pdf");
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", stream.Length.ToString());
            Response.BinaryWrite(stream.ToArray());
            Response.Flush();
            stream.Close();
            Response.End();

        }



The above code worked for me to stream a migradoc to the browser. In the case where i dont specify a (content disposition)filename it loads on the browser window it self without asking to save it as a file which i expect. But on the browser tab it displays "Specified method is not supported"

Attachment:
migradoc.jpg
migradoc.jpg [ 72.28 KiB | Viewed 10890 times ]

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