2012-05-04 45 views
0

我正在使用iTextSharp將Griddata導出爲PDF。我無法將.jpg圖像插入到表格的單元格中。我想將griddata(billdata)導出爲PDF我可以在單元格中插入文本,但無法插入圖像。當在Ext.net中使用iTextsharp將griddata轉換爲PDF文件時,無法在表格的單元格上插入圖像

我的代碼是如下

protected void ToPDF(object sender, EventArgs e) 
     {  
      System.IO.MemoryStream PDFData = new System.IO.MemoryStream(); 
      iTextSharp.text.Document newDocument = new iTextSharp.text.Document(PageSize.A4.Rotate(), 10, 10, 10, 10); 
      iTextSharp.text.pdf.PdfWriter newPdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(newDocument, PDFData); 
      DataSet newDataSet = null; 

      string json = GridData.Value.ToString(); 
      if (json != "[]") 
      { 
       StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null); 
       XmlNode xml = eSubmit.Xml; 

       if (xml != null) 
       { 
        XmlTextReader xtr = new XmlTextReader(xml.OuterXml, XmlNodeType.Element, null); 
        newDataSet = new DataSet(); 
        newDataSet.ReadXml(xtr); 


        int totalColumns = newDataSet.Tables[0].Columns.Count; 
        iTextSharp.text.pdf.PdfPTable newPdfTable = new iTextSharp.text.pdf.PdfPTable(totalColumns - 3+1); 
        newPdfTable.DefaultCell.Padding = 1; 
        newPdfTable.WidthPercentage = 80; 
        newPdfTable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT; 
        newPdfTable.DefaultCell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;  
        newPdfTable.HeaderRows = 1; 
        newPdfTable.DefaultCell.BorderColor = new iTextSharp.text.BaseColor(255, 255, 255); 
        newPdfTable.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(255, 255, 255); 

        iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("D:\\company_logo.jpg"); 
        image.Alignment = iTextSharp.text.Image.ALIGN_LEFT; 

        iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(image); 
        cell.Rowspan = 5; 
        cell.HorizontalAlignment = 0; 
        //cell.Border = 1; 
        newPdfTable.AddCell(cell); 

        cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("Your company name", FontFactory.GetFont("Times New Roman", 18, Font.BOLD, new iTextSharp.text.BaseColor(80, 80, 80)))); 
        cell.Colspan = 6; 
        cell.HorizontalAlignment = 0; 
        //cell.Border = 1; 
        newPdfTable.AddCell(cell); 

        cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(Environment.NewLine)); 
        cell.Colspan = 6; 
        cell.HorizontalAlignment = 0; 
        //cell.Border = 1; 
        newPdfTable.AddCell(cell); 

        cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("Street Address", FontFactory.GetFont("Times New Roman", 12, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80)))); 
        cell.Colspan = 6; 
        cell.HorizontalAlignment = 0; 
        //cell.Border =1; 
        newPdfTable.AddCell(cell); 

        cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("City, Pincode", FontFactory.GetFont("Times New Roman", 12, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80)))); 
        cell.Colspan = 5; 
        cell.HorizontalAlignment = 0;     
        //cell.Border = 1; 
        newPdfTable.AddCell(cell); 

        string dt=Convert.ToString(Convert.ToDateTime(dtBill.Text).ToShortDateString()); 
        cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("Date : " + dt, FontFactory.GetFont("Times New Roman", 12, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80)))); 
        //cell.Colspan = 2; 
        cell.HorizontalAlignment = 0; 
        //cell.Border = 1; 
        newPdfTable.AddCell(cell); 

        cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("Phone no., Website, etc.", FontFactory.GetFont("Times New Roman", 12, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80)))); 
        cell.Colspan = 5; 
        cell.HorizontalAlignment = 0; 
        //cell.Border =1;  
        newPdfTable.AddCell(cell); 

        cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("Bill no : " + cmbBill.Text, FontFactory.GetFont("Times New Roman", 12, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80)))); 
        //cell.Colspan = 2;     
        cell.HorizontalAlignment = 0;     
        //cell.Border =1; 
        newPdfTable.AddCell(cell); 

        cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(Environment.NewLine)); 
        cell.Colspan = 7; 
        cell.HorizontalAlignment = 0; 
        //cell.Border = 1; 
        newPdfTable.AddCell(cell); 

        cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(Environment.NewLine)); 
        cell.Colspan = 7; 
        cell.HorizontalAlignment = 0; 
        //cell.Border = 1; 
        newPdfTable.AddCell(cell); 

        cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(Environment.NewLine)); 
        cell.Colspan = 7; 
        cell.HorizontalAlignment = 0; 
        //cell.Border = 1; 
        newPdfTable.AddCell(cell); 

        for (int i = 0; i <= totalColumns-1 ; i++) 
        { 

         if ((i != 0) & (i != 1) & (i != 2)) 
         { 
          cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(newDataSet.Tables[0].Columns[i].ColumnName, FontFactory.GetFont("Tahoma", 12, Font.BOLD, new iTextSharp.text.BaseColor(80, 80, 80)))); 
          cell.BorderColor = new iTextSharp.text.BaseColor(80, 80, 80); 
          cell.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.Color.LightGray); 
          cell.VerticalAlignment =iTextSharp.text.Element.ALIGN_MIDDLE; 
          if (i == 5) 
          { 
           cell.Colspan = 2; 
           cell.HorizontalAlignment = 1;  
          } 
          else 
          { 
           cell.HorizontalAlignment = 1; 
          } 
          newPdfTable.AddCell(cell); 
         } 

        } 
        int j = 0; 
        foreach (DataRow record in newDataSet.Tables[0].Rows) 
        { 
         for (int i = 0; i <= totalColumns - 1; i++) 
         { 

          if ((i != 0) & (i != 1) & (i != 2)) 
          { 
           if (i == 3) 
           { 
            if(record[i].ToString()=="true") 
             cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("Yes", FontFactory.GetFont("Tahoma", 10, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80)))); 
            else 
             cell = new iTextSharp.text.pdf.PdfPCell(new Phrase("No", FontFactory.GetFont("Tahoma", 10, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80)))); 
           } 
           else 
           { 
             cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(record[i].ToString(), FontFactory.GetFont("Tahoma", 10, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80)))); 
           } 

           if ((j % 2) == 0) 
           { 
            //cell.BorderColor = new iTextSharp.text.BaseColor(System.Drawing.Color); 
           } 
           else 
           { 
            cell.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.Color.WhiteSmoke); 
           } 
           cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE; 
           if (i == 3) 
            cell.Border = iTextSharp.text.pdf.PdfPCell.NO_BORDER; 
           else 
            cell.Border = iTextSharp.text.pdf.PdfPCell.LEFT_BORDER; 


           if (i == 5) 
           { 
            cell.Colspan = 2; 
            cell.HorizontalAlignment = 0; 
           } 
           else 
           { 
            if ((i == 6) | (i == 7) | (i == 8)) 
            { 
             cell.HorizontalAlignment = 2; 
            } 

            else 
            { 
             cell.HorizontalAlignment = 0; 
            } 
           } 

           newPdfTable.AddCell(cell); 
          } 
         } 
         j++; 
        } 

        newDocument.Open(); 
        newDocument.Add(new Phrase(Environment.NewLine)); 
        newDocument.Add(new Phrase(Environment.NewLine)); 
        newDocument.Add(newPdfTable); 
        //newDocument.Add(new Phrase("                                Total " + txtTotalQty.Text + "  " + txtTotalAmount.Text, FontFactory.GetFont("Trebuchet MS", 14, Font.BOLD, new iTextSharp.text.BaseColor(21, 66, 157)))); 
        newDocument.Add(new Phrase(Environment.NewLine)); 
        //newDocument.Add(new Phrase("Printed On: " + DateTime.Now.ToString(), FontFactory.GetFont("Tahoma", 9, Font.NORMAL, new iTextSharp.text.BaseColor(80, 80, 80)))); 
        newDocument.Close(); 
        Response.ContentType = "application/pdf"; 
        Response.Cache.SetCacheability(System.Web.HttpCacheability.Public); 
        Response.AppendHeader("Content-Type", "application/pdf"); 
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + this.ID + ".pdf"); 
        Response.OutputStream.Write(PDFData.GetBuffer(), 0, PDFData.GetBuffer().Length); 
        Response.OutputStream.Flush(); 
        Response.OutputStream.Close(); 
       } 
      } 
     } 

回答

0

只需更換newPdfTable.HeaderRows = 1;newPdfTable.HeaderRows = 0;

還有一件事

iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(image); 

和一個參數爲真

iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(image,true); 
相關問題