フッターコンテンツにスキップ
IRONPRINTの使用

C# IronPDFを使用したPDFファイルの印刷

PDF は最も一般的に使用されるファイル形式です。 軽量で、ほとんどのオペレーティング システムでサポートされています。 PDF ファイルはデバイス間で簡単に共有できます。 どのインターフェースや OS でもまったく同じように表示されるため、読みやすくなります。 PDF には、モバイル、タブレットなど、あらゆるデバイスからアクセスできます。

PDF ファイルは人気があるため、すべての C# 開発者は C# を使用して PDF ファイルを印刷する方法を知っておく必要があります。 これは、請求書、給与明細、注文伝票、プロファイルなどのドキュメントを印刷する必要がある場合に問題が発生する可能性があるためです。これらのドキュメントはしばしばPDFファイル形式であるため、C#を使用してPDFファイルを印刷する方法を理解する必要があります。

C# で PDF ドキュメントを印刷するには、複数のライブラリを使用します。 しかし、遭遇する可能性がある問題の 1 つはライセンス料です。 それらのほとんどは無料ではないか、使いにくいものです。 しかし幸いなことに、心配する必要はありません。 C# で PDF ファイルを印刷する最も簡単でシンプルな方法を見つけました。

PDF ドキュメントを印刷するには、 IronPDFライブラリを使用します。 信じてください、きっと気に入っていただけると思います!


この記事から何が得られますか?

この記事を読んだ後、次のタスクを実行できるようになります。

  • 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を開きます。 既存のソフトウェアにこの機能を追加する場合は、新しいプロジェクトを作成するか、既存のプロジェクトを開きます。

このチュートリアルではコンソール アプリケーションを使用しています。 ソフトウェアの要件に応じて、任意の選択肢を使用できます。

Project Created

プロジェクトが作成されました。

NuGet パッケージをインストールする

ウィンドウの上部にあるメニュー バーから[ツール] > [NuGet パッケージ マネージャー] > [パッケージ マネージャー コンソール]をクリックして、コンソールを開きます。

Csharp Print Pdf Files 2 related to NuGet パッケージをインストールする

NuGet パッケージ マネージャー コンソールは次のように開きます。

NuGet Package Manager コンソール

NuGet Package Manager コンソール

IronPDF NuGetパッケージ をインストールするためにコンソールで次のコマンドを実行し、Enterを押します。

Install-Package IronPrint

NuGet パッケージは以下のようにインストールされます。

NuGet パッケージのインストール

NuGet パッケージのインストール


Print PDF Files in C

ここで、IronPDF ライブラリを使用して C# で PDF ファイルを印刷する方法を示す例を考えてみましょう。

Program.cs ファイルを開きます。IronPDFを使用するには、Program.cs ファイルの先頭に以下の名前空間を追加してください。

using IronPdf;
using IronPdf;
Imports IronPdf
$vbLabelText   $csharpLabel

次に、Main 関数内に次のコードを書きます。

using System; // Required for コンソール 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 コンソール 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 コンソール 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
        Console.WriteLine("File Printed Successfully!")
        Console.ReadKey()
    End Sub
End Class
$vbLabelText   $csharpLabel

コードを理解しましょう。

  • 次のコード行は 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
$vbLabelText   $csharpLabel
  • Print() 関数は、デフォルトのプリンターを使用して読み込んだファイルを印刷します。

プログラムをテストする

プログラムを実行するには、Ctrl + F5 を押します。

コンソール

コンソール


Print Multiple PDF Documents in C

IronPDF を使用して C# で複数の PDF ドキュメントを印刷する方法を説明します。

14個のPDFファイル

14個のPDFファイル

この例では、印刷する必要がある PDF ドキュメントが 14 個あります。 これらには 1 〜 14 の番号を付けました。すべてのファイルを ID 付きで保存することも、ファイルの場所をデータベースに保存することもできます。 データベースからファイルのパスとファイル名を抽出し、配列またはリストに保存します。その配列またはリストを反復処理して、すべてのファイルを印刷します。

Main 関数内に次のコードを書きます。

using System; // Required for コンソール 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 コンソール 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 コンソール 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
                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

        Console.ReadKey()
    End Sub
End Class
$vbLabelText   $csharpLabel

for ループを使用して、ファイル名に応じて 1 から 14 までの数字を生成しました。

ソリューションを実行します:

プログラムを実行するには、Ctrl + F5 を押します。

コンソール

コンソール

URL を PDF に変換して印刷します

URL を解析して IronPDF に Web ページを印刷する方法を紹介します。 ライブラリは HTML を解析して PDF ファイルを作成します。 印刷機能を使用して、その PDF ドキュメントを印刷できます。 例を考えてみましょう。

メイン関数の中に次のコードを書いてください:

using System; // Required for コンソール 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 コンソール 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 コンソール operations

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()
        End Using

        ' Print operation success message to console
        Console.WriteLine("File Printed Successfully!")
    End Sub
End Class
$vbLabelText   $csharpLabel

このプログラムはhttps://ironpdf.com/how-to/print-pdf/#advanced-printing をPDFに解析し、Print() 関数がそのPDFをデフォルトのプリンターを使用して印刷します。

プログラムを実行してみましょう。

プログラムを実行します:

ソリューションを実行するには、Ctrl + F5 を押します。

画面に表示される出力

画面に表示される出力

Print() 関数は、デフォルトのプリンターを使用してドキュメントを印刷します。 デフォルトのプリンターを使用したくない状況が発生する場合があります。 その場合、IronPDF はプリンター名を変更するための 1 行のコードを提供します。

プリンター名の変更:

さらに理解を深めるために例を挙げてみましょう。 デフォルトのプリンタを変更するには、以下のコードを使用してください。

using System; // Required for コンソール 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 コンソール 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 コンソール 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
        Console.WriteLine("File Printed Successfully!")
    End Sub
End Class
$vbLabelText   $csharpLabel

プリンターを指定するための追加コードを 1 行だけ追加したことがわかります。 デモ用に Microsoft Print to PDF を指定しました。

プログラムを実行します:

Ctrl + F5 を押してプログラムを実行します。

Microsoft Print to PDF を使用しているため、保存するファイル名の入力を求める次のダイアログ ボックスが表示されます。

Csharp Print Pdf Files 10 related to プログラムを実行します:

ファイル名を指定して、"保存"ボタンをクリックします。 "SamplePrint.Pdf"を指定しました。 次のコンソール出力が表示されます。

Csharp Print Pdf Files 11 related to プログラムを実行します:

Microsoft Print to PDF で D ドライブの PDF ファイルを印刷しました。確認してみましょう。

Csharp Print Pdf Files 12 related to プログラムを実行します:

このチュートリアルが役に立ち、インタラクティブで、理解しやすいものであったことを願っています。 このチュートリアルに関する質問やフィードバックは、以下のコメント セクションに記入してください。

よくある質問

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の印刷タスクを利用するには十分です。ライブラリは開発者の助けとなる簡単な方法と例を提供します。

Curtis Chau
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

アイアンサポートチーム

私たちは週5日、24時間オンラインで対応しています。
チャット
メール
電話してね