2017-05-03 26 views
0

我正在使用asp.net/c#創建一個將要下載的PDF文件。這一切都是通過創建一系列加載到div創建的HTMLGenericControl中的HTMLTable來完成的。這個div然後被轉換成一個字符串並傳遞到生成PDF文件的函數中。下面是一個代碼示例:C#如何將圖像加載到代碼隱藏生成的表中?

//This dataset contains the student's name, age and grade, want it for all sections 
List<studentExportPersonal> lsep = sp.getExportPersonal(exportStudents); 

//Page Header containg Grade, Student Name, and Age 
HtmlGenericControl div = new HtmlGenericControl("div"); 

foreach (studentExportPersonal sep in lsep) 
{ 
    foreach (string sec in secs) 
    { 
     HtmlTable identityTable = new HtmlTable(); 
     div.Controls.Add(identityTable); 

     identityTable.Width = "100%"; 
     identityTable.Border = 0; 
     identityTable.Style.Add("margin-bottom", "20px"); 

     HtmlTableRow identityTR = new HtmlTableRow(); 
     identityTable.Rows.Add(identityTR); 

     HtmlTableCell tdGrade = new HtmlTableCell(); 
     identityTR.Cells.Add(tdGrade); 
     tdGrade.Style.Add("font-size", "30px"); 
     tdGrade.Align = "center"; 
     tdGrade.RowSpan = 2; 
     tdGrade.InnerText = sep.si.studentGrade; 

     HtmlTableCell tdStudentName = new HtmlTableCell(); 
     identityTR.Cells.Add(tdStudentName); 
     tdStudentName.Style.Add("font-size", "30px"); 
     tdStudentName.Align = "center"; 
     tdStudentName.InnerText = sep.si.studentName; 

     HtmlTableCell tdStudentAge = new HtmlTableCell(); 
     identityTR.Cells.Add(tdStudentAge); 
     tdStudentAge.Style.Add("font-size", "30px"); 
     tdStudentAge.Align = "center"; 
     tdStudentAge.RowSpan = 2; 
     tdStudentAge.InnerText = sep.si.studentAge + " Years Old"; 

     HtmlTableRow identityTR1 = new HtmlTableRow(); 
     identityTable.Rows.Add(identityTR1); 

     HtmlTableCell tdPic = new HtmlTableCell(); 
     identityTR1.Cells.Add(tdPic); 

     Bitmap studPic; 

     if (HttpContext.Current.Request.IsLocal) 
      studPic = (Bitmap)System.Drawing.Image.FromFile(@"C:\studentPics\" + sep.si.studentPicID, true); 
     else 
      studPic = (Bitmap)System.Drawing.Image.FromFile(@"E:\Website\Cascade\studentPics\" + sep.si.studentPicID, true); 
    } 
} 

export dmc = new export(); 

var sb = new StringBuilder(); 
div.RenderControl(new HtmlTextWriter(new StringWriter(sb))); 
string s = sb.ToString(); 

dmc.exportPDF("Student Profiles", "Portrait", s); 

在foreach循環你可以看到,我創建一個位圖變量的底部,我的問題是,如何將其加載到HTMLTableCell變量?

+0

爲什麼不使用CSS呢?另外,我注意到你正在引用本地文件的硬編碼字符串路徑,這些圖像在部署應用程序時不會顯示。 – Chad

+0

謝謝你,我想這是我想弄清楚的東西的類型......但這也是爲什麼我不使用CSS ...因爲一切都在後面的代碼中發生,表處理在擊中.aspx之前,所以css從來沒有機會進入...... –

+0

它是每個單元格的獨特映像,還是一個常用的映像?如果每個單元格都是獨特的圖像,那麼我們可以仔細看看您的邏輯。如果常見,那麼CSS是完美的 – Chad

回答

0

您可以將圖像添加到單元格,就像將單元格添加到行一樣。

HtmlImage image = new HtmlImage(); 
image.Src ="/studentPics/" + sep.si.studentPicID; 
tdPic.Controls.Add(image); 

然而,當我看着你的代碼,GridView將是我認爲更好的選擇。 您可以將List直接綁定到GridView。

<asp:GridView ID="GridView1" runat="server"></asp:GridView> 

if (!Page.IsPostBack) 
{ 
    GridView1.DataSource = lsep; 
    GridView1.DataBind(); 
} 
+0

HtmlImage - 完美!爲了提供更多的細節,我爲一個學校系統工作,這是爲了讓教師能夠在班上爲學生拍照並打印出他們的個人資料。學生的圖片ID存儲在數據庫中,因此在選擇學生後,我從數據庫中讀取ID,因此它是一個動態ID列表。但HtmlImage的作品非常漂亮。再次感謝!! –

+0

圖像是存儲在數據庫中,還是存儲在數據庫中的圖像的路徑,如果它是第一個,那麼在將圖像顯示在表中之前需要先將圖像轉換爲base64。 – VDWWD

相關問題