透かしなしで本番環境でテストしてください。
必要な場所で動作します。
30日間、完全に機能する製品をご利用いただけます。
数分で稼働させることができます。
製品トライアル期間中にサポートエンジニアリングチームへの完全アクセス
PDF フ ァ イ ルを C# (シーシャープ) アプ リ ケーシ ョ ンか ら 直接印刷す る こ と は、 と り わけ物理的な PDF 文書 と シームレスに統合す る 必要があ る アプ リ ケーシ ョ ン を開発す る 際に有用な機能です。 PDF文書管理システム、POSアプリケーション、または印刷を扱うその他のソフトウェアのいずれに取り組んでいる場合でも、C#(シーシャープ)は、このPDF印刷メソッド機能を容易にするための堅牢なライブラリセットを提供しています。
この目的のために、Microsoft C# は PDF ファイルをデフォルトのプリンターに印刷するための印刷メソッドを提供します。 この記事では、C#(シーシャープ)を使ってPDFファイルをプリンターに印刷する手順を探ります。
C#(シーシャープ)Windowsフォームアプリケーションの作成
キーワードを使用してSystem.Drawing.Printingをインポートする
ボタンとその他の必要なコントロールを備えたフォームをデザインする
PrintPage イベントを使用した PrintDocument イベントの処理
印刷 ジョブを開始する
始める前に、以下の前提条件が整っていることを確認してください:
C# 開発環境 (例: Visual Studio)。
プリンターとやりとりするための適切な権限。
新しいC#(シーシャープ)プロジェクトを作成するか、既存のプロジェクトをお好みの開発環境で開きます。 プロジェクトが正しく設定され、プリンターとのやりとりに必要な権限が与えられていることを確認してください。 以下のプロセスで、このプロセスを完了することができます:
Visual Studioをインストールしていない場合は、公式ウェブサイトからダウンロードしてインストールしてください:Visual Studio。
Visual Studioを開きます。
「新しいプロジェクトの作成」ダイアログで、好みに応じて「Windows Forms App (.NET Framework)」または「Windows Forms App (.NET Core)」を選択します。
プロジェクトの名前と場所を記入してください。
プロジェクトが作成されると、メインフォームがデザイナーに表示されます。
ツールボックスを使用して、ボタン、テキストボックス、ラベルなどのコントロールを必要に応じてフォームに追加します。
C#(シーシャープ)コード・ファイルで、印刷に関連するクラスとメソッドにアクセスするために必要な名前空間をインポートします。
using System.Drawing.Printing;
using System.Drawing.Printing;
Imports System.Drawing.Printing
これらの名前空間は、印刷操作を処理するためのPrintDocument、PrintPageEventArgs、およびPrintControllerなどの基本クラスを提供します。
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
この例では、テキストファイルの内容を読み込み、指定されたフォントと書式を使って印刷します。
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
このコードはPrintDocumentインスタンスを作成し、PrintPageイベントハンドラを添付し、その後にドキュメントを印刷します。 オプションの印刷ダイアログでは、印刷ジョブを開始する前に印刷設定を行うことができます。 これは、テキスト文書をローカルに接続されたプリンターに印刷する。 それが存在しない場合、ファイルは以下に示すように印刷ダイアログのデフォルトプリンタ名(Microsoft Print to PDF)に印刷されます。
C#(シーシャープ)コンソールアプリケーションで印刷機能を効率的かつ効果的に処理する場合、IronPrintライブラリは強力なソリューションを提供します。
IronPrintは、Iron Softwareによって開発された包括的な印刷ライブラリであり、.NETアプリケーションとシームレスに統合するように設計されています。 デスクトップ、ウェブ、モバイルのどのプロジェクトでも、IronPrintは様々なファイル形式をサポートし、カスタマイズ可能な印刷設定を提供することで、PDFドキュメントの多彩な印刷機能を提供します。
フォーマットサポート: IronPrintはPDF、PNG、HTML、TIFF、GIF、JPEG、BITMAPを含むさまざまなドキュメントフォーマットをサポートしています。この柔軟性により、開発者は印刷のために異なる種類のコンテンツを扱うことができます。
カスタマイズ可能な設定: 開発者はアプリケーションの要件に応じて印刷設定をカスタマイズする柔軟性があります。 これには、DPI(1インチあたりのドット数)を設定するオプション、用紙の向き(縦または横)を指定するオプション、およびコピー数を制御するオプションが含まれます。
印刷ダイアログ: IronPrintは、印刷前に開発者が印刷ダイアログを表示できるようにすることで、シームレスなユーザーエクスペリエンスを提供します。 これは、ユーザーが印刷プロセスと対話し、特定のオプションを選択する必要があるシナリオで有用である。
クロスプラットフォーム互換性: IronPrint はプラットフォームの制限を超え、Windows (7+)、macOS (10+)、iOS (11+)、および Android API 21+ (v5「ロリポップ」) を含む多様な環境と互換性を提供します。 それは、モバイル(Xamarin、MAUI、Avalonia)、デスクトップ(WPF、MAUI、Windows Avalonia)、コンソール(アプリ & ライブラリ)などのさまざまなプロジェクトタイプとシームレスに統合されます。
ファイル印刷に入る前に、IronPrintライブラリをインストールする必要があります。 NuGetパッケージマネージャーコンソールを使えば簡単にできます:
Install-Package IronPrint
このコマンドラインはIronPrintライブラリをダウンロードし、C#プロジェクトにインストールします。
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
IronPrintを使ったファイルの印刷は簡単です。 Printer クラスを使用し、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
この例では、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
出力はこんな感じだ:
物理的なプリンターがインストールされていない場合、デフォルトのプリンターが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
詳細情報については、ドキュメントページをご覧ください。
C# でのファイル印刷は、System.Drawing.Printing 名前空間によって提供される機能を使用することで管理しやすいタスクです。 PrintPageイベントを処理し、PrintDocumentクラスを利用することで、特定の要件に合わせて印刷プロセスを調整できます。 この包括的なガイドでは、C#(シーシャープ)アプリケーションからファイルを印刷するための基本的な手順について説明します。 さらに検討するうちに、IronPrintに、異なるファイルタイプの取り扱い、画像の組み込み、アプリケーションのニーズに基づいたフォーマットの強化など、さらなるカスタマイズ・オプションがあることに気づきました。
IronPrintをC# (シーシャープ)アプリケーションに統合することで、ファイルをプリンターに印刷するプロセスが簡素化されます。 様々なファイルフォーマットとカスタマイズ可能な印刷設定をサポートするIronPrintは、印刷機能を強化したい開発者に堅牢なソリューションを提供します。 デスクトップ、ウェブ、モバイルのどのプロジェクトでも、IronPrintは印刷プロセスを合理化し、.NET用ツールキットへの価値ある追加となります。
IronPrintは、無料トライアルページで詳細情報を提供しています。 こちらからライブラリをダウンロードして試してみてください。