DEV Community

IronSoftware
IronSoftware

Posted on

Print PDF Programmatically in C# (.NET Guide)

Our kiosk application needed to print receipts without user interaction. No print dialog. No manual printer selection. Just generate PDF and print silently.

Programmatic printing solved this. Here's how to send PDFs to printers from C#.

How Do I Print a PDF to the Default Printer?

Use the Print() method:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

var pdf = PdfDocument.FromFile("receipt.pdf");

pdf.Print();
Enter fullscreen mode Exit fullscreen mode

Sends the PDF to your default printer immediately.

Why Print PDFs Programmatically?

Automation: Kiosks, point-of-sale systems, batch printing
Silent printing: No user dialogs or confirmations
Consistency: Same output every time
Integration: Print from web apps, APIs, services

I use this for automated label printing in warehousing systems.

Can I Specify a Printer?

Yes, use PrinterSettings:

using System.Drawing.Printing;

var pdf = PdfDocument.FromFile("invoice.pdf");

pdf.Print(new PrinterSettings
{
    PrinterName = "HP LaserJet"
});
Enter fullscreen mode Exit fullscreen mode

Prints to the specified printer.

How Do I Print Specific Pages?

Set page range in PrinterSettings:

var settings = new PrinterSettings
{
    PrintRange = PrintRange.SomePages,
    FromPage = 1,
    ToPage = 5
};

pdf.Print(settings);
Enter fullscreen mode Exit fullscreen mode

Only pages 1-5 print.

Can I Print Multiple Copies?

Yes, set Copies:

var settings = new PrinterSettings
{
    Copies = 3,
    Collate = true
};

pdf.Print(settings);
Enter fullscreen mode Exit fullscreen mode

Prints 3 collated copies.

How Do I Print Double-Sided?

Enable duplex printing:

var settings = new PrinterSettings
{
    Duplex = Duplex.Vertical // Or Duplex.Horizontal
};

pdf.Print(settings);
Enter fullscreen mode Exit fullscreen mode

Requires a duplex-capable printer.

What About Print Quality?

Set resolution using GetPrintDocument():

using var printDoc = pdf.GetPrintDocument(new PrinterSettings());

printDoc.PrinterSettings.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
    Kind = PrinterResolutionKind.High
};

printDoc.Print();
Enter fullscreen mode Exit fullscreen mode

Higher DPI produces better quality.

Can I Print Silently?

Yes, programmatic printing doesn't show dialogs by default:

pdf.Print(); // Silent, no UI
Enter fullscreen mode Exit fullscreen mode

Perfect for automated workflows.

How Do I Handle Print Errors?

Wrap in try-catch:

try
{
    pdf.Print();
}
catch (System.Drawing.Printing.InvalidPrinterException ex)
{
    Console.WriteLine($"Printer not found: {ex.Message}");
}
catch (Exception ex)
{
    Console.WriteLine($"Print failed: {ex.Message}");
}
Enter fullscreen mode Exit fullscreen mode

Common errors: printer offline, out of paper, access denied.

Can I Print from ASP.NET?

Printing from web apps requires network printers or printer services. The server needs access to printers:

// In ASP.NET controller
var pdf = renderer.RenderHtmlAsPdf("<h1>Report</h1>");

pdf.Print(new PrinterSettings
{
    PrinterName = @"\\PRINTSERVER\Printer1"
});
Enter fullscreen mode Exit fullscreen mode

Use UNC paths for network printers.

How Do I List Available Printers?

using System.Drawing.Printing;

foreach (string printer in PrinterSettings.InstalledPrinters)
{
    Console.WriteLine(printer);
}
Enter fullscreen mode Exit fullscreen mode

Shows all printers installed on the system.

Can I Print to PDF (Save as PDF)?

Use Print To PDF virtual printer:

pdf.Print(new PrinterSettings
{
    PrinterName = "Microsoft Print to PDF",
    PrintToFile = true,
    PrintFileName = "output.pdf"
});
Enter fullscreen mode Exit fullscreen mode

Or just use SaveAs() directly:

pdf.SaveAs("output.pdf");
Enter fullscreen mode Exit fullscreen mode

What If the Printer Is Unavailable?

Check printer status first:

var settings = new PrinterSettings { PrinterName = "HP LaserJet" };

if (settings.IsValid)
{
    pdf.Print(settings);
}
else
{
    Console.WriteLine("Printer not available");
}
Enter fullscreen mode Exit fullscreen mode

How Do I Print Labels?

Specify custom paper size:

var settings = new PrinterSettings();
settings.DefaultPageSettings.PaperSize = new PaperSize("Label", 400, 600);

pdf.Print(settings);
Enter fullscreen mode Exit fullscreen mode

Paper size in hundredths of an inch (400 = 4 inches).

Can I Batch Print Multiple PDFs?

Yes, loop through files:

var files = Directory.GetFiles("invoices", "*.pdf");

foreach (var file in files)
{
    var pdf = PdfDocument.FromFile(file);
    pdf.Print();
}
Enter fullscreen mode Exit fullscreen mode

Prints all PDFs in sequence.

What's the Performance?

Print job submission: ~100-200ms per PDF
Actual printing: Depends on printer speed

For high-volume printing, submit jobs asynchronously:

var tasks = pdfs.Select(async pdf =>
{
    await Task.Run(() => pdf.Print());
});

await Task.WhenAll(tasks);
Enter fullscreen mode Exit fullscreen mode

How Do I Print Without Margins?

Set margins to zero:

var printDoc = pdf.GetPrintDocument(new PrinterSettings());

printDoc.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);

printDoc.Print();
Enter fullscreen mode Exit fullscreen mode

Useful for full-bleed labels or photos.

Can I Monitor Print Status?

Subscribe to print events:

var printDoc = pdf.GetPrintDocument(new PrinterSettings());

printDoc.BeginPrint += (sender, e) => Console.WriteLine("Starting print...");
printDoc.EndPrint += (sender, e) => Console.WriteLine("Print complete");

printDoc.Print();
Enter fullscreen mode Exit fullscreen mode

Track job progress programmatically.


Written by Jacob Mellor, CTO at Iron Software. Jacob created IronPDF and leads a team of 50+ engineers building .NET document processing libraries.

Top comments (0)