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

C#でPDFファイルをサイレント印刷する方法

PDF ドキュメントは、要求された形式でデータを保存および転送できるポータブル ドキュメント フォーマットです。 保存されたデータのフォーマットが保持され、ユーザーはデジタル ドキュメントの処理の他の側面に集中できるようになります。 デジタル文書の印刷は、プラットフォームによってデータの表示が異なるため、面倒な作業です。 ただし、PDF ドキュメントでデータを送信すると、印刷用の形式が保持されます。 ただし、PDF ファイルをプログラムで印刷することは、C# 開発者にとって難しい場合があります。 IronPDF - C# PDF ライブラリのおかげで、 PDF ファイルの印刷プロセスが非常に簡単かつ簡単になります。

この記事では、IronPDF ライブラリを使用して C# で PDF ドキュメントをサイレントに印刷する方法について説明します。

IronPDF - C# PDF ライブラリ

IronPDF は、開発者が PDF ドキュメントを作成、読み取り、編集できるようにする C# .NET ライブラリです。 これは最高級の C# ライブラリであり、正確性、使いやすさ、速度を重視しています。 C#、F#、VB.NET向けに特別に設計されており、 .NET 7、6、5、Core、Standard、Frameworkと高い互換性を備えています。IronPDFの強力なChromiumエンジンを活用し、Web、デスクトップ、コンソール向けのHTMLからPDFを生成します。

さらに、IronPDF を使用すると、PDF を操作および編集したり、ヘッダーやフッターを追加したり、PDF からテキストや画像を簡単に抽出したりできます。

重要な機能には次のようなものがあります:

  • さまざまなファイル形式から PDF ファイルを読み込み、生成します
  • デフォルトのプリンタを使用してPDFファイルを保存および印刷する
  • PDFファイルの結合と分割
  • Adobe Reader なしの PDF エディター

IronPDF ライブラリを使用して C# で PDF ドキュメントをサイレント印刷する手順

PDF ドキュメントをサイレントに印刷するには、まず、ローカル コンピューターに次のコンポーネントをインストールする必要があります。

  1. Visual Studio - C# 開発用の公式 IDE であり、コンピューターにインストールする必要があります。 Visual Studio Web サイトからダウンロードしてインストールできます。 2.プロジェクトの作成- C# で PDF 印刷用のコンソール アプリケーションを作成するには、Visual Studio 2022 を使用して次の手順に従います。

    • Visual Studioを開き、新しいプロジェクトの作成をクリックします。

      Visual Studio

      Visual Studio

    • C#コンソールアプリを選択し、"次へ"をクリックします。

      新しいプロジェクトダイアログボックス

      新しいプロジェクトダイアログボックス

    • 次に、プロジェクトの名前を入力し、場所を選択して、"次へ"をクリックします。

      Web Forms

      Web Forms

    • アプリケーションに最新の .NET Framework を選択します。 安定バージョン 6.0 を使用します。

      追加情報

      追加情報

    • 作成をクリックすると、コンソール プロジェクトが作成され、プログラムで PDF ドキュメントを印刷できるようになります。
  2. IronPDF をインストールする- IronPDF ライブラリをダウンロードしてインストールするには 3 つの方法があります。 以下の方法があります:

    • Visual Studio の使用- Visual Studio には、C# プロジェクトに NuGet パッケージをインストールするのに役立つ NuGet パッケージ マネージャーがあります。

      • メニューバーのツールをクリックするか、
      • ソリューションエクスプローラーでプロジェクトファイルを右クリックします

        ソリューションエクスプローラー

        ソリューションエクスプローラー

        プロジェクトメニュー > NuGet パッケージの管理

        プロジェクトメニュー > NuGet パッケージの管理

      • 開いたら、NuGet パッケージ マネージャーで IronPDF を参照して、以下に示すようにインストールします。

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

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

    • NuGet パッケージを直接ダウンロードする- IronPDF をダウンロードする別の方法は、NuGet Web サイトにアクセスしてパッケージを直接ダウンロードすることです。 リンクはこちらですhttps://www.nuget.org/packages/IronPdf/
    • IronPDF .DLLライブラリをダウンロード- IronPDFはIronPDFのウェブサイトから直接ダウンロードすることもできます。IronPDF DLLダウンロードにアクセスしてインストールしてください。 使用するには、プロジェクト内の .DLL ファイルを参照してください。

PDFファイルと印刷ジョブを生成する

ここでは、URL から PDF ファイルを生成します。 PDF ファイルの作成は簡単で、通常は 2 つのステップで行われます。 次のコード サンプルは 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/")
$vbLabelText   $csharpLabel

上記のコードで PDF ドキュメント オブジェクトが作成され、印刷できる状態になります。 次に、デフォルトのプリンターを使用してPDF ドキュメントを印刷します。 コードは 1 行で、次のようになります。

// 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()
$vbLabelText   $csharpLabel

このPrintメソッドは、PDF を印刷用にデフォルトのプリンターに送信します。

高度な印刷オプション

サイレント印刷の場合、IronPDF はさまざまな高度な印刷オプションを提供します。

PdfDocument.GetPrintDocumentメソッドが使用され、結果はSystem.Drawing.Printing.PrintDocumentオブジェクトに保存されます。 コードはシンプルで次のようになります。

// 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()
$vbLabelText   $csharpLabel

プリンタ名を指定

