Przejdź do treści stopki
KORZYSTANIE Z IRONPRINT

Jak wydrukować plik PDF bez okna dialogowego w języku C#

This article explores the significance of direct PDF printing and demonstrates how IronPDF, a robust C# library, can facilitate this process.

IronPDF - .NET Framework PDF Library

IronPDF is a powerful C# library designed to facilitate the creation, manipulation, and interaction with PDF documents. With IronPDF, developers can effortlessly generate PDFs from HTML content, images, and other sources, making it a versatile tool for both simple and complex PDF-related tasks. Its capabilities extend beyond mere PDF document generation; IronPDF also empowers you to merge, split, and manipulate PDFs in various ways. Moreover, its intuitive API makes it accessible to both beginners and experienced developers. Niektóre z ważnych funkcji to:

The Importance of Direct PDF Printing

When working with PDFs, printing is a fundamental requirement. Printing PDFs directly from a C# application without presenting a print dialog offers several advantages. It enhances user experience by eliminating unnecessary interaction, automates printing tasks, and enables seamless integration into larger workflows. IronPDF streamlines this process, allowing developers to maintain control over the printing process while ensuring the integrity of the printed output.

Wymagania wstępne

Before moving on to the PDF printing process, ensure that you have the following prerequisites in place:

  1. Visual Studio: It is an integrated development environment (IDE) where you can create, edit, and compile your C# projects. To download and install it on your computer, visit the official Visual Studio website.
  2. IronPDF: The IronPDF library, which can be easily integrated into your project using NuGet Package Manager.

Utwórz projekt Visual Studio

Tworzenie projektu konsoli w Visual Studio jest prostym procesem. Wykonaj poniższe kroki, aby utworzyć nową aplikację konsolową za pomocą programu Visual Studio:

  1. Otwórz Visual Studio: Uruchom środowisko IDE Visual Studio.
  2. Utwórz nowy projekt: Po uruchomieniu programu Visual Studio kliknij "Utwórz nowy projekt".
  3. Wybierz szablon projektu: W oknie "Utwórz nowy projekt" zobaczysz listę szablonów projektów. Wybierz aplikację konsolową C#.

    How to Print PDF File Without Dialog in C#, Figure 1: Create a new C# Console App project in Visual Studio Create a new C# Console App project in Visual Studio

  4. Skonfiguruj szczegóły projektu: Po wybraniu szablonu pojawi się monit o skonfigurowanie szczegółów projektu.

    How to Print PDF File Without Dialog in C#, Figure 2: Configure your new project Configure your new project

  5. Configure Additional Settings: Choose the .NET Framework that has long-term support. IronPDF supports the latest version of .NET Framework.
  6. Utwórz projekt: Po skonfigurowaniu szczegółów projektu kliknij przycisk Utwórz. Visual Studio utworzy projekt i otworzy go w środowisku IDE.

Instalacja IronPDF za pośrednictwem NuGet

You can refer to the following steps to install IronPDF in your project:

  1. Otwórz program Visual Studio i swój projekt.
  2. Przejdź do menu "Narzędzia" i wybierz "Menedżer pakietów NuGet", a następnie kliknij "Zarządzaj pakietami NuGet dla rozwiązania".

    How to Print PDF File Without Dialog in C#, Figure 3: Navigate to NuGet Package Manager Przejdź do menedżera pakietów NuGet

  3. W zakładce "Przeglądaj" wpisz "IronPDF" w polu wyszukiwania.

    How to Print PDF File Without Dialog in C#, Figure 4: Search for IronPDF in NuGet Package Manager UI Search for IronPDF in NuGet Package Manager UI

  4. Click on the IronPdf package and select it for your project, then click the "Install" button.

IronPDF for Printing without Dialog Box - Step-by-Step Process

Importing IronPdf Namespace

The code begins by importing the IronPdf and System.Drawing.Printing namespaces, which allow the use of classes and methods from the IronPDF library, and drawing and printing options from the System namespace.

using IronPdf;
using System.Drawing.Printing;
using IronPdf;
using System.Drawing.Printing;
Imports IronPdf
Imports System.Drawing.Printing
$vbLabelText   $csharpLabel

Creating PDF Document with ChromePdfRenderer

