如何在MVC中下载pdf [英] How to download pdf in MVC

查看:112
本文介绍了如何在MVC中下载pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void pdf()

{

MemoryStream workStream = new MemoryStream();

Document document = new Document();

PdfWriter.GetInstance(document,workStream).CloseStream = false;



document.Open();



document.Add(new Paragraph(msg));

document.Add(new Paragraph(DateTime.Now.ToString()));

document.Close();



byte [] byteInfo = workStream.ToArray();

workStream.Write(byteInfo ,0,byteInfo.Length);

workStream.Position = 0;

Response.Buffer = true;



Response.AddHeader(Content-Disposition,attachment; filename =+ Server.HtmlEncode(doc.pdf));

Response.ContentType =APPLICATION / pdf;

Response.BinaryWrite(byteInfo);

//返回新的FileStreamResult(workStream,application / pdf);

}



我的尝试:



当我试试这个...下载pdf并显示在那个pdfmsg....但我想表现

雇员表在那个pdf ....我该怎么办?

public void pdf()
{
MemoryStream workStream = new MemoryStream();
Document document = new Document();
PdfWriter.GetInstance(document, workStream).CloseStream = false;

document.Open();

document.Add(new Paragraph("msg"));
document.Add(new Paragraph(DateTime.Now.ToString()));
document.Close();

byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
Response.Buffer = true;

Response.AddHeader("Content-Disposition", "attachment; filename= " + Server.HtmlEncode("doc.pdf"));
Response.ContentType = "APPLICATION/pdf";
Response.BinaryWrite(byteInfo);
//return new FileStreamResult(workStream, "application/pdf");
}

What I have tried:

when i try this...its download pdf and show in that pdf "msg".... but i want to show
employee table in that pdf.... how can i do that?

推荐答案



 检查此代码,它将根据您的要求工作。

PdfPTable对象被实例化为五列表,然后使用AddCell添加标题和值。



Hi,
  Check this code, It will work according to your requirement.
The "PdfPTable" object is instantiated as a five column table then use "AddCell" to add header and values.

public void pdf()
        {
 
            Employees emp = new Employees();

            MemoryStream workStream = new MemoryStream();
            Document document = new Document();
            PdfWriter.GetInstance(document, workStream).CloseStream = false;

            document.Open();

            document.Add(new Paragraph("msg"));
            document.Add(new Paragraph(DateTime.Now.ToString()));

            
            
            PdfPTable table = new PdfPTable(5); 
            // To show data in table.
            // "5" total number of columns.


            // Here I have set width of every columns.
            float[] widths = new float[] { 100f, 80f, 100f, 90f, 130f };
            table.SetWidths(widths);

            // Name of the header for each column.
            table.AddCell("Employee Name");
            table.AddCell("Experience in Months");
            table.AddCell("Band");
            table.AddCell("Designation");
            table.AddCell("Current Project");

            // Adding value in the table.
            // If you are getting list values the use FOREACH loop to show all data in                                                     table  
            table.AddCell(emp.Name);
            table.AddCell(emp.Experience.ToString());
            table.AddCell(emp.Band);
            table.AddCell(emp.Designation);
            table.AddCell(emp.Project);
            
            // Here I am adding table to the document.
            document.Add(table);
            document.Close();

            byte[] byteInfo = workStream.ToArray();
            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;
            Response.Buffer = true;

            Response.AddHeader("Content-Disposition", "attachment; filename= " + Server.HtmlEncode("doc.pdf"));
            Response.ContentType = "APPLICATION/pdf";
            Response.BinaryWrite(byteInfo);
        }





这是我从数据库中检索数据的类。如果您希望可以使用Sqlconnection和所有数据从数据库获取数据,那么我已经完成了硬编码。





This is the class where I am retrieving data from database. Here I have done hard coding if you want you can get data from database using Sqlconnection and all.

public class Employees
    {
        // Here I have done hard coding if you want, you can retrieve data from database. 
        public string Name = "Smith";
        public int Experience = 0;
        public string Band = "B4";
        public string Designation = "Software Engineer";
        public string Project = "XYZ";
    }


这篇关于如何在MVC中下载pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