2013-06-19 163 views
0

我有一個樹形視圖,其中有一組文檔列表和另一個具有患者詳細信息的網格。我需要做的是當我們從樹視圖中選擇文檔(選擇multiple..checkbox啓用)和網格中的患者並單擊按鈕時,應使用ITEXTSHARP創建所有文檔,即創建多個文檔。我試了一下這個樣子,使用itextsharp下載多個pdf文檔

按鈕點擊

foreach(TreeNode node in TreeView1.Nodes) 
    { 
     if (node.ChildNodes.Count>0) 
     { 
      for(int i=0 ;i<(node.ChildNodes.Count);i++) 
      { 
       if (node.ChildNodes[i].Checked==true) 
       { 
        string nodevalue = node.ChildNodes[i].Value.ToString(); 

        if (nodevalue=="3") 
        { 
         foreach (GridViewRow row in GridView1.Rows) 
          { 
           if (row.RowType == DataControlRowType.DataRow) 
           { 
            CheckBox chk = row.FindControl("CheckBox1") as CheckBox; 
            if (chk.Checked) 
            { 
             Label lbl_name = row.FindControl("Label1") as Label; 
             Label lbl_dob = row.FindControl("Label3") as Label; 
             pdf_abcd(lbl_name.Text.Trim(), lbl_dob.Text.Trim()); 
            } 
           } 
          } 
        } 


        } 
       } 
      } 
     } 
    } 


} 

和pdf_abcd功能

