简体   繁体   English

文件已损坏且无法修复

[英]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();
}

You can use the third party library to create pdf such as PDFizer您可以使用第三方库创建 pdf 例如PDFizer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将内存流移至文件流时,PDF文件已损坏且无法修复 - PDF File is damaged and cannot be repaired when moving memory stream to filestream pdf下载出现问题“文件已损坏,无法修复” - issue with pdf downloading “ The file is damaged could not be repaired” 无法打开从 Web API 返回的 PDF。 打开此文档时出错。 文件已损坏且无法修复 - PDF returned from Web API cannot be opened. There was an error opening this document. The file is damaged and could not be repaired 错误:“文件已损坏,无法修复。” 在渲染 ServerReport 时 - Error: "The file is damaged and could not be repaired." while rendering ServerReport 如果不使用静态变量来获得健康,敌人就不会受到伤害 - Enemy cannot be damaged if not using static variable for health C#DLL生成的pdf文件已损坏 - c# dll generated pdf file is damaged tcpClient未完全或“损坏”发送文件到tcpServer - tcpClient sends file to tcpServer not completely or “damaged” c#验证excel文件是否损坏,如果损坏,则弹出一个消息框 - c# To verify excel file is damaged or not , if damaged then pop out a message box 使用DocumentFormat.OpenXML创建的Excel文件(.xlsx)在Excel打开时需要修复 - Excel file (.xlsx) created by using DocumentFormat.OpenXML needs to be repaired when opening in Excel “文件已损坏,无法打开”:Windows Phone 8.1 - “File has been damaged and can't be opened”: Windows Phone 8.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM