0

i tried to build a pdf dynamically based on certain conditions and it works pretty good in our machines and one of our client's machine. but for another customer, when we deployed the system, the pdf is not generating correctly. i have the following code to generate this

public void pdf_ModerateData(string name, string dob, string gender, string age)
{

    Phrase pat_name = new Phrase();
    Phrase phr_paitient_name = new Phrase();
    Phrase phr_paitient_dob = new Phrase();


    Paragraph pat_det = new Paragraph();
    Paragraph paitient_name_dob = new Paragraph();
    Paragraph mul_column = new Paragraph();

    iTextSharp.text.Font fntNormalText = FontFactory.GetFont(FontFactory.TIMES, 12, iTextSharp.text.Font.NORMAL);
    iTextSharp.text.Font fntBoldText = FontFactory.GetFont(FontFactory.TIMES, 12, iTextSharp.text.Font.BOLD);
    iTextSharp.text.Font fntsmallText = FontFactory.GetFont(FontFactory.TIMES, 8, iTextSharp.text.Font.NORMAL);



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

    pat_name = new Phrase(System.Environment.NewLine + "               " + System.Environment.NewLine + "                                                                                                                                                            ", fntsmallText);

    pat_det.Add(phr_paitient_name);
    pat_det.Add(pat_name);
    Phrase emt = new Phrase();
    Paragraph emty = new Paragraph();
    emt = new Phrase(System.Environment.NewLine + "    ", fntNormalText);
    emty.Add(emt);


    HttpContext.Current.Response.ContentType = "application/pdf";
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

    Document pdfDoc = new Document();
    PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);

    pdfDoc.Open();


    iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("C:/trial/dms/img/moderatetool.jpg");
    logo.ScaleAbsolute(475, 600);
    PdfPTable table = new PdfPTable(1);

    table.TotalWidth = 500f;

    table.LockedWidth = true;

    iTextSharp.text.Image barcode = iTextSharp.text.Image.GetInstance("C:/trial/dms/barcode/brcode.gif");
    barcode.ScaleAbsolute(100, 50);


    PdfPTable nested = new PdfPTable(2);
    mul_column.Add(pat_det);

    PdfPCell mul = new PdfPCell(mul_column);
    mul.HorizontalAlignment = Element.ALIGN_LEFT;
    mul.BorderWidth = 0;
    nested.AddCell(mul);
    //PdfPCell imag = new PdfPCell(barcode);
    //imag.HorizontalAlignment = Element.ALIGN_RIGHT;
    //imag.BorderWidth = 0;
    //nested.AddCell(imag);
    PdfPCell imag = new PdfPCell(barcode);
    imag.HorizontalAlignment = Element.ALIGN_RIGHT;
    imag.BorderWidth = 0;
    nested.AddCell(imag);
    PdfPTable nested1 = new PdfPTable(2);
    //mul_column.Add(pat_det);

    PdfPCell mul1 = new PdfPCell(emty);
    mul1.HorizontalAlignment = Element.ALIGN_LEFT;
    mul1.BorderWidth = 0;
    nested1.AddCell(mul1);
    PdfPCell imag1 = new PdfPCell(barcode);
    imag1.HorizontalAlignment = Element.ALIGN_RIGHT;
    imag1.BorderWidth = 0;
    nested1.AddCell(imag1);



    PdfPCell nesthousing = new PdfPCell(nested1);
    nesthousing.Padding = 0f;
    nesthousing.BorderWidth = 0;
    table.AddCell(nesthousing);

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

    table.AddCell(image_header);

    Paragraph para = new Paragraph();

    //string pat = get_patientDET();

    //para = new Paragraph(pat);


    PdfPCell header = new PdfPCell(para);
    header.HorizontalAlignment = Element.ALIGN_CENTER;
    header.Colspan = 4;
    header.BorderWidth = 0;
    //table.AddCell(header);
    PdfPCell nesthousing1 = new PdfPCell(nested);
    nesthousing.Padding = 0f;
    nesthousing.BorderWidth = 0;
    nesthousing1.BorderColor = BaseColor.WHITE;
    table.AddCell(nesthousing1);


    pdfDoc.Add(table);


    pdfDoc.Close();

    HttpContext.Current.Response.Write(pdfDoc);
    HttpContext.Current.Response.End();
}

can anybody guide me to solve this..

Server : windows 2008 R2 datacenter 64 bit servicepack1

when we download the pdf it downloads as " Gridviewreoprt.pdf,attachment " and cannot work properly.

4

1 回答 1

2

There's a 99% chance that the problem is caused by:

iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("C:/trial/dms/img/moderatetool.jpg");
iTextSharp.text.Image barcode = iTextSharp.text.Image.GetInstance("C:/trial/dms/barcode/brcode.gif");

Your customer would be crazy to give you access to the C: drive of their servers. Developers should avoid adding hard-coded paths to files on the C: drive in their code. You should encapsulate all resources (images, barcodes, PDF templates,...) in your web application so that your app can be deployed on every server without any requirements regarding the presence of and access to files stored elsewhere on the server.

于 2013-08-11T06:51:20.240 回答