public void pdf_abcd(string name, string dob) 
{ 
    Phrase hospt_name = new Phrase(); 
    Phrase slogan = new Phrase(); 
    Phrase address = new Phrase(); 
    Phrase pat_name = new Phrase(); 
    Phrase phr_Consent_For_Treatment = new Phrase(); 
    Phrase phr_Professional_Care = new Phrase(); 
    Phrase phr_Consent_For_Treatment_head = new Phrase(); 
    Phrase phr_Professional_Care_head = new Phrase(); 
    Phrase phr_Nursing_Care = new Phrase(); 
    Phrase phr_Nursing_Care_head = new Phrase(); 
    Phrase phr_signtur_line = new Phrase(); 
    Phrase phr_signtur_bigfont = new Phrase(); 
    Phrase phr_signtur_smallfont_line = new Phrase(); 
    Phrase phr_signtur_date_line = new Phrase(); 
    Phrase phr_relationship = new Phrase(); 
    Phrase phr_witness = new Phrase(); 
    Phrase phr_paitient_name = new Phrase(); 
    Phrase phr_paitient_dob = new Phrase(); 

    Paragraph para = new Paragraph(); 
    Paragraph pat_det = new Paragraph(); 
    Paragraph Consent_For_Treatment = new Paragraph(); 
    Paragraph Professional_Care = new Paragraph(); 
    Paragraph Nursing_Care = new Paragraph(); 
    Paragraph Consent_For_Treatment_head = new Paragraph(); 
    Paragraph Professional_Care_head = new Paragraph(); 
    Paragraph Nursing_Care_head = new Paragraph(); 
    Paragraph signatur = new Paragraph(); 
    Paragraph relationship = new Paragraph(); 
    Paragraph witness = new Paragraph(); 
    Paragraph paitient_name_dob = new Paragraph(); 

    Font fntNormalText = FontFactory.GetFont(FontFactory.TIMES, 12, iTextSharp.text.Font.NORMAL); 
    Font fntBoldText = FontFactory.GetFont(FontFactory.TIMES, 12, Font.BOLD); 
    Font fntsmallText = FontFactory.GetFont(FontFactory.TIMES, 8, Font.NORMAL); 
    Font fntverysmallText = FontFactory.GetFont(FontFactory.TIMES, 6, Font.NORMAL); 
    Font fntBoldheadingText = FontFactory.GetFont(FontFactory.TIMES, 10, Font.BOLD); 
    Font fntparaText = FontFactory.GetFont(FontFactory.TIMES, 10, Font.NORMAL); 


    hospt_name = new Phrase("abcd", fntBoldText); 
    slogan = new Phrase(System.Environment.NewLine + "Quality health care here at home", fntNormalText); 
    address = new Phrase(System.Environment.NewLine + "P.O. Box 677", fntsmallText); 
    para.Add(hospt_name); 
    para.Add(slogan); 
    para.Add(address); 

    phr_paitient_name = new Phrase(System.Environment.NewLine + System.Environment.NewLine + System.Environment.NewLine + System.Environment.NewLine + name + "                      " + dob, fntNormalText); 

    pat_name = new Phrase(System.Environment.NewLine + "__________________________________________________________               _______________________________" + System.Environment.NewLine + "Patient's Name                                       D.O.B.", fntsmallText); 

    pat_det.Add(phr_paitient_name); 
    pat_det.Add(pat_name); 


    phr_Consent_For_Treatment_head = new Phrase(System.Environment.NewLine + "Consent For Treatment", fntBoldheadingText); 
    Consent_For_Treatment_head.Add(phr_Consent_For_Treatment_head); 

    phr_Consent_For_Treatment = new Phrase("The undersigned consents to procedures and treatments which may be performed during this hospitalization or on an outpatient or emergency basis, including but not limited to anesthesia, laboratory procedures, medical or surgical treatments, x-ray examination, or other services rendered under the general and specific instructions of the physician, physician's assistant, nurse practitioner or Designee. In order to manage accidental exposure of a health care worker to blood or other bodily fluids, the undersigned further consents to such testing; including but not limited to AIDS, TB, Syphilis, and Hepatitis testing, as may be necessary for protection of the heath care worker.", fntparaText); 
    Consent_For_Treatment.Add(phr_Consent_For_Treatment); 

    phr_Professional_Care_head = new Phrase(System.Environment.NewLine + "Professional Care", fntBoldheadingText); 
    Professional_Care_head.Add(phr_Professional_Care_head); 

    phr_Professional_Care = new Phrase("The attending physician, usually selected by the patient except under unusual or emergency circumstances, is the professional who arranges for the patient's care and treatment Doctors of medicine, including anesthesia provider, pathologists, radiologists, emergency room physicians, osteopathy, podiatry, etc., are independent contractors and are not employees of Val Verde Regional Medical Center. You will receive a separate bill from the physician/anesthesia provider.", fntparaText); 
    Professional_Care.Add(phr_Professional_Care); 

    phr_Nursing_Care_head = new Phrase(System.Environment.NewLine + "Nursing Care", fntBoldheadingText); 
    Nursing_Care_head.Add(phr_Nursing_Care_head); 

    phr_Nursing_Care = new Phrase("The hospital provides general nursing care. Private duty nursing must be arranged by the patient's representative. The hospital is not responsible for and is released from all liabilities for failure to provide such care.", fntparaText); 
    Nursing_Care.Add(phr_Nursing_Care); 


    phr_signtur_line = new Phrase(System.Environment.NewLine + System.Environment.NewLine + "__________________________________________________________________________________________   ________________", fntsmallText); 
    phr_signtur_bigfont = new Phrase(System.Environment.NewLine + "Signature of Patient/Responsible Party/Patient Representative", fntNormalText); 
    phr_signtur_smallfont_line = new Phrase("(If patient unable to sign)", fntverysmallText); 
    phr_signtur_date_line = new Phrase("  Date ", fntNormalText); 

    signatur.Add(phr_signtur_line); 
    signatur.Add(phr_signtur_bigfont); 
    signatur.Add(phr_signtur_smallfont_line); 
    signatur.Add(phr_signtur_date_line); 

    phr_relationship = new Phrase(System.Environment.NewLine + "______________________________________________________________" + System.Environment.NewLine + "Relationship to patient", fntNormalText); 
    relationship.Add(phr_relationship); 


    phr_witness = new Phrase(System.Environment.NewLine + "___________________________________            __________" + System.Environment.NewLine + "Signature of Witness                       Date", fntNormalText); 
    witness.Add(phr_witness); 


    Document Doc = new Document(PageSize.A4, 0, 0, 25, 50); 




    PdfWriter.GetInstance(Doc, new FileStream(Server.MapPath("~/abcd/abcd.pdf"), FileMode.Create)); 

    Doc.Open(); 

    PdfPTable table = new PdfPTable(1); 

    table.TotalWidth = 500f; 

    table.LockedWidth = true; 

    iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("~/img/abcd.png"); 
    logo.ScaleAbsolute(30, 30); 

    PdfPCell image_header = new PdfPCell(logo); 
    image_header.HorizontalAlignment = Element.ALIGN_CENTER; 
    image_header.BorderWidth = 0; 

    table.AddCell(image_header); 

    PdfPCell header = new PdfPCell(para); 
    header.HorizontalAlignment = Element.ALIGN_CENTER; 
    header.Colspan = 4; 
    header.BorderWidth = 0; 
    table.AddCell(header); 

    PdfPCell patient = new PdfPCell(pat_det); 
    patient.BorderWidth = 0; 
    table.AddCell(patient); 

    //PdfPCell patientname = new PdfPCell(new Phrase(System.Environment.NewLine + System.Environment.NewLine + "Patient's Name                  D.O.B.")); 
    //patientname.BorderWidth = 0; 
    //table.AddCell(patientname); 

    PdfPCell head_treatment = new PdfPCell(new Phrase(Consent_For_Treatment_head)); 
    head_treatment.BorderWidth = 0; 
    table.AddCell(head_treatment); 

    PdfPCell treatment_content = new PdfPCell(Consent_For_Treatment); 
    treatment_content.BorderWidth = 0; 
    table.AddCell(treatment_content); 



    PdfPCell head_profcare = new PdfPCell(Professional_Care_head); 
    head_profcare.BorderWidth = 0; 
    table.AddCell(head_profcare); 

    PdfPCell profcare_content = new PdfPCell(Professional_Care); 
    profcare_content.BorderWidth = 0; 
    table.AddCell(profcare_content); 




    PdfPCell head_nursing = new PdfPCell(Nursing_Care_head); 
    head_nursing.BorderWidth = 0; 
    table.AddCell(head_nursing); 

    PdfPCell nursing_content = new PdfPCell(Nursing_Care); 
    nursing_content.BorderWidth = 0; 
    table.AddCell(nursing_content); 



    PdfPCell sig = new PdfPCell(signatur); 
    sig.BorderWidth = 0; 
    table.AddCell(sig); 



    PdfPCell raltntopatient = new PdfPCell(relationship); 
    raltntopatient.BorderWidth = 0; 
    table.AddCell(raltntopatient); 



    PdfPCell witnesslines = new PdfPCell(witness); 
    witnesslines.BorderWidth = 0; 
    table.AddCell(witnesslines); 

    Doc.Add(table); 

    Doc.Close(); 
    string path = Server.MapPath("~/abcd/Dabcd.pdf"); 

    ShowPdf(path); 
    //Response.Redirect(Server.MapPath("~/abcd/abcd.pdf")); 


} 