IronPDF では、特定のプリンターに印刷することもできます。 名前を指定するには、 PrinterSettings.PrinterNameプロパティを使用します。 まず、現在の PDF ドキュメント オブジェクトを取得する必要があります。 コードサンプルは次の通りです:

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
$vbLabelText   $csharpLabel

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

もう 1 つの便利な機能は、プリンターの解像度を設定することです。 出力に応じて、印刷および表示されるピクセル数を制御できます。 PDF ドキュメントのDefaultPageSettings.PrinterResolutionプロパティを使用して解像度を設定できます。 以下に簡単なコードサンプルを示します。

// 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()
$vbLabelText   $csharpLabel

PrintToFile メソッド

PdfDocumentクラスは、C# でPDF をファイルに印刷できるPrintToFileメソッドを提供します。 プリンタ ダイアログを開かずにファイルをその場所に直接印刷するには、 pathToFile引数として受け取ります。 コードは簡単で次の通りです:

// 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)
$vbLabelText   $csharpLabel

完全なコード例は次のようになります。

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
$vbLabelText   $csharpLabel

コードが実行されると、URL が PDF ドキュメントに変換されます。 次に、PDF ドキュメントをサイレントに印刷するには、 GetPrintDocumentメソッドを使用します。 プログラム ファイルのコンパイルと実行が正常に完了すると、PDF ドキュメントとして保存するためのプリンター ダイアログ ボックスが表示されます。 指定されたプリンター名を使用して PDF が保存されます。

Csharp Print Pdf Silently 8 related to PrintToFile メソッド

Csharp Print Pdf Silently 9 related to PrintToFile メソッド

まとめ

この記事では、IronPDF を使用して PDF ドキュメントを静かに印刷する方法を詳しく見てきました。 IronPDF は、PDF 印刷時に多くの便利なオプションを提供します。 また、印刷されたページを追跡したり、ページ範囲間で印刷したりすることもできます。

サイレント印刷とその他の印刷オプションにより、IronPDF は PDF を操作する際の C# の優れたライブラリになります。

IronPDF は、さまざまな形式のデータを PDF に変換したり、PDF からさまざまな形式に変換したりするのに役立ちます。 これにより、開発者はアプリケーション開発プロセスに PDF 機能を簡単に統合できるようになります。 さらに、PDF ドキュメントの表示と編集には Adobe Acrobat Reader は必要ありません。

IronPDF は個人開発には無料で使用でき、商用利用のライセンスを取得できます。 It provides a free trial license to access and test out the full functionality of the library. 詳細は、こちらのリンクからご確認いただけます。

よくある質問

C#でPDFを静かに印刷するにはどうすればいいですか?

IronPDFのPrintメソッドを使用することで、C#でPDFを静かに印刷できます。これによりプリンターダイアログを開かずに印刷でき、ドキュメントのフォーマットが保持されます。

静かにPDFを印刷するためにIronPDFを設定するにはどのような手順が必要ですか?

静かにPDFを印刷するためにIronPDFを設定するには、NuGetパッケージマネージャーを介してライブラリをインストールするか、.DLLファイルをダウンロードします。次に、Printメソッドを使用して静かな印刷設定を構成し、PrinterNamePrinterResolutionのようなプリンターのプロパティを指定してください。

印刷時にPDFのフォーマットを保持するにはどうすればよいですか?

IronPDFは、特定のプリンター設定を構成することができ、PrinterResolutionプロパティの設定などにより、印刷の出力が元のドキュメントと一致することを保証します。

C#でPDFをファイルの場所に印刷できますか?

はい、IronPDFはPrintToFileメソッドを提供しており、プリンターダイアログを開かずに指定されたファイルの場所にPDFを直接印刷できます。

Adobe Acrobat Readerを使用せずにPDFを印刷できますか?

はい、IronPDFは、Adobe Acrobat Readerを必要とせずにPDFを印刷、表示、編集することができ、C#アプリケーションでPDFドキュメントを扱うための柔軟なツールです。

IronPDFはクロスプラットフォームのPDF印刷をサポートしていますか?

はい、IronPDFはクロスプラットフォームのPDF印刷をサポートしており、Windows、macOS、Android、iOSなどに対応しているため、さまざまな開発環境での利用が可能です。

IronPDFの試用オプションにはどのようなものがありますか?

IronPDFは個人開発用に無料で提供されており、商用ライセンスを取得する前にその全機能をテストできる試用ライセンスがあります。

IronPDFを使用してPDF印刷のための特定のプリンターを指定するにはどうすればよいですか?

IronPDFでPrinterSettings.PrinterNameプロパティを設定することで、特定のプリンターを指定し、プリントジョブを希望のプリンターに振り向けることができます。

C#アプリケーションをIronPDFで開発するために推奨されるIDEは何ですか?

Visual Studioは、IronPDFを使用したC#アプリケーションの開発に推奨される統合開発環境(IDE)であり、コーディングとプロジェクト管理のための堅牢なプラットフォームを提供します。

C#での静かなPDF印刷に関する問題をどのようにトラブルシュートできますか?

C#での静かなPDF印刷に問題が発生した場合、IronPDFが正しくインストールされていること、PrinterNamePrinterResolutionなどのプリンター設定が正確に構成されていることを確認してください。IronPDFのドキュメントを参照することも、役立つトラブルシューティングのヒントを提供するかもしれません。

カーティス・チャウ
テクニカルライター

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

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