我在c#中使用以下代码导出PDF文件中的字符串。
if (ds.Tables[0].Rows.Count > 0)
{
StringBuilder sb = new StringBuilder();
double TotalUTI = 0;
displayHeader(ds.Tables[0], ref sb, ref TotalUTI);
for (int i = 1; i < ds.Tables.Count; i++)
{
displayBody(ds.Tables[i], ref sb);
}
FooterDisplay(ds.Tables[0], ref sb, TotalUTI);
StringReader sr = new StringReader(sb.ToString());
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=FundingSheet.pdf");
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
pdfDoc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.Flush();
Response.End();
}
displayheader(),displaybody()和displayfooter()是构建一个创建整个HTML表单的字符串的方法。 但不知何故,我收到错误,因为文件已损坏,无法修复。