如何在razorpdf中使用css

cod*_*der 2 asp.net-mvc-4 razorpdf

我正在使用mvc4,这里我razorpdf用来将我的视图转换为pdf,但是我收到了一个错误 - Unable to cast object of type iTextSharp.text.Paragraph to type iTextSharp.text.Table.

这是我正在使用的示例代码:

    @{

    Layout = "~/Views/Shared/_PdfLayout.cshtml";
}
<html>
    <body>
        <table border="1" width='500' bordercolor="RED"><tr><td colspan="3" bgcolor="LightGreen" align="center" valign="top">SSLC Marks Sheet 2013</td></tr></table></body>
</html>
Run Code Online (Sandbox Code Playgroud)

小智 7

由于您的代码使用的_PdfLayout.chtml代码需要使用xml格式的iTextSharp而不是html编写.删除正文标记,更改<tr><row>,更改<td><cell>,然后<chunk>在此处使用文本</chunk>将文本保存在每个单元格内.这是一个例子:

<paragraph style="font-family:Tahoma;font-size:18;font-style:normal;"&
    <chunk style="font-weight:bold;">Customer Address Report</chunk>
</paragraph>
<table width="100%" cellpadding="0" cellspacing="0.5" widths="16;12;12;12;12;12;12;12" borderwidth="1.0" left="true" right="true" top="true" bottom="true" red="0" green="0" blue="0">
    <row>
        <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true" horizontalalign="Center">
            <chunk style="font-size:10;font-weight:bold;">Customer Name</chunk>
        </cell>
        <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true" horizontalalign="Center">
            <chunk style="font-size:10;font-weight:bold;">Address 1</chunk>
        </cell>
        <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true" horizontalalign="Center">
            <chunk style="font-size:10;font-weight:bold;" align="Center">Address 2</chunk>
        </cell>
        <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true" horizontalalign="Center">
            <chunk style="font-size:10;font-weight:bold;" align="Center">Address 3</chunk>
        </cell>
        <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true" horizontalalign="Center">
            <chunk style="font-size:10;font-weight:bold;" align="Center">City</chunk>
        </cell>
        <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true" horizontalalign="Center">
            <chunk style="font-size:10;font-weight:bold;" align="Center">State</chunk>
        </cell>
        <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true" horizontalalign="Center">
            <chunk style="font-size:10;font-weight:bold;" align="Center">Postal Code</chunk>
        </cell>
        <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true" horizontalalign="Center">
            <chunk style="font-size:10;font-weight:bold;" align="Center">Country</chunk>
        </cell>
    </row>
    @foreach (var item in Model)
    {
        <row>
            <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true">
                <chunk style="font-size:10;font-weight:normal;">@Html.DisplayFor(modelItem => item.customerName)</chunk>
            </cell>
            <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true">
                <chunk style="font-size:10;font-weight:normal;">@Html.DisplayFor(modelItem => item.addr1)</chunk>
            </cell>
            <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true">
                <chunk style="font-size:10;font-weight:normal;">@Html.DisplayFor(modelItem => item.addr2)</chunk>
            </cell>
            <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true">
                <chunk style="font-size:10;font-weight:normal;">@Html.DisplayFor(modelItem => item.addr3)</chunk>
            </cell>
            <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true">
                <chunk style="font-size:10;font-weight:normal;">@Html.DisplayFor(modelItem => item.city)</chunk>
            </cell>
            <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true">
                <chunk style="font-size:10;font-weight:normal;">@Html.DisplayFor(modelItem => item.state)</chunk>
            </cell>
            <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true">
                <chunk style="font-size:10;font-weight:normal;">@Html.DisplayFor(modelItem => item.postalCode)</chunk>
            </cell>
            <cell borderwidth="0.5" left="true" right="true" top="true" bottom="true">
                <chunk style="font-size:10;font-weight:normal;">@Html.DisplayFor(modelItem => item.country)</chunk>
            </cell>
        </row>
    }
</table>
Run Code Online (Sandbox Code Playgroud)