我想将该表作为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");
}
但它仅以pdf格式显示“msg”。我想以pdf格式显示表格。我怎么能这样做
答案 0 :(得分:0)
现在你只打印段落,因为你调用它document.Add(new Paragraph("msg"));
正确的下载PDF格式:
public static void DownloadPDF(HttpResponse response,string fileRelativePath)
{
try
{
string contentType = "";
//Get the physical path to the file.
string FilePath = HttpContext.Current.Server.MapPath(fileRelativePath);
string fileExt = Path.GetExtension(fileRelativePath).Split('.')[1].ToLower();
if (fileExt == "pdf")
{
//Set the appropriate ContentType.
contentType = "Application/pdf";
}
//Set the appropriate ContentType.
response.ContentType = contentType;
response.AppendHeader("content-disposition", "attachment; filename=" + (new FileInfo(fileRelativePath)).Name);
//Write the file directly to the HTTP content output stream.
response.WriteFile(FilePath);
response.End();
}
catch
{
//To Do
}
}
希望有所帮助