Aya Abdelaziz
Aya Abdelaziz

Reputation: 375

Pdf view using iTextSharp

I wanna pdf view using ITextSharp , i use datatables to table data from external API using JQuery .. I searched for it but i found that they use data from Model and i'm not dealing with model as i deal with external API

I tried some codes but no result ..

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>  

.. Any Suggestions ?enter image description here

search button display table with data , then clicking Export should pdf all view ..

Upvotes: 0

Views: 737

Answers (1)

Azhar
Azhar

Reputation: 343

The HTML shown below is the raw HTML table element, before it has been enhanced by 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>

The Javascript shown below is used to initialise the table shown in this example:

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

The following CSS library files are loaded for use in this example to provide the styling of the table:

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

please find following link this will help you

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

Upvotes: -1

Related Questions