2013-07-27 25 views
2

我正在使用mvc4,這裏我使用razorpdf將我的視圖轉換爲pdf,但出現錯誤 - Unable to cast object of type iTextSharp.text.Paragraph to type iTextSharp.text.Table如何在razorpdf中使用css

這是我使用的示例代碼:

@{ 

    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> 
+0

在您的示例中設置Layout = null。否則,Razor Layoutengine將再次生成和 ...或者從您的視圖中刪除標籤。 – Obiwan007

回答

7

由於您的代碼使用的是_PdfLayout.chtml需要XML格式,而不是HTML中使用iTextSharp的編寫代碼。刪除身體標記,將<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> 
+0

什麼是其他HTML元素,如div和跨度的替代? – NMathur

0

IM坐在同樣的問題,似乎ü不能使用傳統上使用CSS在烏拉圭回合的HTML內容......只有一些內嵌樣式設置工作..這裏是一個例子是我沒有在我生成的表中獲取一些樣式....

順便說一下,我的佈局設置爲空(佈局=空)或否則它不會工作...我這樣做,因爲我沒有製作與句法相關的知識

<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>