IRONPRINTの使用

C#でファイルをプリンタに印刷する方法

更新済み 4月 29, 2024
共有:

PDF フ ァ イ ルを C# (シーシャープ) アプ リ ケーシ ョ ンか ら 直接印刷す る こ と は、 と り わけ物理的な PDF 文書 と シームレスに統合す る 必要があ る アプ リ ケーシ ョ ン を開発す る 際に有用な機能です。 PDF文書管理システム、POSアプリケーション、または印刷を扱うその他のソフトウェアのいずれに取り組んでいる場合でも、C#(シーシャープ)は、このPDF印刷メソッド機能を容易にするための堅牢なライブラリセットを提供しています。

この目的のために、Microsoft C#(シーシャープ)には プリント方式 PDFファイルをデフォルトのプリンタで印刷する。 この記事では、C#(シーシャープ)を使ってPDFファイルをプリンターに印刷する手順を探ります。

C#(シーシャープ)でファイルをプリンターに印刷する方法;

  1. C#(シーシャープ)Windowsフォームアプリケーションの作成

  2. キーワードを使用してSystem.Drawing.Printingをインポートする。

  3. ボタンとその他の必要なコントロールを備えたフォームをデザインする

  4. PrintPageイベントを使用したPrintDocument**イベントの処理

  5. 印刷**ジョブの開始

  6. アプリケーションを実行し、印刷ボタンをクリックして印刷する。

前提条件

始める前に、以下の前提条件が整っていることを確認してください:

  1. C#(シーシャープ)開発環境 (例:Visual Studio).

  2. プリンターとやりとりするための適切な権限。

  3. C#(シーシャープ)プログラミングの基本的な理解。

ステップ1:プロジェクトの設定

新しいC#(シーシャープ)プロジェクトを作成するか、既存のプロジェクトをお好みの開発環境で開きます。 プロジェクトが正しく設定され、プリンターとのやりとりに必要な権限が与えられていることを確認してください。 以下のプロセスで、このプロセスを完了することができます:

Visual Studioのインストール

Visual Studioがインストールされていない場合は、公式サイトからダウンロードしてインストールしてください: Visual Studio(ビジュアル スタジオ)ご提供いただきましたコンテンツが空白です。翻訳が必要な英語のテキストをお送りください。その内容を日本語に翻訳いたします。

新しいプロジェクトを作成

  1. Visual Studioを開きます。

    1. 「Create a new project」をクリックしてください。

    C#(シーシャープ)でファイルをプリンターに印刷する方法:図1 - 新規プロジェクト

プロジェクトテンプレートの選択

  1. 新規プロジェクトの作成」ダイアログで、「Windows Forms App」を選択します。 (.NETフレームワーク)「または「Windows Forms App (.NET Core(ドットネット コア))"をお好みに応じてお選びください。

    C#(シーシャープ)でファイルをプリンターに印刷する方法:図2 - Windowsフォームアプリ

  2. プロジェクトの名前と場所を記入してください。

    C#(シーシャープ)でファイルをプリンターに印刷する方法:図3 - プロジェクトの構成

  1. "次へ "をクリックし、追加情報画面から.NETフレームワークを選択し、"作成 "をクリックします。

フォームのデザイン

  1. プロジェクトが作成されると、メインフォームがデザイナーに表示されます。

  2. ツールボックスを使用して、ボタン、テキストボックス、ラベルなどのコントロールを必要に応じてフォームに追加します。

    1. プロパティ・ウィンドウを使って、各コントロールのプロパティをカスタマイズする。

    C#(シーシャープ)でファイルをプリンターに印刷する方法:図4 - フォームのデザイン

    1. フォームの外観とレイアウトを調整します。

    C#でファイルをプリンタに印刷する方法 (シーシャープ):図5 - 印刷フォーム

ステップ 2: 必要なライブラリのインポート

C#(シーシャープ)コード・ファイルで、印刷に関連するクラスとメソッドにアクセスするために必要な名前空間をインポートします。

using System.Drawing.Printing;
using System.Drawing.Printing;
Imports System.Drawing.Printing
VB   C#

これらの名前空間は、PrintDocumentPrintPageEventArgsPrintController など、印刷操作を処理するために不可欠なクラスを提供します。

ステップ3:PrintDocumentイベントの処理

