2011-07-19 4 views
0

私は、必要なときに印刷する情報を表示するパネルを持つユーザーコントロールを持っています。 私は検索結果を得るグリッドビューを持っています上記のパネルのpdfを開きたいリンクボタンをクリックしています。ページをロードせずにリンクボタンをクリックするとpdfファイルを動的に生成する方法

+0

。あなたは最後の声明を見逃しましたか? –

答えて

2

私はこれを実装しました。私は他の誰かがそれをやりなおすことを願っています。私は下に私のコードを記載している。

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

インポート役立つだろう、関連するコードを表示上記のDLL

string attachment = "attachment; filename=" + "File Name" + ".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"); 
    Page pg = new Page(); 
    HtmlForm frm = new HtmlForm(); 
    pg.EnableEventValidation = false; 

    pg.RenderControl(htextw); 
    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(); 
    //This is how you can add text in div in a pdf 
    Chunk c = new Chunk(TextHere + "\n", FontFactory.GetFont("Verdana", 15)); 
    Paragraph p = new Paragraph(); 
    p.Alignment = Element.ALIGN_LEFT; 
    p.Add(c); p.Add(c1); 
    // This is how you can generate table and can set proprties 
    PdfPTable table = new PdfPTable(2); 
    table.WidthPercentage = 100; 
    //Bill No and Bill Date 
    PdfPCell pdfcell1 = new PdfPCell(new Phrase("Text in TAble" + "TExt From Database")); 
    pdfcell1.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER | iTextSharp.text.Rectangle.TOP_BORDER; 
    table.AddCell(pdfcell1); 
    PdfPCell pdfcell = new PdfPCell(new Phrase("Text in TAble" + "TExt From Database")); 
    pdfcell.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER | iTextSharp.text.Rectangle.TOP_BORDER; 
    pdfcell.HorizontalAlignment = Element.ALIGN_RIGHT; 
    table.AddCell(pdfcell); 

    document.Add(p); 
    document.Add(table); 
    document.Add(tablegrid); 
    StringReader str = new StringReader(stw.ToString()); 
    HTMLWorker htmlworker = new HTMLWorker(document); 
    htmlworker.Parse(str); 

    document.Close(); 
    Response.Write(document); 
0

私はあなたがこの問題に遭遇するかもしれないと思います。 JavaScriptは(意図的に)ファイルを操作することができないので、これをすべてクライアントサイドで行うことはできません。 は、だからあなたのオプションは次のとおりです。

  1. のフルポストバック
  2. Ajax呼び出し

カスタムハンドラ(.ashx)PDFファイルで応答を返すwhickファイルへのAjax呼び出しを試みることができます。私は前にこれを試していないが、うまくいくかもしれない。どのようにtarget="_blank"で別のページを開くと元のページは元のままのまま、サーバー側のコードを実行し、PDFを生成しますか?

+0

私はこれもサーバ側のコードを使って行うことができます。それを手伝ってもらえますか? – Aman

+1

私はあなたの最善の策は、これを行う多くの仲間の1人を使うことだと思います。いくつかは他のものより優れており、いくつかは無料です。 Googleですばやく検索するだけで手のひらが見つかります。 –

+0

私の質問をしなかった私の友人は、質問を読んで解決策を提供するだけで何もこれを行うことはありません – Aman

関連する問題