0

iTextSharpを使用してGriddataをPDFにエクスポートしています。テーブルのセルに.jpg画像を挿入できません。griddata(billdata)を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();
                }
            }
        }
4

1 に答える 1

0

newPdfTable.HeaderRows=1を置き換えるだけです。newPdfTable.HeaderRows=0へ;

後もう一つ

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

そしてもう1つのパラメータがtrue

iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(image,true);
于 2012-05-04T07:50:17.453 に答える