PrintDocumentクラスはC#(シーシャープ)での印刷の要です。 そのPrintPage**イベントを処理して、印刷するコンテンツとその書式を定義する。 テキストファイルの内容を印刷する簡単な例から始めよう:

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    // Specify the file path
    string filePath = "C:\\path\\to\\your\\file.txt";
    // Read the content of the file
    string line = System.IO.File.ReadAllText(filePath);
    // Create a Font object (adjust as needed)
    Font font = new Font("Arial", 12);
    // Create a RectangleF to define the printing area
    RectangleF area = new RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height);
    // Draw the content to the printing area
    e.Graphics.DrawString(line, font, Brushes.Black, area);
    // Set HasMorePages to false to indicate that there are no more pages to print
    e.HasMorePages = false;
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    // Specify the file path
    string filePath = "C:\\path\\to\\your\\file.txt";
    // Read the content of the file
    string line = System.IO.File.ReadAllText(filePath);
    // Create a Font object (adjust as needed)
    Font font = new Font("Arial", 12);
    // Create a RectangleF to define the printing area
    RectangleF area = new RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height);
    // Draw the content to the printing area
    e.Graphics.DrawString(line, font, Brushes.Black, area);
    // Set HasMorePages to false to indicate that there are no more pages to print
    e.HasMorePages = false;
}
Private Sub printDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
	' Specify the file path
	Dim filePath As String = "C:\path\to\your\file.txt"
	' Read the content of the file
	Dim line As String = System.IO.File.ReadAllText(filePath)
	' Create a Font object (adjust as needed)
	Dim font As New Font("Arial", 12)
	' Create a RectangleF to define the printing area
	Dim area As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
	' Draw the content to the printing area
	e.Graphics.DrawString(line, font, Brushes.Black, area)
	' Set HasMorePages to false to indicate that there are no more pages to print
	e.HasMorePages = False
End Sub
VB   C#

この例では、テキストファイルの内容を読み込み、指定されたフォントと書式を使って印刷します。

ステップ4:印刷ジョブの開始

PrintDocumentクラスのインスタンスを作成し、PrintPage**イベントハンドラをアタッチし、印刷処理をトリガーすることで、印刷ジョブを開始します。 オプションで、ユーザー設定用の印刷ダイアログを表示することができます:

private void btnPrint_Click(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
    // Optionally, display a print dialog for user configuration
    PrintDialog printDialog = new PrintDialog();
    if (printDialog.ShowDialog() == DialogResult.OK)
    {
        pd.PrinterSettings = printDialog.PrinterSettings;
        pd.Print();
    }
}
private void btnPrint_Click(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
    // Optionally, display a print dialog for user configuration
    PrintDialog printDialog = new PrintDialog();
    if (printDialog.ShowDialog() == DialogResult.OK)
    {
        pd.PrinterSettings = printDialog.PrinterSettings;
        pd.Print();
    }
}
Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As EventArgs)
	Dim pd As New PrintDocument()
	AddHandler pd.PrintPage, AddressOf printDocument1_PrintPage
	' Optionally, display a print dialog for user configuration
	Dim printDialog As New PrintDialog()
	If printDialog.ShowDialog() = DialogResult.OK Then
		pd.PrinterSettings = printDialog.PrinterSettings
		pd.Print()
	End If
End Sub
VB   C#

このコードでは、PrintDocument インスタンスを作成し、PrintPage イベントハンドラをアタッチして、ドキュメントを印刷します。 オプションの印刷ダイアログでは、印刷ジョブを開始する前に印刷設定を行うことができます。 これは、テキスト文書をローカルに接続されたプリンターに印刷する。 存在しない場合、ファイルはデフォルトのプリンター名に印刷されます。 (マイクロソフトPDFプリント) プリントダイアログの

C#(シーシャープ)でファイルをプリンターに印刷する方法:図6 - Microsoft印刷ダイアログ

C#(シーシャープ)の高度な印刷; IronPrint(アイアンプリント)ライブラリ経由

C#(シーシャープ)コンソールアプリケーションで印刷機能を効率的かつ効果的に処理する場合、IronPrint(アイアンプリント)ライブラリは強力なソリューションを提供します。

IronPrint の紹介

IronPrint (アイアンプリント) .NETアプリケーションとシームレスに統合するために設計され、アイアンソフトウェア)によって開発された包括的な印刷ライブラリです。 デスクトップ、ウェブ、モバイルのどのプロジェクトでも、アイアンプリントは様々なファイル形式をサポートし、カスタマイズ可能な印刷設定を提供することで、PDFドキュメントの多彩な印刷機能を提供します。

