Jean-Paul
Jean-Paul

Reputation: 378

PDFSharp download file thru filestream

I am new to this library but I couldn't find anything about downloading to the filestream with this library, I could only find a document.Save(filepath) option which is not always allowed. I would like to create a filestream so the file gets downloaded to the directed Downloads folder.

Could someone point me in the right direct?

Upvotes: 1

Views: 4765

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34846

There is a Save overload that takes a MemoryStream object instead of path to a file.

The PDFSharp website has an example showing how to do this:

private void Page_Load(object sender, System.EventArgs e)
{
    // Create new PDF document
    PdfDocument document  = new PdfDocument();
    this.time             = document.Info.CreationDate;
    document.Info.Title   = "PDFsharp Clock Demo";
    document.Info.Author  = "Stefan Lange";
    document.Info.Subject = "Server time: " + 
    this.time.ToString("F", CultureInfo.InvariantCulture);

    // Create new page
    PdfPage page = document.AddPage();
    page.Width  = XUnit.FromMillimeter(200);
    page.Height = XUnit.FromMillimeter(200);

    // Create graphics object and draw clock
    XGraphics gfx = XGraphics.FromPdfPage(page);
    RenderClock(gfx);

    // Send PDF to browser
    MemoryStream stream = new MemoryStream();
    document.Save(stream, false);
    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-length", stream.Length.ToString());
    Response.BinaryWrite(stream.ToArray());
    Response.Flush();
    stream.Close();
    Response.End();
}

Upvotes: 2

Related Questions