[英]Save PDF from ashx handler using itextsharp
我在使用处理程序保存pdf时遇到问题。 当我直接从aspx页面的后端使用时,cs文件正常保存。 但是,当我尝试从ashx处理程序运行代码时,什么也没发生。 我的代码:
public void ProcessRequest(HttpContext context)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content- disposition", "attachment;filename=john.pdf");
HttpContext.Current.Response.Cache.SetCacheability (HttpCacheability.NoCache);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
string imagepath = context.Server.MapPath(@"~/img/logo3.png");
Document Doc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(Doc);
PdfWriter pdfwriter = PdfWriter.GetInstance(Doc, HttpContext.Current.Response.OutputStream);
Doc.Open();
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath);
image.ScalePercent(106f, 90f);
Doc.Add(image);
AddPDf(pdfwriter, Doc);
OnEndPage(pdfwriter, Doc);
Doc.Close();
HttpContext.Current.Response.End();
}
public void AddPDf(PdfWriter writer, Document document)
{
PdfPTable table = new PdfPTable(3);
table.TotalWidth = 400f;
//fix the absolute width of the table
table.LockedWidth = true;
//relative col widths in proportions - 1/3 and 2/3
float[] widths = new float[] { 2f, 4f, 6f };
table.SetWidths(widths);
table.HorizontalAlignment = 0;
//leave a gap before and after the table
table.SpacingBefore = 20f;
table.SpacingAfter = 30f;
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col 3 Row 2");
document.Open();
document.Add(table);
}
public void OnEndPage(PdfWriter writer, Document document)
{
var content = writer.DirectContent;
var pageBorderRect = new Rectangle(document.PageSize);
pageBorderRect.Left += document.LeftMargin;
pageBorderRect.Right -= document.RightMargin;
pageBorderRect.Top -= document.TopMargin;
pageBorderRect.Bottom += document.BottomMargin;
content.SetColorStroke(BaseColor.BLACK);
content.Rectangle(pageBorderRect.Left, pageBorderRect.Bottom, pageBorderRect.Width, pageBorderRect.Height);
content.Stroke();
}
private static void addCell(PdfPTable table, string text, int rowspan)
{
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 6, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);
PdfPCell cell = new PdfPCell(new Phrase(text, times));
cell.Rowspan = rowspan;
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
}
您可以这样导出pdf:
作为链接:
<a target="_blank" href="/exportPDF.ashx">Export PDF</a>
作为javascript函数:
function exportPDF() {
location.href = "/exportPDF.ashx";
}
Javascript会进行重定向,但是由于响应是文件,因此浏览器将提示您执行操作(打开/保存)并停留在同一页面上。
您的代码确实会生成PDF,因此您可以按原样使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.