Jak drukować pliki PDF w języku C# w trybie cichym
PDF document is a Portable Document Format which can store and transfer data in the requested format. It preserves the formatting of the data stored and allows its users to focus on other aspects of handling digital documents. Printing digital documents is a tedious task as data displays differently on different platforms. But sending data in PDF documents helps preserve the format for printing. However, printing PDF files programmatically can be a challenge for developers in C#. Thanks to IronPDF - C# PDF Library, it makes the process of printing PDF files extremely easy and hassle-free.
This article will explain how to silently print PDF documents in C# using the IronPDF Library.
How to Print PDF File Silently in C#
- Install C# library to print PDF file silently
- Utilize
Printmethod to print PDF document with default settings - Set
PrinterNameproperty to target the specific printer - Set custom resolution with
PrinterResolutionclass in C# - Wydrukuj plik PDF w tle do określonego katalogu bez otwierania okna dialogowego drukarki
IronPDF - C# PDF Library
IronPDF is a C# .NET Library, which allows developers to create, read, and edit PDF documents. It is a top notch C# Library and prioritizes accuracy, ease of use, and speed. It is specially designed for C#, F#, & VB.NET and highly compatible with .NET 7, 6, 5, Core, Standard, or Framework. It helps generate PDFs from HTML for Web, Desktop, and Console utilizing IronPDF's powerful Chromium engine.
Additionally, IronPDF allows the user to manipulate and edit PDFs, add headers and footers, extract text and images from PDFs with ease.
Some Important Features include:
- Load and Generate PDF files from different file formats
- Save and Print PDF files using Default printer
- Merge and Splitting PDF file
- PDF Editor without Adobe Reader
Steps to Silently Print PDF Documents in C# using IronPDF Library
To silently print PDF documents, first we need the following components to be installed on the local computer.
- Visual Studio - It is the official IDE for C# development and it must be installed on the computer. You can download and install from Visual Studio website.
-
Create Project - To create a console application for PDF printing in C#, follow the steps using Visual Studio 2022:
-
Open Visual Studio and click on create a new project
Visual Studio
-
Select the C# Console App and click on next
New Project Dialog Box
-
Now, type the name of your project, select the Location, and click next
Formularze internetowe
-
Choose the latest .NET Framework for your application. We will use stable version 6.0.
Additional Information
- Click on create, the console project is created and we are ready to print PDF documents programmatically.
-
-
Install IronPDF - There are 3 ways to download and install the IronPDF library. Są to:
-
Using Visual Studio - Visual Studio has NuGet Package Manager which helps to install NuGet packages in C# projects.
- Click on Tools in Menu bar, or
-
Right-click the project file in the Eksplorator rozwiązań
Eksplorator rozwiązań
Project Menu > Manage NuGet Packages
-
Once it is opened, browse IronPDF in NuGet package manager and install it, as shown below:
Install IronPDF from NuGet Package
- Download the NuGet Package directly - Another way to download IronPDF is by navigating to the NuGet website and downloading the package directly. Here is the linkhttps://www.nuget.org/packages/IronPdf/.
- Download the IronPDF .DLL Library - IronPDF can also be downloaded directly from IronPDF website. Navigate to: IronPDF DLL download to install it. Reference the .DLL file in your project to use it.
-
Generate a PDF File and Print Job
Here we will generate a PDF file from a URL. Creating a PDF file is easy and usually a two-step process. The following code sample generates a PDF:
using IronPdf;
// Initialize a new instance of ChromePdfRenderer.
ChromePdfRenderer Renderer = new ChromePdfRenderer();
// Render the specified URL as a PDF document.
PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
using IronPdf;
// Initialize a new instance of ChromePdfRenderer.
ChromePdfRenderer Renderer = new ChromePdfRenderer();
// Render the specified URL as a PDF document.
PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
Imports IronPdf
' Initialize a new instance of ChromePdfRenderer.
Private Renderer As New ChromePdfRenderer()
' Render the specified URL as a PDF document.
Private Pdf As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/")
A PDF document object is created with the above code and it is ready for printing. Next, we will use the default printer to paper print PDF documents. The code is a one-liner and is as follows:
// Print the PDF document using the default printer settings.
Pdf.Print();
// Print the PDF document using the default printer settings.
Pdf.Print();
' Print the PDF document using the default printer settings.
Pdf.Print()
This Print method will send the PDF to the default printer for printing.
Advanced Printing Options
For silent printing, IronPDF provides various advanced printing options.
PdfDocument.GetPrintDocument method is used, and the result is stored in System.Drawing.Printing.PrintDocument object. The code is simple and as following:
// Remember to add assembly reference to System.Drawing.dll in project
// Get the print document for the PDF.
System.Drawing.Printing.PrintDocument PrintPDF = Pdf.GetPrintDocument();
// Remember to add assembly reference to System.Drawing.dll in project
// Get the print document for the PDF.
System.Drawing.Printing.PrintDocument PrintPDF = Pdf.GetPrintDocument();
' Remember to add assembly reference to System.Drawing.dll in project
' Get the print document for the PDF.
Dim PrintPDF As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument()
Specify Printer Name
IronPDF also gives the opportunity to print to a specific printer. To specify the name, the PrinterSettings.PrinterName property is used. First, we need to get the current PDF document object. Przykładowy kod wygląda następująco:
using (var printDocument = pdfDocument.GetPrintDocument())
{
// Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Send the print job to the specified printer.
printDocument.Print();
}
using (var printDocument = pdfDocument.GetPrintDocument())
{
// Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Send the print job to the specified printer.
printDocument.Print();
}
Using printDocument = pdfDocument.GetPrintDocument()
' Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
' Send the print job to the specified printer.
printDocument.Print()
End Using
Set Printer Resolution
Another cool feature is setting the printer resolution. We can control the number of pixels to be printed, displayed, depending on the output. The DefaultPageSettings.PrinterResolution property of the PDF document can be used to set the resolution. Here is a very quick code sample:
// Set the custom resolution for the printer.
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Send the print job with the custom resolution settings.
printDocument.Print();
// Set the custom resolution for the printer.
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Send the print job with the custom resolution settings.
printDocument.Print();
' Set the custom resolution for the printer.
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
.Kind = PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
' Send the print job with the custom resolution settings.
printDocument.Print()
PrintToFile Method
The PdfDocument class provides the PrintToFile method which allows us to print PDF to a file in C#. It takes the pathToFile as an argument to print the file directly to that location without opening the printer dialog. Kod jest prosty i wygląda następująco:
// Print the document to a specified file location.
printDocument.PrintToFile("PathToFile", false);
// Print the document to a specified file location.
printDocument.PrintToFile("PathToFile", false);
' Print the document to a specified file location.
printDocument.PrintToFile("PathToFile", False)
The complete code example goes as follows:
using IronPdf;
// Initialize the PDF renderer and create the PDF document.
ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
using (var printDocument = pdfDocument.GetPrintDocument())
{
// Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Set a custom print resolution.
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Execute the print job.
printDocument.Print();
}
using IronPdf;
// Initialize the PDF renderer and create the PDF document.
ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
using (var printDocument = pdfDocument.GetPrintDocument())
{
// Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Set a custom print resolution.
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Execute the print job.
printDocument.Print();
}
Imports IronPdf
' Initialize the PDF renderer and create the PDF document.
Private Renderer As New ChromePdfRenderer()
Private pdfDocument As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/")
Using printDocument = pdfDocument.GetPrintDocument()
' Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
' Set a custom print resolution.
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
.Kind = PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
' Execute the print job.
printDocument.Print()
End Using
When the code is executed, it converts a URL to a PDF document. Then to silently print a PDF document, the GetPrintDocument method is used. On successful compiling and executing the program files, a printer dialog box appears to save it as a PDF document. The PDF is then saved using the specified printer name.
Podsumowanie
In this article, we closely looked at how to silently print a PDF document using IronPDF. IronPDF provides a lot of useful options while PDF printing. It can also keep track of printed pages and also allows you to print between the page range.
Silent printing along with other printing options makes IronPDF a standout Library in C# while working with PDFs.
IronPDF helps convert data from different formats to PDF and from PDF to different formats. It makes it easy for the developers to integrate PDF functionality in the application development process. Moreover, it does not require Adobe Acrobat Reader to view and edit PDF documents.
IronPDF is free for individual development and can be licensed for commercial use. It provides a free trial license to access and test out the full functionality of the library. You can check more details on this link.
Często Zadawane Pytania
Jak mogę wydrukować plik PDF w tle w języku C#?
W języku C# można wydrukować plik PDF w tle, korzystając z metody PRINT biblioteki IronPDF. Pozwala to na drukowanie bez otwierania okna dialogowego drukarki, zapewniając zachowanie formatowania dokumentu.
Jakie kroki należy wykonać, aby skonfigurować IronPDF do cichego drukowania plików PDF?
Aby skonfigurować IronPDF do cichego drukowania plików PDF, zainstaluj bibliotekę za pomocą menedżera pakietów NuGet lub pobierając plik .DLL. Następnie użyj metody PRINT, aby skonfigurować ustawienia cichego drukowania i określić właściwości drukarki, takie jak PrinterName i PrinterResolution.
Jak zapewnić zachowanie formatowania pliku PDF podczas drukowania?
IronPDF pomaga zachować formatowanie plików PDF, umożliwiając konfigurację określonych ustawień drukarki, takich jak ustawienie właściwości PrinterResolution, co gwarantuje, że wydruk będzie zgodny z oryginalnym dokumentem.
Czy w języku C# mogę wydrukować plik PDF do lokalizacji pliku?
Tak, IronPDF udostępnia metodę PrintToFile, która pozwala na drukowanie pliku PDF bezpośrednio do określonej lokalizacji pliku bez otwierania okna dialogowego drukarki.
Czy można drukować pliki PDF bez użycia programu Adobe Acrobat Reader?
Tak, IronPDF umożliwia drukowanie, przeglądanie i edycję plików PDF bez konieczności korzystania z programu Adobe Acrobat Reader, co czyni go elastycznym narzędziem do obsługi dokumentów PDF w aplikacjach napisanych w języku C#.
Czy IronPDF obsługuje drukowanie plików PDF na różnych platformach?
Tak, IronPDF obsługuje drukowanie plików PDF na różnych platformach, zapewniając kompatybilność z systemami Windows, macOS, Android i iOS, co czyni go wszechstronnym rozwiązaniem dla różnych środowisk programistycznych.
Jakie są dostępne opcje wersji próbnych IronPDF?
IronPDF jest bezpłatny dla indywidualnych programistów i oferuje Licencję Trial, która pozwala przetestować pełną funkcjonalność przed zakupem licencji komercyjnej.
Jak mogę określić konkretną drukarkę do drukowania plików PDF za pomocą IronPDF?
Można określić konkretną drukarkę, ustawiając właściwość PrinterSettings.PrinterName w IronPDF, kierując zadanie drukowania do wybranej drukarki.
Jakie środowisko IDE jest zalecane do tworzenia aplikacji w języku C# z wykorzystaniem IronPDF?
Visual Studio to zalecane zintegrowane środowisko programistyczne (IDE) do tworzenia aplikacji w języku C# z wykorzystaniem IronPDF, zapewniające solidną platformę do kodowania i zarządzania projektami.
Jak mogę rozwiązać problemy z cichym drukowaniem plików PDF w języku C#?
Jeśli napotkasz problemy z cichym drukowaniem plików PDF w języku C#, upewnij się, że IronPDF jest poprawnie zainstalowany, a ustawienia drukarki, takie jak PrinterName i PrinterResolution, są prawidłowo skonfigurowane. Pomocne wskazówki dotyczące rozwiązywania problemów można również znaleźć w dokumentacji IronPDF.



