简体   繁体   中英

Download multiple pdf documents using itextsharp

i have a tree view in which there are certain set of document list and another grid having patient details. what i need to do is when we select docs from treeview (select multiple..checkbox enabled) and patients from grid and click on a button should create all the documents using ITEXTSHARP.that is multiple documents are created. I tried it like this,

on button click

 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());
                                    }
                                }
                            }
                    }


                    }
                }
            }
        }
    }


}

and the pdf_abcd function is

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"));


}

and

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();

}

but the system only down loads one doc..can anyone helpme

If you step back and ignore PDFs for a bit and concentrate on just HTTP requests and responses you should have your answer. When a browser makes an HTTP request (your button click) the server is allowed to send one and only one response. You're code is trying (unsuccessfully) to send multiple responses to the browser. The first time that Response.End is called the pipeline gets terminated actually and the rest of your code doesn't run.

The solution is to just make one giant PDF in one pass, make individual PDFs and merge them or create a zip file containing all of the PDFs.

我想你应该创建一个包含PDF文件的zip文件

If you want to Open the file in new tab you have different Methods:

Method 1:

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

Method 2:

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

Hope this helps you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM