簡體   English   中英

使用 iTextSharp 的 PDF 視圖

[英]Pdf view using iTextSharp

我想要使​​用 ITextSharp 的 pdf 視圖,我使用數據表來使用 JQuery 將來自外部 API 的數據制成表格。

我嘗試了一些代碼但沒有結果..

報表控制器:

 [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");
        }
    }

索引.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>  

.. 有什么建議 ? 在此處輸入圖片說明

搜索按鈕顯示帶有數據的表格,然后單擊導出應 pdf 所有視圖..

下面顯示的 HTML 是原始 HTML 表格元素,在它被 DataTables 增強之前:

<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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM