使用itextsharp下载多个pdf文档

本文关键字:pdf 文档 itextsharp 下载 使用 | 更新日期: 2023-09-27 18:18:57

我有一个树形视图,其中有一组特定的文档列表和另一个具有患者详细信息的网格。我需要做的是,当我们从树视图中选择文档(选择多个…复选框启用)和从网格中选择患者并单击按钮时,应该使用ITEXTSHARP创建所有文档。也就是说创建了多个文档。我试过了,

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

                    }
                }
            }
        }
    }

}

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

但是系统只下载一个文档,谁能帮我一下

使用itextsharp下载多个pdf文档

如果你退后一步,稍微忽略pdf,只关注HTTP请求和响应,你应该有你的答案。当浏览器发出HTTP请求(单击按钮)时,服务器只允许发送一个响应。您的代码正在尝试(不成功)向浏览器发送多个响应。第一次调用Response.End时,管道实际上被终止,其余代码不运行。

解决方案是一次性制作一个巨大的PDF文件,制作单独的PDF文件并合并它们,或者创建一个包含所有PDF文件的zip文件。

我想你应该创建一个压缩文件,将你的PDF文件组合在一起

如果你想要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();

希望这对你有帮助!