2016-07-08 4 views
0

내 애플리케이션에서 gridview의 데이터를 이미지와 함께 PDF로 내보내고 있습니다. 아래 코드를 사용하여 텍스트 만 내보내고 있습니다. 이미지로 데이터를 내보내려면 어떻게해야합니까? 단지 그런Gridview를 PDF로 내보내기 (데이터가 포함 된 이미지 포함)

var img1 = iTextSharp.text.Image.GetInstance("c:\\img.png"); 
var img2 = iTextSharp.text.Image.GetInstance(new Uri("http://www.example.com/img.png")); 

: 당신은 당신이 오버로드 Image.GetInstance() 중 하나를 사용하여 검색 할 수있는 이미지가 무엇인지에 따라

gvDetails.DataSource = dt1; 
gvDetails.DataBind(); 

int colCount = gvDetails.Columns.Count ; 

PdfPTable table = new PdfPTable(colCount); 
table.HorizontalAlignment = 0; 
table.WidthPercentage = 100; 
int[] colWidths = new int[gvDetails.Columns.Count]; 
PdfPCell cell; 
string cellText; 
table.SetWidths(new int[] { 10, 05, 05, 05, 05, 25, 05,40 }); 
Document pdfDoc = new Document(iTextSharp.text.PageSize.A1, 3, 3, 10, 10); 
pdfDoc.Open(); 
MemoryStream mem = new MemoryStream(); 
PdfWriter pdf = PdfWriter.GetInstance(pdfDoc, mem); 

for (int colIndex = 0; colIndex < gvDetails.Columns.Count; colIndex++) 
{     
    cellText = Server.HtmlDecode(gvDetails.HeaderRow.Cells[colIndex].Text); 
    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA,BaseFont.CP1252,BaseFont.EMBEDDED); 
    iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.WHITE); 
    cell = new PdfPCell(new Phrase(cellText.Replace("<br />", Environment.NewLine), font)); 
    pdfDoc.Open(); 
    cell.HorizontalAlignment = Element.ALIGN_CENTER; 
    cell.VerticalAlignment = Element.ALIGN_MIDDLE; 
    cell.FixedHeight = 45f; 
    cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#a52a2a")); 
    table.AddCell(cell); 
} 

for (int rowIndex =0 ; rowIndex < gvDetails.Rows.Count; rowIndex++) 
{ 
    if (gvDetails.Rows[rowIndex].RowType == DataControlRowType.DataRow) 
    { 
     for (int j = 0; j < gvDetails.Columns.Count - 1; j++) 
     { 
      cellText = Server.HtmlDecode(gvDetails.Rows[rowIndex].Cells[j].Text); 
      cell = new PdfPCell(new Phrase(cellText, FontFactory.GetFont("PrepareForExport", 8))); 
      cell.HorizontalAlignment = Element.ALIGN_CENTER; 
      cell.VerticalAlignment = Element.ALIGN_MIDDLE; 
      cell.FixedHeight = 25f; 
      table.AddCell(cell); 
     } 
    } 
} 
pdfDoc.Add(table); 
pdfDoc.Close(); 
Response.ContentType = "application/pdf"; 
Response.AddHeader("Content-Disposition", "attachment;filename=test.pdf"); 
Response.BinaryWrite(mem.ToArray()); 
Response.Flush(); 
Response.End(); 

답변

1

: 여기

내가 PDF로 내보내기에 사용되는 코드입니다 cell에 추가 :

cell.AddElement(img1); 
관련 문제