C#でPDFドキュメントを印刷

This article was translated from English: Does it need improvement?
Translated
View the article in English

.NETアプリケーションで、Visual BasicまたはC#コードを使用して簡単にPDFを印刷できます。 このチュートリアルでは、C#のPDF印刷機能を使用してプログラム的に印刷する方法を説明します。



PDF 用 C# NuGet ライブラリ

でインストール NuGet

Install-Package IronPdf
または
Java PDF JAR(ジャバPDF JAR)

ダウンロード DLL (ディーエルエル)

DLLをダウンロード

プロジェクトに手動でインストールする

PDF 用 C# NuGet ライブラリ

でインストール NuGet

Install-Package IronPdf
または
Java PDF JAR(ジャバPDF JAR)

ダウンロード DLL (ディーエルエル)

DLLをダウンロード

プロジェクトに手動でインストールする

今日からプロジェクトでIronPDFを使い始めましょう。無料のトライアルをお試しください。

最初のステップ:
green arrow pointer

チェックアウト IronPDF オン Nuget 迅速なインストールと展開のために。8百万以上のダウンロード数により、PDFをC#で変革しています。

PDF 用 C# NuGet ライブラリ nuget.org/packages/IronPdf/
Install-Package IronPdf

インストールを検討してください IronPDF DLL 直接。ダウンロードして、プロジェクトまたはGACの形式で手動でインストールしてください。 IronPdf.zip

プロジェクトに手動でインストールする

DLLをダウンロード

PDFを作成して印刷してください

PDFドキュメントをプリンターにサイレントで直接送信することができます。または、 System.Drawing.Printing.PrintDocument GUI印刷ダイアログに送信して操作できるオブジェクト。

以下のコードは、両方のオプションで使用できます:

:path=/static-assets/pdf/content-code-examples/how-to/csharp-print-pdf-create-and-print-pdf.cs
using IronPdf;
using System.Threading.Tasks;

// Create a new PDF and print it
ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");

// Print PDF from default printer
await pdf.Print();

// For advanced silent real-world printing options, use PdfDocument.GetPrintDocument
// Remember to add an assembly reference to System.Drawing.dll
System.Drawing.Printing.PrintDocument PrintDocYouCanWorkWith = pdf.GetPrintDocument();
Imports IronPdf
Imports System.Threading.Tasks

' Create a new PDF and print it
Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")

' Print PDF from default printer
Await pdf.Print()

' For advanced silent real-world printing options, use PdfDocument.GetPrintDocument
' Remember to add an assembly reference to System.Drawing.dll
Dim PrintDocYouCanWorkWith As System.Drawing.Printing.PrintDocument = pdf.GetPrintDocument()
VB   C#

高度な印刷

IronPDFは、プリンター名の検索や設定、プリンターの解像度の設定など、高度な印刷機能に十分対応できます。

プリンターの名前を指定する

プリンター名を指定するには、現在の印刷ドキュメントオブジェクトを取得するだけです。 (使用して GetPrintDocument (印刷ドキュメントを取得) PDFドキュメントのメソッド)次に、PrinterSettings.PrinterName プロパティを使用します。

:path=/static-assets/pdf/content-code-examples/how-to/csharp-print-pdf-specify-printer-name.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Get PrintDocument object
var printDocument = pdf.GetPrintDocument();

// Assign the printer name
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

// Print document
printDocument.Print();
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Get PrintDocument object
Private printDocument = pdf.GetPrintDocument()

' Assign the printer name
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"

' Print document
printDocument.Print()
VB   C#

プリンターの解像度を設定する

解像度とは、出力に応じて印刷または表示されるピクセルの数を指します。 お客様はIronPDFを使用して印刷の解像度を設定できます。 DefaultPageSettings.PrinterResolution(デフォルトページ設定.プリンター解像度) PDFドキュメントのプロパティ。 こちらは非常に簡単なデモンストレーションです。

:path=/static-assets/pdf/content-code-examples/how-to/csharp-print-pdf-specify-printer-resolution.cs
using IronPdf;
using System.Drawing.Printing;

PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Get PrintDocument object
var printDocument = pdf.GetPrintDocument();

// Set printer resolution
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
    Kind = PrinterResolutionKind.Custom,
    X = 1200,
    Y = 1200
};

// Print document
printDocument.Print();
Imports IronPdf
Imports System.Drawing.Printing

Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Get PrintDocument object
Private printDocument = pdf.GetPrintDocument()

' Set printer resolution
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
	.Kind = PrinterResolutionKind.Custom,
	.X = 1200,
	.Y = 1200
}

' Print document
printDocument.Print()
VB   C#

ご覧の通り、解像度をカスタムレベルに設定しました:垂直1200および水平1200。

ファイルへの印刷メソッド

PdfDocument.PrintToFile メソッドを使用すると、PDFをファイルに印刷できます。出力ファイルパスを指定し、プレビューを表示するかどうかを指定するだけです。 以下のコードは、プレビューを含めずに指定されたファイルに印刷します:

:path=/static-assets/pdf/content-code-examples/how-to/csharp-print-pdf-printtofile.cs
using IronPdf;
using System.Threading.Tasks;

PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

await pdf.PrintToFile("PathToFile", false);
Imports IronPdf
Imports System.Threading.Tasks

Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

Await pdf.PrintToFile("PathToFile", False)
VB   C#

C#で印刷プロセスのトレース

IronPDFとC#の組み合わせの素晴らしさは、印刷されたページや印刷に関連するあらゆるものを追跡する際に、とても簡単であるということです。 次の例では、プリンター名と解像度を変更する方法、および印刷されたページ数を取得する方法を示します。

:path=/static-assets/pdf/content-code-examples/how-to/csharp-print-pdf-trace-printing-process.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Get PrintDocument object
var printDocument = pdf.GetPrintDocument();

// Subscribe to the PrintPage event
var printedPages = 0;
printDocument.PrintPage += (sender, args) => printedPages++;

// Print document
printDocument.Print();
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Get PrintDocument object
Private printDocument = pdf.GetPrintDocument()

' Subscribe to the PrintPage event
Private printedPages = 0
Private printDocument.PrintPage += Sub(sender, args) printedPages++

' Print document
printDocument.Print()
VB   C#