The ChromePdfRenderer is responsible for rendering the web page. The PdfDocument class represents the PDF document and provides methods to perform various operations on it, including printing. The following code generates a PDF file from the IronPDF website URL (https://ironpdf.com/):

// Initialize the ChromePdfRenderer which is used to convert URLs to PDFs
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a PdfDocument by rendering a specified URL as a PDF
PdfDocument pdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/");
// Initialize the ChromePdfRenderer which is used to convert URLs to PDFs
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a PdfDocument by rendering a specified URL as a PDF
PdfDocument pdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/");
' Initialize the ChromePdfRenderer which is used to convert URLs to PDFs
Dim renderer As New ChromePdfRenderer()

' Create a PdfDocument by rendering a specified URL as a PDF
Dim pdfDocument As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/")
$vbLabelText   $csharpLabel

In the above code sample, the first line initializes an instance of ChromePdfRenderer from the IronPDF library, responsible for converting web pages to PDF format. The second line uses this instance to create a PdfDocument by rendering content from a specified URL, enabling further PDF-related actions.

Print PDF Files without Dialog

// Initiate the printing process for the PdfDocument
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf");
// Initiate the printing process for the PdfDocument
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf");
' Initiate the printing process for the PdfDocument
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf")
$vbLabelText   $csharpLabel

The above line of code initiates the printing process for the PdfDocument using the specified print resolution (DPI), sending it to the "Microsoft Print to PDF" printer, which is normally the default printer if no printer is installed, and saving the printed output as a PDF file named "printedfile1.pdf" in the files directory. You can change the printer name and file location as per your needs.

The PDF file is printed pixel-perfect:

How to Print PDF File Without Dialog in C#, Figure 5: Output image of the PDF file named printedfile1.pdf Output image of the PDF file named "printedfile1.pdf"

Silent Printing with Advanced Printing Options

To have more control over printing PDF files programmatically, have a look at the following code snippet with advanced options:

using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Set the printer name to "Microsoft Print to PDF"
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    // Specify the file name for the printed document
    printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf";
    // Enable printing to file
    printDocument.PrinterSettings.PrintToFile = true;
    // Set custom printer resolution
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };

    // Initiate the print process
    printDocument.Print();
}
using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Set the printer name to "Microsoft Print to PDF"
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    // Specify the file name for the printed document
    printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf";
    // Enable printing to file
    printDocument.PrinterSettings.PrintToFile = true;
    // Set custom printer resolution
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };

    // Initiate the print process
    printDocument.Print();
}
Using printDocument = pdfDocument.GetPrintDocument()
	' Set the printer name to "Microsoft Print to PDF"
	printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
	' Specify the file name for the printed document
	printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf"
	' Enable printing to file
	printDocument.PrinterSettings.PrintToFile = True
	' Set custom printer resolution
	printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
		.Kind = PrinterResolutionKind.Custom,
		.X = 1200,
		.Y = 1200
	}

	' Initiate the print process
	printDocument.Print()
End Using
$vbLabelText   $csharpLabel

In the above code snippet, IronPDF facilitates advanced PDF printing customization. It generates a PrintDocument from a PdfDocument, allowing customization of printer settings, printer name, output file name, and resolution. Setting the PrintFileName is important, as it allows you to bypass the print dialog for printing. The code then triggers printing using the print method, directing the print PDF document content to a specified printer, i.e., "Microsoft Print to PDF" in this case. Finally, save the output as a PDF file. This enables seamless, dialog-free PDF printing with tailored settings.

You can also specify a page range to print a PDF document without a printer dialog and Adobe Acrobat Reader, all by incorporating IronPDF into your project. For more detailed information on printing, visit the code examples page.

Wynik

After executing the project, the two output printed PDF files are generated in the specified folder without any user interaction. You can also compare the size difference with the advanced options used.

How to Print PDF File Without Dialog in C#, Figure 6: Image displaying the two printed PDF files printedfile1.pdf and printedfile2.pdf Image displaying the two printed PDF files "printedfile1.pdf" and "printedfile2.pdf"

Wnioski

Printing a PDF file directly from a C# application simplifies document processing and enhances the user experience. IronPDF, with its array of PDF manipulation tools, empowers developers to print PDF documents seamlessly. By integrating IronPDF into your C# project, you can take full advantage of its features.