private void ShowPdf(string strS) 
{ 
    Response.ClearContent(); 
    Response.ClearHeaders(); 
    Response.ContentType = "application/pdf"; 
    Response.AddHeader("Content-Disposition", "attachment; filename=" + strS); 
    Response.TransmitFile(strS); 
    Response.End(); 
    Response.Flush(); 
    Response.Clear(); 

} 

但系統只下跌加載一個文檔..誰可以幫我

回答

0

我想你應該創建一個結合了你的PDF文件的zip文件

+0

HttpContext.Current.Response.ContentType = 「應用/ PDF」 HttpContext.Current.Response.AddHeader( 「內容處置」, 「附件;文件名= GridViewExport.pdf」) HttpContext.Current.Response.Cache。 SetCacheability(HttpCacheability.NoCache) 昏暗pdfDoc爲新文件的() PdfWriter.GetInstance(pdfDoc,HttpContext.Current.Response.OutputStream) pdfDoc.Open() 「WRITE PDF pdfDoc.Add(新段(「我的第一PDF「)) 'END WRITE PDF >>>>> pdfDoc.Close() HttpContext.Current.Response.Write(pdfDoc) HttpContext.Current.Response.End()..如何在新標籤 – Sivajith

3

如果你退後一步並忽略了一點點的PDF,只專注於HTTP請求和響應,你應該有你的答案。當瀏覽器發出一個HTTP請求時(您的按鈕點擊),服務器被允許發送一個且只有一個響應。您的代碼正在嘗試(不成功)向瀏覽器發送多個響應。第一次調用Response.End時,管道會實際終止,而其餘代碼不會運行。

解決方案是在一次傳遞中製作一個巨型PDF,製作包含所有PDF的個別PDF和mergethemcreate a zip文件。

+0

中打開它或做一些AJAX魔術...... – mkl

0

如果你想Openfile in new tab你有不同的方法:

方法1:

Response.Clear(); 
Response.ContentType = "application/pdf"; 
Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf"); 
Response.TransmitFile(filePath); 
Response.End(); 

方法2:

Process process = new Process(); 
process.StartInfo.UseShellExecute = true; 
process.StartInfo.FileName = outputPdfFile; 
process.Start(); 

希望這可以幫助你!