Printing PDFs to a Network Printer in C#
This guide explains how to print a PDF document or an image to a network printer with GdPicture.NET SDK.
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 to a network printer, follow these steps:
-
Create a
GdPicturePDF
object. -
Load the source document by passing its file path to the
LoadFromFile
method. -
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. -
Select the printer to use by passing its name to the
PrintSetActivePrinter
method. -
Optional: Configure the printing process programmatically. For more information, see the configuring printing guide.
-
Print the document with the
Print
method. -
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); // Set `printer1` as the active printer. gdpicturePDF.PrintSetActivePrinter("printer1"); // Print the document. gdpicturePDF.Print(); // Release unnecessary resources. gdpicturePDF.CloseDocument();
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() ' Load the source document. gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Store the handle of the active window in a variable. Dim WINDOW_HANDLE = IntPtr.Zero ' Enable the printer settings dialog. gdpicturePDF.PrintShowPrinterSettingsDialog(WINDOW_HANDLE) ' Set `printer1` as the active printer. gdpicturePDF.PrintSetActivePrinter("printer1") ' Print the document. gdpicturePDF.Print() ' Release unnecessary resources. gdpicturePDF.CloseDocument() End Using
Related Topics
Printing an Image
To print an image to a network printer, follow these steps:
-
Create a
GdPictureImaging
object. -
Load the source document by passing its file path to the
CreateGdPictureImageFromFile
method. -
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. -
Select the printer to use by passing its name to the
PrintSetActivePrinter
method. -
Optional: Configure the printing process programmatically. For more information, see the configuring printing guide.
-
Print the image with the
Print
method. -
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); // Set `printer1` as the active printer. gdpictureImaging.PrintSetActivePrinter("printer1"); // Print the image. gdpictureImaging.Print(imageId); // Release unnecessary resources. gdpictureImaging.ReleaseGdPictureImage(imageId);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() ' Load the source image. Dim imageId As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.png") ' Store the handle of the active window in a variable. Dim WINDOW_HANDLE = IntPtr.Zero ' Enable the printer settings dialog. gdpictureImaging.PrintShowPrinterSettingsDialog(WINDOW_HANDLE) ' Set `printer1` as the active printer. gdpictureImaging.PrintSetActivePrinter("printer1") ' Print the image. gdpictureImaging.Print(imageId) ' Release unnecessary resources. gdpictureImaging.ReleaseGdPictureImage(imageId) End Using