How to export PDF from data of datagrid?

2019-05-26 21:57发布

问题:

How can I export the data from DataGrid into PDF? The quick solution.

<DataGrid Name="table" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" IsTextSearchEnabled="True" Background="White">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Timestamp" Binding="{Binding StartDate}" SortDirection="Descending" SortMemberPath="StartDate" IsReadOnly="True" />
        <DataGridTextColumn Header="Title" Binding="{Binding Title}" IsReadOnly="True" />
        <DataGridTextColumn Header="Description" Binding="{Binding Description}" IsReadOnly="True" />
        <DataGridTextColumn Header="Type" Binding="{Binding Tag}" IsReadOnly="True" />
    </DataGrid.Columns>
</DataGrid>

回答1:

You can use iTextSharp (http://sourceforge.net/projects/itextsharp/) to generate datagrid to pdf.

(or) Look at http://wpfextendeddatagrid.codeplex.com/



回答2:

You can use iTextSharp to do it. You can add DataGrid visual in iTextSharp Document and use its PdfWriter to write it to the pdf file.

Below is the simple tutorial on it.

http://www.c-sharpcorner.com/uploadfile/f2e803/basic-pdf-creation-using-itextsharp-part-i/