PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 5:35 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon Nov 02, 2009 10:01 am 
Offline

Joined: Mon Nov 02, 2009 9:57 am
Posts: 2
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 12, 2009 2:37 pm 
Offline
User avatar

Joined: Tue Oct 14, 2008 6:15 pm
Posts: 32
Location: USA
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.


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 13, 2009 2:15 am 
Offline

Joined: Mon Nov 02, 2009 9:57 am
Posts: 2
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.


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 16, 2009 10:35 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 15, 2010 1:01 am 
Offline

Joined: Fri Jan 15, 2010 12:42 am
Posts: 4
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 10478 times ]


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 47 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group