Asp.net mvc 4 如何在razorpdf中使用css

Asp.net mvc 4 如何在razorpdf中使用css,asp.net-mvc-4,razorpdf,Asp.net Mvc 4,Razorpdf,我正在使用mvc4,这里我正在使用razordf将我的视图转换为pdf,但是我得到一个错误-无法将iTextSharp.text.paragration类型的对象强制转换为iTextSharp.text.Table 这是我正在使用的示例代码: @{ Layout = "~/Views/Shared/_PdfLayout.cshtml"; } <html> <body> <table border="1" width='500

我正在使用
mvc4
,这里我正在使用
razordf
将我的视图转换为pdf,但是我得到一个错误-
无法将iTextSharp.text.paragration类型的对象强制转换为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>
@{
Layout=“~/Views/Shared/_PdfLayout.cshtml”;
}
2013年SSLC标志表

由于您的代码使用的是
\u PdfLayout.chtml
,因此需要使用iTextSharp以xml格式而不是html格式编写代码。移除正文标签,将
更改为
,将
更改为
,然后使用
此处的文本
将文本保存在每个单元格内。以下是一个例子:

<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>
item.customerName)
@DisplayFor(modelItem=>item.addr1)
@DisplayFor(modeleItem=>item.addr2)
@DisplayFor(modelItem=>item.addr3)
@DisplayFor(modeleItem=>item.city)
@DisplayFor(modelItem=>item.state)
@DisplayFor(modelItem=>item.postalCode)
@DisplayFor(modelItem=>item.country)
}

我也遇到了同样的问题,似乎你不能在html内容中使用传统的css。。。只有一些内联样式设置有效。。下面是我在生成的表中获得一些样式的示例

顺便说一下,我的布局被设置为null(Layout=null),否则它将无法工作。。。我这样做是因为我不知道如何用语法制作表格

   <itext creationdate="@DateTime.Now.ToString()" producer="RazorPDF">
    <table>
        <tr>
            <th align="center">@Model.CampaignName</th>
        </tr>
    </table>
    <h4>@ViewBag.CampaignName</h4>

    <table border="1" cellpadding="3">
        <thead>
            <tr>
                <td colspan="5" align="center">
                    <strong>Delivery List</strong>
                </td>
            </tr>
            <tr>
                <th>@Html.DisplayNameFor(m => m.DeliveryList.FirstOrDefault().ShopName)</th>
                <th width="35%">@Html.DisplayNameFor(m => m.DeliveryList.FirstOrDefault().MediaTypeName)</th>
                <th>@Html.DisplayNameFor(m => m.DeliveryList.FirstOrDefault().Count)</th>
                <th>@Html.DisplayNameFor(m => m.DeliveryList.FirstOrDefault().Width)</th>
                <th>@Html.DisplayNameFor(m => m.DeliveryList.FirstOrDefault().Height)</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.DeliveryList)
            {
                <tr>
                    <th>@Html.DisplayFor(m => item.ShopName) - @item.ShopAddress</th>
                    <td>@Html.DisplayFor(m => item.MediaTypeName)</td>
                    <th>@Html.DisplayFor(m => item.Count)</th>
                    <td>@Html.DisplayFor(m => item.Width)</td>
                    <td>@Html.DisplayFor(m => item.Height)</td>
                </tr>
            }
        </tbody>
    </table>
    <br />
    <table border="1" cellpadding="3">
        <thead>
            <tr>
                <td colspan="5" align="center">
                    <strong>Production List</strong>
                </td>
            </tr>
            <tr>
                <th>@Html.DisplayNameFor(m => m.ProductionList.FirstOrDefault().MediaTypeName)</th>
                <th width="45%">@Html.DisplayNameFor(m => m.ProductionList.FirstOrDefault().Comments)</th>
                <th>@Html.DisplayNameFor(m => m.ProductionList.FirstOrDefault().CountAll)</th>
                <th>@Html.DisplayNameFor(m => m.ProductionList.FirstOrDefault().Width)</th>
                <th>@Html.DisplayNameFor(m => m.ProductionList.FirstOrDefault().Height)</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.ProductionList)
            {
                <tr>
                    <td>@Html.DisplayFor(m => item.MediaTypeName)</td>
                    <th width="30%">@Html.DisplayFor(m => item.Comments)</th>
                    <th>@Html.DisplayFor(m => item.CountAll)</th>
                    <th>@Html.DisplayFor(m => item.Width)</th>
                    <th>@Html.DisplayFor(m => item.Height)</th>

                </tr>
            }
        </tbody>
    </table>
</itext>

@模型名称
@ViewBag.campaigname
交付清单
@DisplayNameFor(m=>m.DeliveryList.FirstOrDefault().ShopName)
@DisplayNameFor(m=>m.DeliveryList.FirstOrDefault().MediaTypeName)
@DisplayNameFor(m=>m.DeliveryList.FirstOrDefault().Count)
@DisplayNameFor(m=>m.DeliveryList.FirstOrDefault().Width)
@DisplayNameFor(m=>m.DeliveryList.FirstOrDefault().Height)
@foreach(Model.DeliveryList中的var项)
{
@Html.DisplayFor(m=>item.ShopName)->@item.ShopAddress
@DisplayFor(m=>item.MediaTypeName)
@DisplayFor(m=>item.Count)
@DisplayFor(m=>item.Width)
@DisplayFor(m=>item.Height)
}

生产清单 @DisplayNameFor(m=>m.ProductionList.FirstOrDefault().MediaTypeName) @DisplayNameFor(m=>m.ProductionList.FirstOrDefault().Comments) @DisplayNameFor(m=>m.ProductionList.FirstOrDefault().CountAll) @DisplayNameFor(m=>m.ProductionList.FirstOrDefault().Width) @DisplayNameFor(m=>m.ProductionList.FirstOrDefault().Height) @foreach(Model.ProductionList中的var项) { @DisplayFor(m=>item.MediaTypeName) @DisplayFor(m=>item.Comments) @DisplayFor(m=>item.CountAll) @DisplayFor(m=>item.Width) @DisplayFor(m=>item.Height) }
Set Layout=null在您的示例中。否则将生成剃刀布局引擎,然后再次。。。或者从视图中删除标记。其他html元素(如div和span)的替换内容是什么?