Kseniya  Yudina
Kseniya Yudina

Reputation: 137

Save PDF document to the Response.OutputStream using PDFsharp

I'm trying to save a copy of the source file directly into a response output stream. But, as a result of this code, the browser window has a dark background. How can I do it without using а MemoryStream?

public static void CreateCollage(IEnumerable<Stamp> stamps, Stream input)
{

    using (PdfDocument outDoc = new PdfDocument())
        using (PdfDocument inputDoc = PdfReader.Open(input, PdfDocumentOpenMode.Import))
        {
            for (int i = 0; i < inputDoc.PageCount; i++)
            {
                var page = inputDoc.Pages[i];
                var pageOut = outDoc.AddPage(page);

                foreach (var stamp in stamps.Where(s => s.xyp.page == (i + 1)))
                    InsertData(pageOut, stamp, page.Width.Value, page.Height.Value);
            }
            outDoc.Save(context.Response.OutputStream, true);
        }
}

If I use Save() function - I get an error:

The specified method is not supported.

in System.Web.HttpResponseStream.get_Position() in PdfSharp.Pdf.IO.PdfWriter.WriteFileHeader(PdfDocument document) in d:\Users\yudina\Desktop\pdfsharp\PDFsharp\src\PdfSharp\Pdf.IO\PdfWriter.cs:row 488

Upvotes: 0

Views: 1595

Answers (1)

You do not call outDoc.Close() and nothing ever gets written into your OutputStream.

Upvotes: 1

Related Questions