7

I'm using FileResult as a return value for a function in MVC that returns a PDF file.

What return type should I use in Web Forms?

Thanks

public FileResult PrintPDFVoucher(object sender, EventArgs e)
    {
        PdfDocument outputDoc = new PdfDocument();
        PdfDocument pdfDoc = PdfReader.Open(
            Server.MapPath(ConfigurationManager.AppSettings["Template"]),
            PdfDocumentOpenMode.Import
        );

        MemoryStream memory = new MemoryStream();

        try
        {
            //Add pages to the import document
            int pageCount = pdfDoc.PageCount;
            for (int i = 0; i < pageCount; i++)
            {
                PdfPage page = pdfDoc.Pages[i];
                outputDoc.AddPage(page);
            }
            //Target specifix page
            PdfPage pdfPage = outputDoc.Pages[0];

            XGraphics gfxs = XGraphics.FromPdfPage(pdfPage);
            XFont bodyFont = new XFont("Arial", 10, XFontStyle.Regular);


            //Save 
            outputDoc.Save(memory, true);
            gfxs.Dispose();
            pdfPage.Close();
        }
        finally
        {
            outputDoc.Close();
            outputDoc.Dispose();
        }

        var result = new FileContentResult(memory.GetBuffer(), "text/pdf");
        result.FileDownloadName = "file.pdf";
        return result;
    }
ausesr
  • 73
  • 1
  • 3

2 Answers2

7

In ASP.NET Webforms you'll need to write the file to the Response stream manually. There is no result abstraction in webforms.

  Response.ContentType = "Application/pdf";      
  //Write the generated file directly to the response stream
  Response.BinaryWrite(memory);//Response.WriteFile(FilePath); if you have a physical file you want them to download
  Response.End();

This code is not tested, but this should get you in the general direction.

Jab
  • 13,713
  • 2
  • 42
  • 48
5

Classic ASP.NET doesn't have the idea of a return type. The way to approach this would be to create an custom .ashx page/handler to serve up the file.

Your code behind for this file should look something similar to:

public class Download : IHttpHandler 
{
    public void ProcessRequest (HttpContext context) 
    {
        PdfDocument outputDoc = new PdfDocument();
        PdfDocument pdfDoc = PdfReader.Open(
            Server.MapPath(ConfigurationManager.AppSettings["Template"]),
            PdfDocumentOpenMode.Import
        );

        MemoryStream memory = new MemoryStream();

        try
        {
            //Add pages to the import document
            int pageCount = pdfDoc.PageCount;
            for (int i = 0; i < pageCount; i++)
            {
                PdfPage page = pdfDoc.Pages[i];
                outputDoc.AddPage(page);
            }
            //Target specifix page
            PdfPage pdfPage = outputDoc.Pages[0];

            XGraphics gfxs = XGraphics.FromPdfPage(pdfPage);
            XFont bodyFont = new XFont("Arial", 10, XFontStyle.Regular);


            //Save 
            Response.ContentType = ""text/pdf"";
            Response.AppendHeader("Content-Disposition","attachment; filename=File.pdf");

            outputDoc.Save(Response.OutputStream, true);

            gfxs.Dispose();
            pdfPage.Close();
        }
        finally
        {
            outputDoc.Close();
            outputDoc.Dispose();
        }
    }

    public bool IsReusable
    {
        get 
        {
               return false;
        }
    }
}
Charlino
  • 15,802
  • 3
  • 58
  • 74