[英]The File is damaged and cannot be repaired
string attachment = "attachment; filename=" + filename + ".pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
htextw.AddStyleAttribute("font-size", "7pt");
htextw.AddStyleAttribute("color", "Black");
Panel_Name.RenderControl(htextw);// .RenderControl(htextw);
//Name of the Panel
Document document = new Document();
document = new Document(PageSize.A4, 5, 5, 15, 5);
FontFactory.GetFont("Arial", 50, iTextSharp.text.BaseColor.BLUE);
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
StringReader str = new StringReader(stw.ToString());
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.Parse(str);
document.Close();
Response.Write(document);
I have return this code to generate pdf of an aspx page(ie Default.aspx to Default.pdf).我已返回此代码以生成 aspx 页面的 pdf(即 Default.aspx 到 Default.pdf)。 It generates a pdf file but the pdf file generated is not supported I have the latest version of pdf.
它会生成一个 pdf 文件,但不支持生成的 pdf 文件我有最新版本的 pdf。 It gives an error of opening the document.
它给出了打开文档的错误。 The File is damaged and cannot be repaired.
文件已损坏,无法修复。
You need to download ITextSharp and add its reference to your project.您需要下载ITextSharp并将其引用添加到您的项目中。 ITextSharp is a free HTML to PDF Library.
ITextSharp是一个免费的 HTML 到 PDF 库。 You can download it using the following download link.
您可以使用以下下载链接下载它。
HTML Markup
HTML 标记
<form id="form1" runat="server">
<div>
<img src = "//www.aspsnippets.com/images/Blue/Logo.png" /><br />
</div>
<div style = "font-family:Arial">This is a test page</div>
<div>
<table border = "1" width = "100">
<tr><td>Name</td><td>Age</td></tr>
<tr><td>John</td><td>11</td></tr>
<tr><td>Sam</td><td>13</td></tr>
<tr><td>Tony</td><td>12</td></tr>
</table>
</div>
<div>
<asp:Button ID="btnExport" runat="server" Text="Export" onclick="btnExport_Click" />
</div>
</form>
Namespaces
命名空间
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
C# Code
C#代码
protected void btnExport_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.