IronPDF is a commercial library renowned for its robust PDF handling capabilities. It offers a free trial period, enabling users to test and experience its features. Following the trial, a license can be acquired for continued usage. To get started, you can download the product from the official IronPDF website.

Często Zadawane Pytania

Jak mogę wydrukować plik PDF bezpośrednio z aplikacji C# bez okna dialogowego?

Możesz użyć API IronPDF do drukowania pliku PDF bezpośrednio z aplikacji C# bez okna dialogowego. Zarządzając programowo ustawieniami drukarki, możesz wysłać plik PDF do drukarki takiej jak „Microsoft Print to PDF” i zautomatyzować proces drukowania.

Jakie są zalety korzystania z IronPDF do drukowania plików PDF w języku C#?

IronPDF poprawia komfort użytkowania poprzez automatyzację zadań drukowania plików PDF bez interakcji użytkownika, płynną integrację z procesami roboczymi oraz zapewnienie zaawansowanych opcji, takich jak niestandardowe ustawienia drukarki i specyfikacje plików wyjściowych.

Jak skonfigurować projekt Visual Studio, aby drukować pliki PDF przy użyciu IronPDF?

Aby skonfigurować projekt Visual Studio, otwórz Visual Studio, utwórz nową aplikację konsolową, wybierz odpowiedni szablon projektu i zainstaluj IronPDF za pomocą menedżera pakietów NuGet, aby włączyć funkcje drukowania do formatu PDF.

Czy IronPDF może drukować określone strony pliku PDF bez interwencji użytkownika?

Tak, IronPDF pozwala określić zakres stron do wydruku bez konieczności interwencji użytkownika lub okna dialogowego drukarki, co czyni go idealnym rozwiązaniem do zadań zautomatyzowanych.

Jak wygląda proces generowania pliku PDF z adresu URL za pomocą IronPDF?

IronPDF udostępnia metody generowania plików PDF na podstawie adresów URL stron internetowych, które można następnie wydrukować bezpośrednio na określonej drukarce. Umożliwia to automatyczną konwersję i drukowanie treści internetowych.

Jak mogę dostosować ustawienia drukowania za pomocą IronPDF?

IronPDF oferuje zaawansowane opcje dostosowywania ustawień drukowania, w tym definiowanie rozdzielczości drukarki i określanie nazw plików wyjściowych, co zapewnia większą kontrolę nad procesem drukowania.

Jakie platformy są kompatybilne z biblioteką IronPrint, biblioteką drukowania .NET firmy Iron Software?

IronPrint obsługuje wiele platform, w tym Windows, macOS, Android i iOS, co pozwala na wszechstronne wykorzystanie w różnych środowiskach.

Jak zainstalować IronPDF w moim projekcie C#?

Możesz zainstalować IronPDF w swoim projekcie C# za pomocą menedżera pakietów NuGet w Visual Studio. Po prostu wyszukaj IronPDF w zakładce „Przeglądaj” i dodaj go do swojego projektu.

Jakie funkcje oferuje IronPDF do edycji plików PDF w języku C#?

IronPDF zapewnia rozbudowane funkcje tworzenia, edycji i interakcji z plikami PDF, w tym scalanie, dzielenie, szyfrowanie plików PDF oraz drukowanie bezpośrednio z aplikacji C#.

Jakie opcje licencyjne są dostępne dla IronPDF?

IronPDF oferuje programistom bezpłatną wersję próbną do oceny swoich funkcji, a także różne opcje licencyjne do dalszego użytkowania w środowiskach produkcyjnych.

Curtis Chau
Autor tekstów technicznych

Curtis Chau posiada tytuł licencjata z informatyki (Uniwersytet Carleton) i specjalizuje się w front-endowym rozwoju, z ekspertką w Node.js, TypeScript, JavaScript i React. Pasjonuje się tworzeniem intuicyjnych i estetycznie przyjemnych interfejsów użytkownika, Curtis cieszy się pracą z nowoczesnymi frameworkami i tworzeniem dobrze zorganizowanych, atrakcyjnych wizualnie podrę...

Czytaj więcej

Zespol wsparcia Iron

Jestesmy online 24 godziny, 5 dni w tygodniu.
Czat
Email
Zadzwon do mnie