繁体   English   中英

使用itextsharp下载多个pdf文档

[英]Download multiple pdf documents using itextsharp

我有一个树视图,其中有一组文档列表和另一个具有患者详细信息的网格。 我需要做的是当我们从treeview中选择文档时(选择multiple..checkbox enabled)和来自网格的患者并点击按钮应该使用ITEXTSHARP创建所有文档。即创建多个文档。 我试过这样的,

点击按钮

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

}

但系统只下载一个doc ..任何人都可以帮助

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

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

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

如果要file in new tab Open file 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();

希望这对你有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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