2016-09-30 63 views
0

我遇到了使用處理程序保存pdf的問題。當我直接從aspx頁面的後端使用時,cs文件正常保存。但是當我嘗試從ashx處理程序運行代碼時,並沒有發生任何事情。 我的代碼:使用itextsharp保存來自ashx處理程序的PDF

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); 
    } 
+0

測試了您的代碼。有用。我得到了一個包含6個表格單元和一個「3個標題欄」的pdf。所以問題不在你的片段中。 – VDWWD

+0

你正在使用pdf文件夾?我在問,因爲代碼正確運行,但我無法找到任何地方pdf – Equilibrium

+0

瀏覽器提示我是否打開或保存。 – VDWWD

回答

0

導出這樣的PDF格式:

作爲鏈接:

<a target="_blank" href="/exportPDF.ashx">Export PDF</a> 

作爲一個JavaScript函數:

function exportPDF() { 
     location.href = "/exportPDF.ashx"; 
    } 

JavaScript並重定向,但由於響應是一個文件,瀏覽器會提示做什麼(打開/保存)並保持不變頁。

您的代碼確實會生成PDF,因此您可以按原樣使用它。