C# IronPDFを使用したPDFファイルの印刷
PDF は最も一般的に使用されるファイル形式です。 軽量で、ほとんどのオペレーティング システムでサポートされています。 PDF ファイルはデバイス間で簡単に共有できます。 どのインターフェースや OS でもまったく同じように表示されるため、読みやすくなります。 PDF には、モバイル、タブレットなど、あらゆるデバイスからアクセスできます。
PDF ファイルは人気があるため、すべての C# 開発者は C# を使用して PDF ファイルを印刷する方法を知っておく必要があります。 これは、請求書、給与明細書、注文書、プロファイルなどのドキュメントを印刷する必要があるときに問題が発生する可能性があるためです。これらのドキュメントは多くの場合 PDF ファイル形式であるため、C# を使用して PDF ファイルを印刷する方法を知っておく必要があります。
C# で PDF ドキュメントを印刷するには、複数のライブラリを使用します。 しかし、遭遇する可能性がある問題の 1 つはライセンス料です。 それらのほとんどは無料ではないか、使いにくいものです。 しかし幸いなことに、心配する必要はありません。 C# で PDF ファイルを印刷する最も簡単でシンプルな方法を見つけました。
I will use the IronPDF Library to print PDF documents. 信じてください、きっと気に入っていただけると思います!
この記事から何が得られますか?
この記事を読んだ後、次のタスクを実行できるようになります。
- C#で単一のPDFドキュメントを印刷する
- C# を使用して複数の PDF ファイルを一度に印刷する
- URL を PDF に変換して印刷します。
このチュートリアルをより深く理解するには、C# プログラミングの基本的な知識が必要です。
チュートリアルを始める前に、まず IronPDF とは何かを理解しましょう。
IronPDF:
IronPDFは、PDFファイルの読み取り、作成、操作、印刷を最も簡単な方法で行えるようにする.NETライブラリです。 このライブラリは、C# および VB .NET アプリケーションで使用できます。 ASP .NET、ASP .NET Core、MVC、Web フォーム、および .NET と .NET Core の両方の Web API で簡単に動作します。 ユーザーは PDF ファイルをダウンロードし、電子メールで送信し、クラウドに保存することができます。
このリンクを使用して、IronPDF について詳しく調べることができます。
デモを始めましょう。
Visual Studioを開く
Visual Studioを開きます。 既存のソフトウェアにこの機能を追加する場合は、新しいプロジェクトを作成するか、既存のプロジェクトを開きます。
このチュートリアルではコンソール アプリケーションを使用しています。 ソフトウェアの要件に応じて、任意の選択肢を使用できます。
プロジェクトが作成されました。
NuGet パッケージをインストールする
ウィンドウ上部のメニューバーから Tools > NuGet Package Manager > Package Manager Console をクリックしてコンソールを開きます。
NuGet パッケージ マネージャー コンソールは次のように開きます。
NuGet Package Manager コンソール
Write the following command in the console to Install IronPdf NuGet Package and press Enter.
Install-Package IronPrint
NuGet パッケージは以下のようにインストールされます。
NuGet パッケージのインストール
C#でPDFファイルを印刷する
ここで、IronPDF ライブラリを使用して C# で PDF ファイルを印刷する方法を示す例を考えてみましょう。
Program.cs ファイルを開きます。IronPDFを使用するには、Program.cs ファイルの先頭に次の名前空間を追加します。
using IronPdf;
using IronPdf;
Imports IronPdf
それでは、Main 関数の中に以下のコードを記述してください。
using System; // Required for Console 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
Console.WriteLine("File Printed Successfully!");
Console.ReadKey();
}
}
using System; // Required for Console 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
Console.WriteLine("File Printed Successfully!");
Console.ReadKey();
}
}
Imports System ' Required for Console operations
Friend 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()
' Print operation success message to console
Console.WriteLine("File Printed Successfully!")
Console.ReadKey()
End Using
End Sub
End Class
コードを理解しましょう。
- 次のコード行は PDF ファイルをメモリに読み込みます。 "Sample.Pdf"はファイル名で、Dドライブがファイルの場所です。 ファイル名とともに完全なファイル パスを記述する必要があります。
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
Print()関数は、デフォルトのプリンタを使用して読み込まれたファイルを印刷します。
プログラムをテストする
プログラムを実行するには、Ctrl + F5 を押します。
コンソール
C#で複数のPDFドキュメントを印刷する
IronPDF を使用して C# で複数の PDF ドキュメントを印刷する方法を説明します。
14個のPDFファイル
この例では、印刷する必要がある PDF ドキュメントが 14 個あります。 これらには 1 〜 14 の番号を付けました。すべてのファイルを ID 付きで保存することも、ファイルの場所をデータベースに保存することもできます。 データベースからファイルのパスとファイル名を抽出し、配列またはリストに保存します。その配列またはリストを反復処理して、すべてのファイルを印刷します。
Main 関数の中に以下のコードを記述してください。
using System; // Required for Console 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
Console.WriteLine("Printing File: {0}.pdf", i);
// Print the loaded PDF document to the default printer
pdfDocument.Print();
// Print operation success message to console
Console.WriteLine("{0}.pdf Printed Successfully!", i);
}
Console.ReadKey();
}
}
using System; // Required for Console 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
Console.WriteLine("Printing File: {0}.pdf", i);
// Print the loaded PDF document to the default printer
pdfDocument.Print();
// Print operation success message to console
Console.WriteLine("{0}.pdf Printed Successfully!", i);
}
Console.ReadKey();
}
}
Imports System ' Required for Console operations
Friend 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
Console.WriteLine("Printing File: {0}.pdf", i)
' Print the loaded PDF document to the default printer
pdfDocument.Print()
' Print operation success message to console
Console.WriteLine("{0}.pdf Printed Successfully!", i)
End Using
Next i
Console.ReadKey()
End Sub
End Class
for ループを使用して、ファイル名に応じて 1 から 14 までの数字を生成しました。
ソリューションを実行します:
プログラムを実行するには、Ctrl + F5 を押します。
コンソール
URL を PDF に変換して印刷します
URL を解析して IronPDF に Web ページを印刷する方法を紹介します。 ライブラリは HTML を解析して PDF ファイルを作成します。 印刷機能を使用して、その PDF ドキュメントを印刷できます。 例を考えてみましょう。
メイン関数の中に次のコードを書いてください:
using System; // Required for Console operations
class Program
{
static void Main()
{
// HTML to PDF Renderer
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
// Inform about the printing operation
Console.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
Console.WriteLine("File Printed Successfully!");
}
}
using System; // Required for Console operations
class Program
{
static void Main()
{
// HTML to PDF Renderer
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
// Inform about the printing operation
Console.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
Console.WriteLine("File Printed Successfully!");
}
}
Imports System ' Required for Console operations
Friend Class Program
Shared Sub Main()
' HTML to PDF Renderer
Dim Renderer As New IronPdf.HtmlToPdf()
' Inform about the printing operation
Console.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()
' Print operation success message to console
Console.WriteLine("File Printed Successfully!")
End Using
End Sub
End Class
このプログラムは https://ironpdf.com/how-to/print-pdf/#advanced-printing を解析して PDF に変換し、Print() 関数はデフォルトのプリンタを使用して PDF を印刷します。
プログラムを実行してみましょう。
プログラムを実行します:
ソリューションを実行するには、Ctrl + F5 を押します。
画面に表示される出力
Print() 関数は、既定のプリンタを使用してドキュメントを印刷します。 デフォルトのプリンターを使用したくない状況が発生する場合があります。 その場合、IronPDF はプリンター名を変更するための 1 行のコードを提供します。
プリンター名の変更:
さらに理解を深めるために例を挙げてみましょう。 デフォルトのプリンタを変更するには、以下のコードを使用してください。
using System; // Required for Console 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
Console.WriteLine("File Printed Successfully!");
}
}
using System; // Required for Console 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
Console.WriteLine("File Printed Successfully!");
}
}
Imports System ' Required for Console operations
Friend 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
Console.WriteLine("File Printed Successfully!")
End Sub
End Class
プリンターを指定するための追加コードを 1 行だけ追加したことがわかります。 デモ用に Microsoft Print to PDF を指定しました。
プログラムを実行します:
Ctrl + F5 を押してプログラムを実行します。
Microsoft Print to PDF を使用しているため、保存するファイル名の入力を求める次のダイアログ ボックスが表示されます。
ファイル名を指定して、"保存"ボタンをクリックします。 "SamplePrint.Pdf"を指定しました。 次のコンソール出力が表示されます。
Microsoft Print to PDF で D ドライブの PDF ファイルを印刷しました。確認してみましょう。
このチュートリアルが役に立ち、インタラクティブで、理解しやすいものであったことを願っています。 このチュートリアルに関する質問やフィードバックは、以下のコメント セクションに記入してください。
よくある質問
C#でIronPDFを使用してPDFファイルを印刷するにはどうすればよいですか?
C#でIronPDFを使用してPDFファイルを印刷するには、PdfDocument.FromFileメソッドを使用してPDF文書をロードし、ロードした文書に対してPrintメソッドを呼び出します。
IronPDFは複数のPDFファイルを順次印刷できますか?
はい、IronPDFはファイルパスのコレクションを通過して各文書に対してPrintメソッドを呼び出すことで複数のPDFファイルを印刷できます。
C#でURLをPDFに変換してから印刷するにはどうすればよいですか?
IronPDFを使用して、HtmlToPdf.RenderUrlAsPdfメソッドでURLをPDFに変換します。そして、生成されたPDF文書に対してPrintメソッドを使用して印刷します。
IronPDFでPDFを印刷するために特定のプリンタを選択することは可能ですか?
はい、PdfDocumentクラスのGetPrintDocumentメソッドを使用する際に、PrinterSettingsオブジェクト内のPrinterNameプロパティを設定することで特定のプリンターを選択できます。
IronPDFはどのプラットフォームでPDF印刷をサポートしていますか?
IronPDFは.NET Framework、.NET Core、.NET 5および6アプリケーションでPDF印刷をサポートしており、Windows、macOS、Android、iOSプラットフォームと互換性があります。
C#で.NETライブラリを使用してPDFの印刷を始めるにはどうすればいいですか?
IronPDFを使用してPDFを印刷するには、Visual StudioのNuGetパッケージマネージャーを介してライブラリをインストールし、Iron Softwareが提供するドキュメンテーションと例に従います。
文書共有のためにPDFを使用する利点は何ですか?
PDFは軽量で、異なるデバイス間で一貫したフォーマットを維持し、ほとんどのオペレーティングシステムでサポートされているため、文書共有に有利です。
IronPDFを使って印刷するには高度なC#スキルが必要ですか?
基本的なC#の知識で、IronPDFの印刷タスクを利用するには十分です。ライブラリは開発者の助けとなる簡単な方法と例を提供します。



