使用iTextSharp的Pdf视图

时间:2018-02-10 13:29:58

标签: asp.net-mvc pdf datatable itext

我想使用ITextSharp进行pdf视图,我使用数据表来使用JQuery来表示来自外部API的数据..我搜索了它但我发现他们使用的是模型中的数据而我没有处理模型,因为我处理外部API

我尝试了一些代码但没有结果..

ReportController:

 [HttpPost]
    [ValidateInput(false)]
    public FileResult Export(string GridHtml)
    {
        using (MemoryStream stream = new System.IO.MemoryStream())
        {
            StringReader sr = new StringReader(GridHtml);
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
            pdfDoc.Open();
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
            pdfDoc.Close();
            return File(stream.ToArray(), "application/pdf", "StudentDetails.pdf");
        }
    }

Index.cshtml:

 @using (Html.BeginForm("Export", "Home", FormMethod.Post))  
{  
    <div style="text-align: center;background-color:yellowgreen;width:100%">  
        <input type="hidden" name="GridHtml" />  
        @*<input type="submit" id="btnSubmit" value="Export" />*@  
        <span style="font-family: Arial Black;color:red; font-size:larger;font-style: oblique">Export PDF</span>  
        <input type="image" id="btnSubmit" src="~/Images/Pdf.png" value="Pdf" />  
    </div>  
    <br />  
    @*<input type="hidden" name="GridHtml" />  
        <input type="submit" id="btnSubmit" value="Export" />*@  
    <div id="Grid">  
                            <table class="table table-bordered table-hover" id="VacationsReport_table">
                    <thead>
                        <tr>
                            <th>تاريخ الطلب</th>
                            <th>كود الموظف</th>
                            <th>الوظيفة</th>
                            <th>نوع الاجازة</th>
                            <th>بداية الاجازة</th>
                            <th>نهاية الاجازة</th>
                            <th>سبب الاجازة</th>
                            <th>مدة الاجازة</th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
    </div>  

..有什么建议吗?enter image description here

使用数据搜索按钮显示表,然后单击“导出应该pdf all view ..”

1 个答案:

答案 0 :(得分:-1)

在DataTables增强之前,下面显示的HTML是原始HTML表格元素:

<table id="example" class="display nowrap" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
            <tr>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
            <tr>
                <td>Ashton Cox</td>
                <td>Junior Technical Author</td>
                <td>San Francisco</td>
                <td>66</td>
                <td>2009/01/12</td>
                <td>$86,000</td>
            </tr>
            <tr>
                <td>Cedric Kelly</td>
                <td>Senior Javascript Developer</td>
                <td>Edinburgh</td>
                <td>22</td>
                <td>2012/03/29</td>
                <td>$433,060</td>
            </tr>

        </tbody>
    </table>

下面显示的Javascript用于初始化此示例中显示的表:

$(document).ready(function() {
    $('#example').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
    } );
} );

加载以下CSS库文件以在此示例中使用以提供表的样式:

https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css https://cdn.datatables.net/buttons/1.5.1/css/buttons.dataTables.min.css

请找到以下链接,这将有助于您

https://datatables.net/extensions/buttons/examples/initialisation/export.html