Printing PDFs programmatically in C#

This guide explains how to print a PDF document or an image programmatically with Nutrient .NET SDK (formerly GdPicture.NET).

Printing and scanning aren’t supported in the cross-platform .NET 6.0 assembly. For more information, see the system compatibility guide.

Printing a PDF document

To print a PDF document programmatically, follow these steps:

  1. Create a GdPicturePDF object.
  2. Load the source document by passing its file path to the LoadFromFile method.
  1. Optional: Enable the printer settings dialog by storing the handle of the active windows in a variable and then passing this variable to the PrintShowPrinterSettingsDialog method. To print silently, skip this step.
  1. Optional: Configure the printing process programmatically. For more information, see the configuring printing guide.
  2. Print the document with the Print method.
  3. Release unnecessary resources.
using GdPicturePDF gdpicturePDF = new GdPicturePDF();
// Load the source document.
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Store the handle of the active window in a variable.
IntPtr WINDOW_HANDLE = IntPtr.Zero;
// Enable the printer settings dialog.
gdpicturePDF.PrintShowPrinterSettingsDialog(WINDOW_HANDLE);
// Print the document.
gdpicturePDF.Print();
// Release unnecessary resources.
gdpicturePDF.CloseDocument();

Printing an image

To print an image programmatically, follow these steps:

  1. Create a GdPictureImaging object.
  2. Load the source document by passing its file path to the CreateGdPictureImageFromFile method.
  1. Optional: Enable the printer settings dialog by storing the handle of the active windows in a variable and then passing this variable to the PrintShowPrinterSettingsDialog method. To print silently, skip this step.
  1. Optional: Configure the printing process programmatically. For more information, see the configuring printing guide.
  2. Print the image with the Print method.
  3. Release unnecessary resources.
using GdPictureImaging gdpictureImaging = new GdPictureImaging();
// Load the source image.
int imageId = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.png");
// Store the handle of the active window in a variable.
IntPtr WINDOW_HANDLE = IntPtr.Zero;
// Enable the printer settings dialog.
gdpictureImaging.PrintShowPrinterSettingsDialog(WINDOW_HANDLE);
// Print the image.
gdpictureImaging.Print(imageId);
// Release unnecessary resources.
gdpictureImaging.ReleaseGdPictureImage(imageId);