【问题标题】:How to increase the table height from the code behind file in ASP.Net while using StringWriter?如何在使用 StringWriter 时从 ASP.Net 中的代码隐藏文件中增加表格高度?
【发布时间】:2011-10-11 00:07:33
【问题描述】:

我正在使用 StringWriter 和 HtmlTextWriter 从代码隐藏文件中生成一个 PDF。 编码如下:

System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView gv = new GridView();
gv.BorderStyle = BorderStyle.None; 
gv.DataSource = dt2;
gv.DataBind();
gv.RenderControl(hw);
string str = sw.ToString();
string str1 = "<table width='100%' border='1'><tr><td><img src='" + Server.MapPath("App_Themes/Ribo/ribologo.bmp") + "' alt='' width=75px height=75px /></td><td align='center' colspan='8' font size='3'><h2><b>MATERIAL RECEIPT CUM INSPECTION REPORT(MRIR)</b></h2</td></tr>";
str1 += "<tr><td font size='3'>MRIR NO</td><td font size='3'>Date</td><td align='center' font size='3'>JOB DESCRIPTION</td><td font size='3'>SUPPLIER NAME</td><td font size='3'>DC NO</td><td font size='3'>DATE</td><td font size='3'>LWB NO/DATE</td><td font size='3'>INVOICE NO</td><td font size='3'>DATE</td></tr>";
str1 += "<tr><td font size='3'>" + txtMRVNumber.Text + "</td><td font size='3'></td><td font size='3'></td><td font size='3'>" + TDSSVendor.Text + "</td><td font size='3'>" + txtDCNumber.Text + "</td><td font size='3'></td><td font size='3'>" + txtLWBNo.Text + "</td><td font size='3'>" + txtInvoiceNo.Text + "</td><td font size='3'></td></tr>";
str1 += "<tr><td rowspan='2' font size='3'>DESCRIPTION</td><td font size='3' colspan='2' align='center'>SIZE(mm)</td><td colspan='6'></td></tr>";
str1 += "<tr><td font size='3' colspan='2'>" + sw + "</td><td colspan='6'></td></tr></table>";           

if (str.StartsWith("<div>"))
{
    str = str1;
}
System.IO.StringReader sr = new System.IO.StringReader(str);
iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A3.Rotate(), 40f, 10f, 40f, 2f);
iTextSharp.text.html.simpleparser.HTMLWorker htmlparser = new iTextSharp.text.html.simpleparser.HTMLWorker(pdfDoc);
iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

在这里,我生成了我想要的 PDF。但是表格显示在 PDF 的顶部。所以我想显示在 PDF 的中心,并且我想增加表格的高度。如何做到这一点?

我尝试如下:

 string str1 = "<table **height='100%'** width='100%' border='1'><tr>.....

但它显示为相同。如何增加桌子的高度?我需要你的所有建议。

【问题讨论】:

    标签: c# html asp.net html-table


    【解决方案1】:

    仅凭这一点是行不通的。您可以将生成的 .pdf 包装在另一个表中(1 行 1 列),然后将该表放在新表的唯一 TD 中,然后仅垂直对齐 (valign='middle') 封闭的 TD。

    这是我知道如何做你所要求的唯一方法,虽然我不知道它是否适合你:

         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title>Untitled Page</title>
    
    <!-- Put this on your presentation page -->
                <style type="text/css">
                    html, body {
                        margin: 0;
                        padding: 0;
                        height: 100%;
                        border: none;
                    }
                </style>
            </head>
            <body>
                <table style="height: 100%" width="100%" align="center">
                    <tr>
                        <td valign="middle" align="center">
    
    
                            <table>
                                <tr>
                                    <td valign="middle">
                                    <!-- Embed your .pdf here -->
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </body>
            </html>
    

    【讨论】:

    • BTW - 以上内容在浏览器中可以工作,但谁知道当 HTML 转换为 .pdf 时会发生什么;您还必须使用 css 更改表格“高度”,表格标签上没有高度属性。
    • 好的。如何增加高度?
    • 我试过如下: string str1 = "
      .. . 但它仍然提供相同的 o/p。
    • @thevan - 查看我更新的 sn-p - 我误解了你想要做什么。
    【解决方案2】:

    从表格的高度你可以设置下面的文档类构造函数的参数。

    Dim doc As Document = New Document(PageSize.A4, 1, 0, 0, 30)
    

    希望对您有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 2012-04-29
      • 1970-01-01
      • 2012-10-13
      • 2012-05-02
      • 2018-08-21
      相关资源
      最近更新 更多