如何通过itextsharp导出到PDF格式时正确导出HTML表格? [英] How to export HTML tables properly aligned on exporting to PDF via itextsharp?

查看:122
本文介绍了如何通过itextsharp导出到PDF格式时正确导出HTML表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Itextsharp将Html表导出为PDF。它工作正常。有2个html表,这些表是垂直的。我需要让它们左右水平对齐。

下面是我的核心HTML代码:

< div> 
< asp:Panel ID =pnltestrunat =server>
< table border =1width =100style =float:left>
< tr>< td>名称< / td>< td>年龄< / td>< / tr>


< / table>
< table border =1width =100style =float:left>
< tr>< td>名称< / td>< td>年龄< / td>< / tr>


< / table>
< / asp:Panel>
< / div>





我的尝试:



通过itextsharp dll导出的html表格为PDF。但渲染时的表格对齐是垂直而不是水平。

解决方案





这样更好使用代码而不是HTML来显示PDF页面上的表格。它提供了设计桌子的灵活性。



请按照以下代码解决您的问题:



 PdfPTable table = new PdfPTable(5); 
//在表格中显示数据。
//5总列数。


//这里我设置了每列的宽度。
float [] widths = new float [] {100f,80f,100f,90f,130f};
table.SetWidths(widths);

//每列标题的名称。
table.AddCell(Name);
table.AddCell(Age);
table.AddCell(Band);
table.AddCell(指定);
table.AddCell(当前项目);

//在表格中添加值。
//如果要获取列表值,请使用FOREACH循环显示表
table.AddCell(emp.Name)中的所有数据;
table.AddCell(emp.Age.ToString());
table.AddCell(emp.Band);
table.AddCell(emp.Designation);
table.AddCell(emp.Project);

//这里我要在表格中添加表格。
document.Add(table);





要获取更多信息,请访问以下链接:



如何在MVC中下载pdf [ ^


I am exporting Html tables via Itextsharp to PDF . It is working fine. There are 2 html tables and those are coming vertically. I need to have them aligned horizontally left and right.
Below is my core html code:

<div>
       <asp:Panel ID="pnltest"  runat="server" >
   <table border = "1" width = "100" style="float:left">
   <tr><td>Name</td><td>Age</td></tr>


   </table>
   <table border = "1" width = "100" style="float:left">
   <tr><td>Name</td><td>Age</td></tr>


   </table>
           </asp:Panel>
   </div>



What I have tried:

Exported html tables via itextsharp dll to PDF. But the table alignment on rendering is coming vertically instead of horizontally.

解决方案

Hi,

It's better to use code behind rather than HTML to show tables on PDF page. It provides flexibility to design your tables.

follow these code to solve your problem:

PdfPTable table = new PdfPTable(5); 
            // To show data in table.
            // "5" total number of columns.


            // Here I have set width of every columns.
            float[] widths = new float[] { 100f, 80f, 100f, 90f, 130f };
            table.SetWidths(widths);

            // Name of the header for each column.
            table.AddCell("Name");
            table.AddCell("Age");
            table.AddCell("Band");
            table.AddCell("Designation");
            table.AddCell("Current Project");

            // Adding value in the table.
            // If you are getting list values the use FOREACH loop to show all data in                                                     table  
            table.AddCell(emp.Name);
            table.AddCell(emp.Age.ToString());
            table.AddCell(emp.Band);
            table.AddCell(emp.Designation);
            table.AddCell(emp.Project);
            
            // Here I am adding table to the document.
            document.Add(table);



To get more information check this link:

How to download pdf in MVC[^]


这篇关于如何通过itextsharp导出到PDF格式时正确导出HTML表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