C#でファイルをプリンタに印刷する方法 (シーシャープ):図7 - IronPrint (アイアンプリント)

主な機能

  1. フォーマットサポート: IronPrintはPDF、PNG、HTML、TIFF、GIF、JPEG、BITMAPを含む様々なドキュメントフォーマットをサポートします。この多様性により、開発者はさまざまなタイプのコンテンツを印刷することができます。

  2. カスタマイズ可能な設定: 開発者は、アプリケーションの要件に応じて印刷設定を柔軟にカスタマイズできます。 これにはDPIを設定するオプションも含まれます。 (1インチ当たりのドット数)用紙の向きを指定する (縦向きまたは横向き)そして、部数をコントロールする。

  3. 印刷ダイアログ: IronPrintは、開発者が印刷前に印刷ダイアログを表示できるようにすることで、シームレスなユーザー体験を促進します。 これは、ユーザーが印刷プロセスと対話し、特定のオプションを選択する必要があるシナリオで有用である。

  4. クロスプラットフォーム互換性: IronPrintはプラットフォームの制限を超え、Windowsを含む様々な環境との互換性を提供します。 (7+)マックオス (10+)iOS (11+)およびAndroid API 21以上 (v5 "ロリポップ"). それは、モバイルのような異なるプロジェクトタイプとシームレスに統合されています。 (Xamarin、MAUI、Avalonia)デスクトップ (WPF、MAUI、ウィンドウズ・アヴァロニア)コンソール (アプリ&ライブラリー).

  5. 幅広い.NETバージョンサポート: 最新の.NET 8、7、6、またはコア3.1+を使用しているかどうかにかかわらず、IronPrintはあなたをカバーしています。 また、.NETフレームワークのサポートも拡張している。 (4.6.2+)様々な開発環境での互換性を確保。

IronPrint (アイアンプリント)のインストール

ファイル印刷に入る前に、IronPrint (アイアンプリント)ライブラリをインストールする必要があります。 NuGetパッケージマネージャーコンソールを使えば簡単にできます:

Install-Package IronPrint

このコマンドラインはIronPrint (アイアンプリント)ライブラリをダウンロードし、C#プロジェクトにインストールします。

IronPrint (アイアンプリント)の初期化

IronPrint (アイアンプリント)をインストールしたら、C# (シーシャープ)コードで初期化する必要があります。 IronPrintネームスペースをインポートし、適切な機能を確保するためにライセンスキーを設定します:

using IronPrint;
class Program
{
    public static void Main()
    {
        License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
    }
}
using IronPrint;
class Program
{
    public static void Main()
    {
        License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
    }
}
Imports IronPrint
Friend Class Program
	Public Shared Sub Main()
		License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY"
	End Sub
End Class
VB   C#

IronPrint (アイアンプリント)でファイルを印刷する

アイアンプリントを使ったファイルの印刷は簡単です。 次のコンテンツを日本語に翻訳してください: **プリンター クラスで、ファイルパスをPrintメソッドに指定して、PDFを静かに印刷します:

using IronPrint;
class Program
{
// static void main
    public static void Main()
    {
        License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
        // Specify the file path
        var document = "C:\\path\\to\\your\\file.pdf";
        // Print PDFs
        Printer.Print(document);
    }
}
using IronPrint;
class Program
{
// static void main
    public static void Main()
    {
        License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
        // Specify the file path
        var document = "C:\\path\\to\\your\\file.pdf";
        // Print PDFs
        Printer.Print(document);
    }
}
Imports IronPrint
Friend Class Program
' static void main
	Public Shared Sub Main()
		License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY"
		' Specify the file path
		Dim document = "C:\path\to\your\file.pdf"
		' Print PDFs
		Printer.Print(document)
	End Sub
End Class
VB   C#

この例ではPDFファイルを印刷していますが、IronPrintはPDF、PNG、HTML、TIFF、GIF、JPEG、IMAGE、BITMAPなどさまざまなファイル形式をサポートしています。

印刷設定のカスタマイズ

IronPrint (アイアンプリント)では、以下のカスタマイズが可能です。 印刷設定 アプリケーションの要件に従ってください。 PrintSettings**クラスを使って、DPI、部数、用紙の向きなどの設定を行うことができます。 次のコード例は、ページ設定を行い、PDF文書を印刷するのに役立ちます:

