2016-04-26 158 views
0

我正在生成一個簡單的pdf文件,其中包含一個表格。當我在文檔中添加一些短語時,它可以正常工作,但我也在該文檔中添加了一個表格,但表格未添加在文件中。任何人都可以告訴我我在這裏犯了什麼錯誤?提前致謝。 這裏是我的生成pdf.Everything代碼是除了PDF表格精細在PDF中添加pdf表格文檔

public Document GetPDFparams(Document disclaimer) 
    { 
     StringBuilder Content = new StringBuilder(); 
     Content.Append("Testing"); 
     Paragraph NullContent = new Paragraph(Content.ToString(), FontFactory.GetFont(FontFactory.TIMES_ROMAN, font_size, Font.NORMAL)); 
     disclaimer.Add(NullContent); 
     PdfPTable tableh = new PdfPTable(6); 
     tableh.WidthPercentage = 100; 
     tableh.SetTotalWidth(new float[] { 300f, 300f, 300f, 300f,300f,300f }); 
     tableh.HorizontalAlignment = Element.ALIGN_CENTER; 

     iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/Images/ORIXLOGO.png")); 
     //img.ScaleToFit(100, 40); 
     PdfPCell LogoCell = new PdfPCell(img); 
     LogoCell.HorizontalAlignment = Element.ALIGN_RIGHT; 
     LogoCell.VerticalAlignment = Element.ALIGN_BOTTOM; 
     LogoCell.Colspan = 2; 
     LogoCell.Padding = 20; 
     LogoCell.Border = 0; 
     LogoCell.Indent = 0;   

     tableh.AddCell(LogoCell); 


     Phrase LogoText = new Phrase("abc", FontFactory.GetFont(FontFactory.TIMES_BOLD, font_size)); 
     PdfPCell LogoTextCell = new PdfPCell(LogoText); 
     LogoTextCell.HorizontalAlignment = Element.ALIGN_LEFT; 
     LogoTextCell.VerticalAlignment = Element.ALIGN_LEFT; 
     LogoTextCell.Border = Rectangle.NO_BORDER; 
     LogoTextCell.PaddingTop = 15; 
     LogoTextCell.PaddingBottom = 5; 
     LogoTextCell.PaddingLeft = 5; 
     LogoTextCell.PaddingRight = 5; 
     tableh.AddCell(LogoTextCell); 

     disclaimer.Add(tableh); 
     return disclaimer; 
    } 
+0

http://www.dotnetfox.com/articles/how-to-create-table-in-pdf-document-using-Asp-Net-with-C-Sharp-和-itextsharp-1027.aspx –

+1

我完成了它,但仍然沒有在表格中添加表格。 –

+0

http://www.codeproject.com/Articles/18040/Tutorials-on-creating-PDF-files-using-C http://developers.itextpdf.com/examples/tables –

回答

0

最後我得到了solution.The問題是THA表中有每行6列,我測試只增加3列之後,所以後添加行列表的所有6列獲取添加:)。 這裏是我的代碼:

public Document GetPDFparams(Document disclaimer) 
    { 
     StringBuilder Content = new StringBuilder(); 
     Content.Append("Testing"); 
     Paragraph NullContent = new Paragraph(Content.ToString(), FontFactory.GetFont(FontFactory.TIMES_ROMAN, font_size, Font.NORMAL)); 
     disclaimer.Add(NullContent); 
     PdfPTable tableh = new PdfPTable(6); 
     tableh.WidthPercentage = 100; 
     tableh.SetTotalWidth(new float[] { 30f, 30f, 30f, 30f,30f,30f }); 
     iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/Images/ORIXLOGO.png")); 
     //img.ScaleToFit(100, 40); 
     PdfPCell LogoCell = new PdfPCell(img); 
     LogoCell.HorizontalAlignment = Element.ALIGN_RIGHT; 
     LogoCell.VerticalAlignment = Element.ALIGN_BOTTOM; 
     LogoCell.Colspan = 2; 
     LogoCell.Padding = 20; 
     LogoCell.Border = 0; 
     //LogoCell.Indent = 0;   

     tableh.AddCell(LogoCell); 


     Phrase LogoText = new Phrase("abc", FontFactory.GetFont(FontFactory.TIMES_BOLD, font_size)); 
     PdfPCell LogoTextCell = new PdfPCell(LogoText); 
     LogoTextCell.HorizontalAlignment = Element.ALIGN_LEFT; 
     LogoTextCell.VerticalAlignment = Element.ALIGN_LEFT; 
     LogoTextCell.Border = Rectangle.NO_BORDER; 
     tableh.AddCell(LogoTextCell); 
     tableh.AddCell(LogoTextCell); 
     tableh.AddCell(LogoTextCell); 
     tableh.AddCell(LogoTextCell); 

     disclaimer.Add(tableh); 
     return disclaimer; 
    }