Skip to main content Skip to footer

Saving C1DataGrid for WPF as PDF

Below I have provided a way to save the contents from a C1DataGrid to PDF using the Windows Forms version of C1PdfDocument (C1.C1Pdf.4.dll). It is quite possible to use a Windows Forms library in WPF. The ComponentOne PDF for .NET library depends on System.Drawing, so you will need this reference in your project as well. In a previous blog post I talked about how to handle the licensing in Using Licensed WinForms controls in WPF. Using the C1Pdf class library we can easily create documents in code from our data. It all starts with an extension method name ToPdf


public static C1PdfDocument ToPdf(this C1DataGrid grid, C1PdfDocument pdf, Thickness pageMargins, float cellsMargins)  

We pass in the instance of C1DataGrid we wish to export, along with some optional margins. From there, the method cycles through and renders each row to the document. It is smart enough to handle column header repetition on each page, as well as rendering grouping headers. Aside from that, any additional required functionality can be extended within the code (style, for instance). Then we call one line of code in our page to start the export process.


PdfUtils.Save(c1DataGrid1.ToPdf());  

The outputted PDF file is text search-able as we are not just rendering images of the grid to the document. The sample code is identical to the Studio for Silverlight sample for the same feature. The one major difference is that instead of using Rect and double types throughout, we use RectangleF and float types because the WinForms version of C1Pdf prefers floats. Download Sample

ComponentOne Product Manager Greg Lutz

Greg Lutz

comments powered by Disqus