using IronPrint;
class Program
{
    public static void Main()
    {
        IronPrint.License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
    Console.WriteLine("Printing Started...");
        // Specify the file path
        string filePath = "C:\\path\\to\\your\\file.pdf";
        // Configure print settings
        PrintSettings printSettings = new PrintSettings();
        printSettings.Dpi = 300;
        printSettings.NumberOfCopies = 2;
        printSettings.PaperOrientation = PaperOrientation.Landscape;
        // Print the document with custom settings
        Printer.Print(filePath, printSettings);
    // Print using the Print dialog
    Printer.ShowPrintDialog(filePath, printSettings);
    }
}
using IronPrint;
class Program
{
    public static void Main()
    {
        IronPrint.License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
    Console.WriteLine("Printing Started...");
        // Specify the file path
        string filePath = "C:\\path\\to\\your\\file.pdf";
        // Configure print settings
        PrintSettings printSettings = new PrintSettings();
        printSettings.Dpi = 300;
        printSettings.NumberOfCopies = 2;
        printSettings.PaperOrientation = PaperOrientation.Landscape;
        // Print the document with custom settings
        Printer.Print(filePath, printSettings);
    // Print using the Print dialog
    Printer.ShowPrintDialog(filePath, printSettings);
    }
}
Imports IronPrint
Friend Class Program
	Public Shared Sub Main()
		IronPrint.License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY"
	Console.WriteLine("Printing Started...")
		' Specify the file path
		Dim filePath As String = "C:\path\to\your\file.pdf"
		' Configure print settings
		Dim printSettings As New PrintSettings()
		printSettings.Dpi = 300
		printSettings.NumberOfCopies = 2
		printSettings.PaperOrientation = PaperOrientation.Landscape
		' Print the document with custom settings
		Printer.Print(filePath, printSettings)
	' Print using the Print dialog
	Printer.ShowPrintDialog(filePath, printSettings)
	End Sub
End Class
VB   C#

出力はこんな感じだ:

C#(シーシャープ)でファイルをプリンターに印刷する方法図8 - カスタマイズされた印刷出力

物理的なプリンターがインストールされていない場合、デフォルトのプリンターがPDF文書の印刷に使用されます。 利用可能なすべてのプリンターを取得するには GetPrinterNames メソッド。

// Retrieve printers' name
List<string> printersName = Printer.GetPrinterNames();
foreach (string printer in printersName)
{
    Console.WriteLine(printer);
}
// Retrieve printers' name
List<string> printersName = Printer.GetPrinterNames();
foreach (string printer in printersName)
{
    Console.WriteLine(printer);
}
' Retrieve printers' name
Dim printersName As List(Of String) = Printer.GetPrinterNames()
For Each printer As String In printersName
	Console.WriteLine(printer)
Next printer
VB   C#

詳細な情報については、次のウェブサイトをご覧ください。 ドキュメント ページ

結論

C#(シーシャープ)でファイルを印刷することは、System.Drawing.Printing名前空間が提供する機能で管理可能なタスクです。 PrintPageイベントを処理し、PrintDocument**クラスを利用することで、特定の要件に合わせた印刷処理を行うことができます。 この包括的なガイドでは、C#(シーシャープ)アプリケーションからファイルを印刷するための基本的な手順について説明します。 さらに検討するうちに、アイアンプリントに、異なるファイルタイプの取り扱い、画像の組み込み、アプリケーションのニーズに基づいたフォーマットの強化など、さらなるカスタマイズ・オプションがあることに気づきました。

IronPrint (アイアンプリント)をC# (シーシャープ)アプリケーションに統合することで、ファイルをプリンターに印刷するプロセスが簡素化されます。 様々なファイルフォーマットとカスタマイズ可能な印刷設定をサポートするアイアンプリントは、印刷機能を強化したい開発者に堅牢なソリューションを提供します。 デスクトップ、ウェブ、モバイルのどのプロジェクトでも、IronPrintは印刷プロセスを合理化し、.NET用ツールキットへの価値ある追加となります。

IronPrint(アイアンプリント)は、次のようなサービスを提供しています。 無料体験 のページに詳細を掲載している。 ライブラリを以下からダウンロード [以下の内容を日本語に翻訳します:

ここに

ご希望のイディオムや技術用語が追加されることによって、より適切な翻訳が提供できる場合もありますので、詳細なコンテキストを教えていただけると幸いです。](/csharp/print/) そしてお試しください。

< 以前
C#でQRコードを印刷する方法
次へ >
C#でQRコードを印刷する方法

準備はできましたか? バージョン: 2024.8 新発売

無料のNuGetダウンロード 総ダウンロード数: 7,144 View Licenses >