A typical export task for a WinForms grid control is saving it to a PDF file. You can implement this easily for iGrid.NET with the PrintManager add-on if you have a virtual printer allowing you to save output to PDF. If you work in Windows 10 or Windows 11, they come with the preinstalled ‘Microsoft Print to PDF’ virtual printer you can use for this purpose. If you don’t have such a printer in your OS, there are a bunch of free virtual PDF printers you can install, such as Foxit PDF Printer or Sumatra PDF Printer.
Well, let’s start. The simplest code to save iGrid to PDF is the following:
iGPrintManager1.Document.PrinterSettings.PrinterName = "Microsoft Print to PDF";
iGPrintManager1.Print(iGrid1);
All what we need is just to redirect the output of the PrintManager from the default printer to the virtual PDF printer. This is done with the
PrinterName property of the
PrinterSettings object property of our printable document, i.e. the Document property of PrintManager.
If we launch this piece of code, we will see a Save Print Output As dialog prompting for a file name to save. If you allow your users to enter the file name to save, that’s ok, but what if you want to save the grid to a file with a predefined name without showing any dialogs? Two more properties of the PrinterSetting class, PrintToFile and PrintFileName, will help us:
iGPrintManager1.Document.PrinterSettings.PrinterName = "Microsoft Print to PDF";
iGPrintManager1.Document.PrinterSettings.PrintToFile = true;
iGPrintManager1.Document.PrinterSettings.PrintFileName = @"C:\Users\igor\Documents\iGrid.pdf";
iGPrintManager1.Print(iGrid1);
Pay attention to the fact that the PrintFileName property is ignored unless you set the PrintToFile property to True.
If you want to make this code more robust in real-world apps, you may think about checking whether the PDF printer is available before printing. The static
PrinterSettings.InstalledPrinters collection contains the names of currently available printers and can be used for that. An even better version of our code might look like this:
string printerName = "Microsoft Print to PDF";
IEnumerable<string> printerList = PrinterSettings.InstalledPrinters.Cast<string>();
bool printerExists = printerList.Any(name => name.Equals(printerName, StringComparison.OrdinalIgnoreCase));
if (!printerExists)
{
MessageBox.Show($"Printer '{printerName}' does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
PrinterSettings printerSettings = iGPrintManager1.Document.PrinterSettings;
printerSettings.PrinterName = printerName;
printerSettings.PrintToFile = true;
printerSettings.PrintFileName = @"C:\Users\igor\Documents\iGrid.pdf";
iGPrintManager1.Print(iGrid1);
This code uses .NET printing classes, LINQ and generics, so the ‘using’ section at the top of your code module must have the following statements:
using System.Drawing.Printing;
using System.Collections.Generic;
using System.Linq;
As the final words, note that this technique can theoretically work with any virtual printers saving to other formats – such as XPS. At least, it does not fail for another standard Windows printer named ‘Microsoft XPS Document Writer’.