【问题标题】:The File is damaged and cannot be repaired文件已损坏且无法修复
【发布时间】:2011-10-09 03:10:41
【问题描述】:
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);

我已返回此代码以生成 aspx 页面的 pdf(即 Default.aspx 到 Default.pdf)。它会生成一个 pdf 文件,但不支持生成的 pdf 文件我有最新版本的 pdf。它给出了打开文档的错误。文件已损坏,无法修复。

【问题讨论】:

  • 我已返回此代码以生成 aspx 页面的 pdf(即 Default.aspx 到 Default.pdf)。它生成一个 pdf 文件,但不支持生成的 pdf 文件我有最新版本pdf.打开文档时出现错误。文件已损坏,无法修复。
  • 另外,另一个错误是 document.Close();-> 它说它无法获取路径。我真的坚持这个。我需要帮助,所以请任何人都可以提供一些建议.提前致谢。
  • ITextSharp 库提供了将 aspx 转换为 PDS 的功能,以及其他功能,请阅读我的帖子,它会对您有所帮助。谢谢。

标签: c# asp.net pdf itext


【解决方案1】:

您需要下载ITextSharp 并将其引用添加到您的项目中。 ITextSharp 是一个免费的 HTML 到 PDF 库。您可以使用以下下载链接进行下载。

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>

命名空间

using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

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

【讨论】:

  • 基本上这是我非常古老的问题,wqs 已经解决了似乎你的答案也是正确的。还有一个名为 syncfusion 的库,它提供了比 iTextSharp 更多的功能。但它的成本。
【解决方案2】:

可以使用第三方库创建pdf如PDFizer

【讨论】:

  • 我已经下载了,请您指导我如何使用。我已经使用 ItextSharp.dll 完成了,但它不起作用。
  • Pdfizer 也是基于 iTextSharp 的。它可以工作,但免费版本不支持 CSS 和 backgroupd 图像。如果您想拥有好的工具,我建议您使用 ABCPdf。我正在使用 ABCPdf,对此我很满意。 websupergoo.com/abcpdf-9.htm
  • Doc theDoc = new Doc(); theDoc.AddImageUrl("google.com/"); theDoc.Save(Server.MapPath("htmlimport.pdf")); theDoc.Clear(); 当我使用上面的代码时,它会给我一个错误,比如类型或命名空间名称 doc could找不到你能告诉我为什么会出现这个错误吗?
猜你喜欢
  • 2018-08-19
  • 2011-08-10
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 2012-07-21
  • 1970-01-01
相关资源
最近更新 更多