【发布时间】:2012-03-02 05:20:50
【问题描述】:
我只想生成一个 pdf 文档,其中包含单击按钮时显示的详细信息。
【问题讨论】:
标签: asp.net-mvc-3
我只想生成一个 pdf 文档,其中包含单击按钮时显示的详细信息。
【问题讨论】:
标签: asp.net-mvc-3
为了生成 PDF 文件,您需要一些第三方库,因为此功能未内置在 .NET 框架中。 iTextSharp 很受欢迎。
例如,您可以编写自定义操作结果:
public class PdfResult : ActionResult
{
public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.Response;
response.ContentType = "application/pdf";
var cd = new ContentDisposition
{
Inline = true,
FileName = "test.pdf",
};
response.AddHeader("Content-Disposition", cd.ToString());
using (var doc = new Document())
using (var writer = PdfWriter.GetInstance(doc, response.OutputStream))
{
doc.Open();
doc.Add(new Phrase("Hello World"));
}
}
}
然后让您的控制器操作返回此结果:
public class HomeController : Controller
{
public ActionResult Index()
{
return new PdfResult();
}
}
【讨论】:
Inline = false。