Drukowanie plików PDF w języku C# przy użyciu IronPDF
PDF is the most commonly used file format. It is lightweight and supported by most operating systems. PDF files can be shared easily from one device to another. They provide ease of reading as they appear exactly the same on any interface and OS. PDFs can be accessed from all devices including mobiles, tablets, etc.
Their popularity demands that all C# developers know how to print PDF files using C#. This is because you might come across a problem when you have to print a document such as an invoice, pay slip, order slip or profile, etc. These documents are often in PDF file format, so we must know how to print PDF files using C#.
Printing PDF documents in C# can be done using multiple libraries. But, one issue you may come across is the license fee. Most of them are not free or are difficult to use. But luckily, you do not have to worry. I have found the easiest and simplest way to print PDF files in C#.
I will use the IronPDF Library to print PDF documents. Believe me, you are going to like it!
What will you get from this article?
After reading this article you will be able to perform the following tasks:
- Print single PDF documents in C#
- Print multiple PDF files in one go using C#
- Convert a URL to PDF and then print it.
You must have basic knowledge of C# programming for a better understanding of this tutorial.
Before starting the tutorial, let's first understand what IronPDF is.
IronPDF:
IronPDF is a .NET Library that provides us the simplest way to read, create, manipulate and print PDF files. This library can be used in C# and VB .NET applications. It works easily with ASP .NET, ASP .NET core, MVC, Web Forms, and Web APIs on both .NET and .NET core. It allows users to download PDF files, send them by email and store them in a cloud.
You can explore more about IronPDF using this link.
Let's start the demonstration.
Open Visual Studio
Otwórz program Visual Studio. Create a new project or open an existing project if you are going to add this functionality to your existing software.
I am using Konsola Application for this tutorial; you can use any of your choices according to your software requirements.
Project Created.
Install NuGet Package
Open the console by clicking on Tools > NuGet Package Manager > Package Manager Konsola from the Menu bar on the top of the window.
Konsola menedżera pakietów NuGet will open as shown below:
Konsola menedżera pakietów NuGet
Write the following command in the console to Install IronPdf NuGet Package and press Enter.
Install-Package IronPrint
NuGet Package will be installed as shown below:
Install NuGet Package
Print PDF Filed in C
Now, let us consider an example to demonstrate how we can print a PDF file in C# using the IronPDF Library.
Open Program.cs file. Add the following namespace at the top of the Program.cs file to use IronPDF:
using IronPdf;
using IronPdf;
Imports IronPdf
Now, write the following code inside the Main function:
using System; // Required for Konsola operations
class Program
{
static void Main()
{
// Load the PDF document from file
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
// Print the loaded PDF document to the default printer
pdfDocument.Print();
// Print operation success message to console
Konsola.WriteLine("File Printed Successfully!");
Konsola.ReadKey();
}
}
using System; // Required for Konsola operations
class Program
{
static void Main()
{
// Load the PDF document from file
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
// Print the loaded PDF document to the default printer
pdfDocument.Print();
// Print operation success message to console
Konsola.WriteLine("File Printed Successfully!");
Konsola.ReadKey();
}
}
Imports System ' Required for Konsola operations
Class Program
Shared Sub Main()
' Load the PDF document from file
Using pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
' Print the loaded PDF document to the default printer
pdfDocument.Print()
End Using
' Print operation success message to console
Konsola.WriteLine("File Printed Successfully!")
Konsola.ReadKey()
End Sub
End Class
Let's understand the code.
- The following line of code will load the PDF file into memory. "Sample.Pdf" is the file name and D Drive is my file location. You have to write a complete file path along with the file name.
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
Using pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
End Using
- The
Print()function will print the loaded file using your default printer.
Test the Program
Press Ctrl + F5 to run the program.
Konsola
Print Multiple PDF Documents in C
I will show you how to print multiple PDF documents in C# using IronPDF.
14 PDF files
In this example, I have 14 PDF documents which I need to print. I have named them as numbers 1 - 14. You can save all your files with an ID, or you can save the file location in a database. Extract the file Path and Filename from the database, and save them in an array or on a list. Iterate that array or list and get all your files printed.
Write the following code inside the Main function:
using System; // Required for Konsola operations
class Program
{
static void Main()
{
// Loop through each file number and print
for (int i = 1; i <= 14; i++)
{
// Load each PDF document from file
using PdfDocument pdfDocument = PdfDocument.FromFile($@"D:\Print\{i}.pdf");
// Inform which file is being printed
Konsola.WriteLine("Printing File: {0}.pdf", i);
// Print the loaded PDF document to the default printer
pdfDocument.Print();
// Print operation success message to console
Konsola.WriteLine("{0}.pdf Printed Successfully!", i);
}
Konsola.ReadKey();
}
}
using System; // Required for Konsola operations
class Program
{
static void Main()
{
// Loop through each file number and print
for (int i = 1; i <= 14; i++)
{
// Load each PDF document from file
using PdfDocument pdfDocument = PdfDocument.FromFile($@"D:\Print\{i}.pdf");
// Inform which file is being printed
Konsola.WriteLine("Printing File: {0}.pdf", i);
// Print the loaded PDF document to the default printer
pdfDocument.Print();
// Print operation success message to console
Konsola.WriteLine("{0}.pdf Printed Successfully!", i);
}
Konsola.ReadKey();
}
}
Imports System ' Required for Konsola operations
Class Program
Shared Sub Main()
' Loop through each file number and print
For i As Integer = 1 To 14
' Load each PDF document from file
Using pdfDocument As PdfDocument = PdfDocument.FromFile($"D:\Print\{i}.pdf")
' Inform which file is being printed
Konsola.WriteLine("Printing File: {0}.pdf", i)
' Print the loaded PDF document to the default printer
pdfDocument.Print()
' Print operation success message to console
Konsola.WriteLine("{0}.pdf Printed Successfully!", i)
End Using
Next
Konsola.ReadKey()
End Sub
End Class
I have used a for loop to generate Numbers from 1 to 14 according to my file name.
Run the Solution:
Press Ctrl + F5 to run the program.
Konsola
Convert URL to PDF then Print it:
I will show you how to print a web page by parsing URLs to IronPDF. The library will create a PDF file by parsing HTML. We can print that PDF document using the print function. Let's consider an example.
Write the following code inside the main function:
using System; // Required for Konsola operations
class Program
{
static void Main()
{
// HTML to PDF Renderer
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
// Inform about the printing operation
Konsola.WriteLine("Printing File");
// Render URL as PDF
using PdfDocument PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/print-pdf/#advanced-printing");
// Print the PDF document
PdfDocument.Print();
// Print operation success message to console
Konsola.WriteLine("File Printed Successfully!");
}
}
using System; // Required for Konsola operations
class Program
{
static void Main()
{
// HTML to PDF Renderer
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
// Inform about the printing operation
Konsola.WriteLine("Printing File");
// Render URL as PDF
using PdfDocument PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/print-pdf/#advanced-printing");
// Print the PDF document
PdfDocument.Print();
// Print operation success message to console
Konsola.WriteLine("File Printed Successfully!");
}
}
Imports System ' Required for Konsola operations
Class Program
Shared Sub Main()
' HTML to PDF Renderer
Dim Renderer As New IronPdf.HtmlToPdf()
' Inform about the printing operation
Konsola.WriteLine("Printing File")
' Render URL as PDF
Using PdfDocument As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/print-pdf/#advanced-printing")
' Print the PDF document
PdfDocument.Print()
End Using
' Print operation success message to console
Konsola.WriteLine("File Printed Successfully!")
End Sub
End Class
This program will parse https://ironpdf.com/how-to/print-pdf/#advanced-printing to PDF and the Print() function will print the PDF using the default printer.
Let's run the program.
Run the Program:
Press Ctrl + F5 to run the solution.
Output displayed on screen
The Print() function will print the document using the default printer. There may be a situation where we do not want to use the default printer. In that case, IronPDF provides us with one line code to change the printer name.
Change Printer Name:
Let's use an example to help us understand further. Use the following code to change the default printer:
using System; // Required for Konsola operations
class Program
{
static void Main()
{
// Load the PDF document from file
PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
// Change to a different printer by name
pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Print the PDF document
pdfDocument.Print();
// Print operation success message to console
Konsola.WriteLine("File Printed Successfully!");
}
}
using System; // Required for Konsola operations
class Program
{
static void Main()
{
// Load the PDF document from file
PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
// Change to a different printer by name
pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Print the PDF document
pdfDocument.Print();
// Print operation success message to console
Konsola.WriteLine("File Printed Successfully!");
}
}
Imports System ' Required for Konsola operations
Class Program
Shared Sub Main()
' Load the PDF document from file
Dim pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
' Change to a different printer by name
pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF"
' Print the PDF document
pdfDocument.Print()
' Print operation success message to console
Konsola.WriteLine("File Printed Successfully!")
End Sub
End Class
You can see that I have added only one line of additional code to specify the printer. I have specified Microsoft Print to PDF for demonstration.
Run the Program:
Run the Program by pressing Ctrl + F5.
The following Dialogue Box will appear that will ask us to enter a file name to save because we are using Microsoft Print to PDF.
Specify the File name and click on the Save button. I have specified "SamplePrint.Pdf". The following console output will display:
Microsoft Print to PDF has printed a PDF file in my D Drive. Let's see it.
I hope this tutorial was helpful, interactive, and easy for you to understand. You can ask questions or give feedback on this tutorial in the comments section below.
Często Zadawane Pytania
Jak wydrukować plik PDF w C# używając IronPDF?
Aby wydrukować plik PDF używając IronPDF w C#, załaduj dokument PDF za pomocą metody PdfDocument.FromFile, a następnie wywołaj metodę Print na załadowanym dokumencie.
Czy można użyć IronPDF do sekwencyjnego drukowania wielu plików PDF?
Tak, IronPDF może drukować wiele plików PDF iterując przez kolekcję ścieżek do plików i wywołując metodę Print dla każdego dokumentu.
Jak mogę przekonwertować URL na PDF, a następnie wydrukować go w C#?
Używając IronPDF, przekonwertuj URL na PDF za pomocą metody HtmlToPdf.RenderUrlAsPdf. Następnie użyj metody Print na wygenerowanym dokumencie PDF, aby go wydrukować.
Czy możliwe jest wybranie konkretnej drukarki do drukowania plików PDF za pomocą IronPDF?
Tak, możesz wybrać konkretną drukarkę, ustawiając właściwość PrinterName w obiekcie PrinterSettings podczas używania metody GetPrintDocument klasy PdfDocument.
Jakie platformy obsługuje IronPDF do drukowania PDF?
IronPDF obsługuje drukowanie PDF na aplikacjach .NET Framework, .NET Core, oraz .NET 5 i 6, a także jest kompatybilny z platformami Windows, macOS, Android i iOS.
Jak mogę rozpocząć drukowanie PDF w C# używając biblioteki .NET?
Aby rozpocząć drukowanie PDF używając IronPDF, zainstaluj bibliotekę poprzez Menedżera Pakietów NuGet w Visual Studio i postępuj zgodnie z dokumentacją i przykładami dostarczonymi przez Iron Software.
Jakie są zalety używania PDF do współdzielenia dokumentów?
PDF jest korzystny do współdzielenia dokumentów, ponieważ jest lekki, zachowuje spójne formatowanie na różnych urządzeniach i jest obsługiwany przez większość systemów operacyjnych.
Czy potrzebuję zaawansowanych umiejętności C#, aby używać IronPDF do drukowania?
Podstawowa znajomość C# jest wystarczająca do używania IronPDF do zadań drukowania, ponieważ biblioteka dostarcza prostych metod i przykładów, które pomagają programistom.



