Since v4.6, the WinForms edition of ReoGrid exports worksheets straight to PDF. Everything PDF output needs is built into ReoGrid, so no additional library or printer driver is involved.
using unvell.ReoGrid.PDF;
Export a worksheet
PDFExporter.Export(grid.CurrentWorksheet, @"C:\Reports\sales.pdf");
Export a whole workbook
Pass several worksheets to put them into one PDF, one after another:
// every worksheet of the control
PDFExporter.Export(grid.Worksheets, @"C:\Reports\book.pdf");
// selected worksheets, in the order given
PDFExporter.Export(new[] { grid.Worksheets["Sheet1"], grid.Worksheets["Sheet3"] }, @"C:\Reports\summary.pdf");
A worksheet with nothing to print adds no pages.
Export to a stream
using (var ms = new MemoryStream())
{
PDFExporter.Export(grid.CurrentWorksheet, ms);
byte[] pdf = ms.ToArray(); // e.g. for an email attachment
}
PDFExporter methods
| Method | Description |
|---|---|
Export(Worksheet sheet, string filePath) | Export one worksheet to a file |
Export(Worksheet sheet, Stream stream) | Export one worksheet to a stream |
Export(IEnumerable<Worksheet> sheets, string filePath) | Export several worksheets to one file |
Export(IEnumerable<Worksheet> sheets, Stream stream) | Export several worksheets to one stream |
Page layout follows the print settings
A PDF is laid out exactly as the worksheet prints: each printed page becomes one PDF page. Everything set up for printing applies —
- the printable range and page breaks
- paper size, orientation and margins (
PrintSettings.PaperName,Landscape,Margins) - page scaling, or fit to a number of pages (
PageScaling,FitToPage) - page order and centering on the page (
PageOrder,CenterHorizontally,CenterVertically) - the header and footer (
Worksheet.HeaderFooter)
var sheet = grid.CurrentWorksheet;
sheet.PrintSettings.PaperName = "A4";
sheet.PrintSettings.Landscape = true;
sheet.PrintSettings.FitToPage = true; // one page wide,
sheet.PrintSettings.FitToPagesWide = 1;
sheet.PrintSettings.FitToPagesTall = 0; // as many pages tall as it takes
sheet.HeaderFooter.OddFooter = "&CPage &P of &N";
// recalculate the page breaks for the new settings
sheet.AutoSplitPage();
PDFExporter.Export(sheet, @"C:\Reports\sales.pdf");
If a worksheet has not been paged yet, the export pages it automatically. After changing the print settings of a worksheet that was already paged, call AutoSplitPage() so the page breaks follow. All of these settings are described in Paging and Print.
What the PDF contains
- Cell fills and every border style
- Cell text with its font, size, color and alignment — including rotated text, and wrapped or multi-line text broken with Japanese line-breaking rules (禁則処理)
- Images in cells
- The header and footer of each page
Cells, borders and text are written as vector graphics and real text, so the PDF stays sharp at any zoom, and its text can be searched, selected and copied. Partial transparency is kept.
Fonts
The fonts a worksheet uses are embedded, cut down to the glyphs the document draws — a Japanese font adds tens of kilobytes rather than megabytes. Text the cell’s font cannot display falls back to a CJK-capable font, just as it does on screen.
OpenType fonts with PostScript outlines (.otf, CFF) cannot be embedded; text in such a font is written with the default font instead.
License
Like printing, PDF export requires a valid license key. Without one, Export throws InvalidOperationException. See Set the license key.
PDF on a server
PDFExporter is part of the WinForms edition and runs in a desktop application. To produce PDFs without a UI — in an ASP.NET Core service, a batch job or a Linux container — use ReoGrid One V5, whose headless core exports PDF on Windows, Linux and macOS. The same license covers both.