透かしなしで本番環境でテストしてください。
必要な場所で動作します。
30日間、完全に機能する製品をご利用いただけます。
数分で稼働させることができます。
製品トライアル期間中にサポートエンジニアリングチームへの完全アクセス
Humanizerは、データを扱うプロセスを簡素化し、人間に優しい形式で情報を表示する際に特に役立つ、強力で柔軟な.NETライブラリです。 日付を相対時間文字列に変換する必要があるかどうか(3日前)複数形の単語化、数値を単語としてフォーマット、列挙型の操作、文字列の表示、Pascalケースの入力文字列を文章に変換、カスタム説明付きの単語として表示、アンダースコアのついた入力文字列を通常のタイトルケース文字列に変換、長いテキストの切り詰めなど、HumanizerはこれらのタスクをC#.NETで優雅に処理するための多数のツールと拡張メソッドを提供します。
この記事では、C# における Humanizer の詳細なチュートリアルについて説明します。 HumanizerおよびIronPDF for C# PDF Libraryを使用してPDFドキュメントを生成する方法についても話し合います。
はじめにHumanizerを使用するには、NuGet経由でライブラリをインストールする必要があります。 プロジェクト内で、次のコマンドを使用してパッケージ マネージャー コンソールでこれを実行できます:
Install-Package Humanizer
Install-Package Humanizer
代わりに、.NET Core CLIを使用している場合は、次のコマンドでHumanizerを追加できます:
dotnet add package Humanizer
インストール後、C#ファイルに適切な名前空間を含めることで、Humanizerの使用を開始できます:
using Humanizer;
using Humanizer;
Humanizerの最も一般的な使用方法の一つは、日付や時刻を人間が読みやすい形式に変換し、タイムスパン、数値、および数量をHumanize
メソッドを使用して表示することです。 これは、たとえば「2時間前」や「5日後」のような相対的な時間を表示する際に特に有用です。
DateTime pastDate = DateTime.Now.AddDays(-3);
string humanizedTime = pastDate.Humanize(); // Output: "3 days ago"
DateTime futureDate = DateTime.Now.AddHours(5);
string futureHumanizedTime = futureDate.Humanize(); // Output: "in 5 hours"
DateTime pastDate = DateTime.Now.AddDays(-3);
string humanizedTime = pastDate.Humanize(); // Output: "3 days ago"
DateTime futureDate = DateTime.Now.AddHours(5);
string futureHumanizedTime = futureDate.Humanize(); // Output: "in 5 hours"
Humanizer 拡張メソッドは自動的に異なる時間単位を処理し、文法的な正しさも調整します。
Humanizerは、TimeSpan
オブジェクトもヒューマナイズすることができ、期間を読みやすい形式で表示することを容易にします。
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
string humanizedTimeSpan = timeSpan.Humanize(2); // Output: "2 hours, 3 minutes"
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
string humanizedTimeSpan = timeSpan.Humanize(2); // Output: "2 hours, 3 minutes"
Humanizerは、数値を人間に分かりやすい言葉に変換するためのいくつかのメソッドを提供し、序数の処理も行います。
int number = 123;
string words = number.ToWords(); // Output: "one hundred and twenty-three"
int number = 123;
string words = number.ToWords(); // Output: "one hundred and twenty-three"
int number = 21;
string ordinal = number.ToOrdinalWords(); // Output: "twenty-first"
int number = 21;
string ordinal = number.ToOrdinalWords(); // Output: "twenty-first"
Humanizerは、単語を単数形と複数形の間で簡単に変換でき、数量に基づいて長いテキストを動的に生成するのに役立ちます。
string singular = "car";
string plural = singular.Pluralize(); // Output: "cars"
string word = "people";
string singularForm = word.Singularize(); // Output: "person"
string singular = "car";
string plural = singular.Pluralize(); // Output: "cars"
string word = "people";
string singularForm = word.Singularize(); // Output: "person"
Humanizerは不規則な複数形化と単数形化も処理し、さまざまなユースケースに対して頑丈です。
Enumsは、名前付き定数のセットを表すためにC#アプリケーションで頻繁に使用されます。 Humanizerは列挙型の値を人間が読める文字列に変換できます。
MyEnum enumValue = MyEnum.FirstValue;
string humanizedEnum = enumValue.Humanize();
System.Console.WriteLine(humanizedEnum);
public enum MyEnum
{
FirstValue,
SecondValue
} // Output: "First value"
MyEnum enumValue = MyEnum.FirstValue;
string humanizedEnum = enumValue.Humanize();
System.Console.WriteLine(humanizedEnum);
public enum MyEnum
{
FirstValue,
SecondValue
} // Output: "First value"
このメソッドは、ユーザーインターフェイスでユーザーフレンドリーなラベルを表示するのに特に有用です。
Humanizerのもう一つ便利な機能は、バイトサイズを人間が読みやすい形式に変換する能力です。大きなバイト値をKB、MB、GBなどの形式に変換します。
long bytes = 1048576;
string humanizedBytes = bytes.Bytes().Humanize(); // Output: "1 MB"
long bytes = 1048576;
string humanizedBytes = bytes.Bytes().Humanize(); // Output: "1 MB"
Humanizerは、上述した基本的なシナリオに限定されません。 Truncate
メソッドや複数の言語および拡張機能などの高度な機能に対応しています。
Humanizerは、タイムゾーンを扱うアプリケーションに便利なDateTimeOffset
も処理できます。
DateTimeOffset dateTimeOffset = DateTimeOffset.Now.AddDays(-2);
string humanizedDateTimeOffset = dateTimeOffset.Humanize(); // Output: "2 days ago"
DateTimeOffset dateTimeOffset = DateTimeOffset.Now.AddDays(-2);
string humanizedDateTimeOffset = dateTimeOffset.Humanize(); // Output: "2 days ago"
Humanizerは効率的に設計されていますが、他のライブラリと同様、パフォーマンスは使用方法に依存します。 高性能を必要とするアプリケーション、特に大規模なデータセットやリアルタイム処理を扱うものでは、頻繁な人為的操作が与える影響を考慮することが重要です。
IronPDFは、.NETアプリケーション向けの総合的なPDF生成および操作ライブラリです。 それにより、開発者はPDFファイルからコンテンツを簡単に作成、読み取り、編集、および抽出できるようになります。 IronPDFは、使いやすさを重視して設計されており、HTMLからPDFへの変換、ドキュメントの結合、ウォーターマークの追加など、幅広い機能を提供しています。 その汎用性と強力な機能により、C#プロジェクトでPDFドキュメントを処理する際に最適な選択肢となります。
以下の手順に従って、NuGetパッケージマネージャーを使用してIronPDFをインストールします:
Visual Studioでプロジェクトを開く:
NuGet パッケージ マネージャーを開く:
ソリューションエクスプローラーでプロジェクトを右クリックします。
IronPDFのインストール
NuGetパッケージマネージャーで、「参照」タブに移動します。
IronPDFを検索。
検索結果から IronPDF パッケージを選択します。
これらの手順に従うことで、IronPDFがインストールされ、C#プロジェクトで使用できるようになります。その結果、強力なPDF操作機能を活用することができます。
using Humanizer;
using IronPdf;
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
List<string> content = GenerateHumanizedContent();
string htmlContent = "<h1>Humanizer Examples</h1><ul>";
// Iterate over each item in the List and add it to the HTML string
foreach (var item in content)
{
htmlContent += $"<li>{item}</li>";
}
htmlContent += "</ul>";
// Create a PDF from an HTML string using C#
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Export to a file or stream
pdf.SaveAs("output.pdf");
}
static List<string> GenerateHumanizedContent()
{
List<string> content = new List<string>();
// DateTime examples
DateTime pastDate = DateTime.Now.AddDays(-3);
DateTime futureDate = DateTime.Now.AddHours(5);
content.Add($"DateTime.Now: {DateTime.Now}");
content.Add($"3 days ago: {pastDate.Humanize()}");
content.Add($"In 5 hours: {futureDate.Humanize()}");
// TimeSpan examples
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
content.Add($"TimeSpan of 123 minutes: {timeSpan.Humanize()}");
// Number examples
int number = 12345;
content.Add($"Number 12345 in words: {number.ToWords()}");
content.Add($"Ordinal of 21: {21.ToOrdinalWords()}");
// Pluralization examples
string singular = "car";
content.Add($"Plural of 'car': {singular.Pluralize()}");
string plural = "children";
content.Add($"Singular of 'children': {plural.Singularize()}");
// Byte size examples
long bytes = 1048576;
content.Add($"1,048,576 bytes: {bytes.Bytes().Humanize()}");
return content;
}
}
using Humanizer;
using IronPdf;
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
List<string> content = GenerateHumanizedContent();
string htmlContent = "<h1>Humanizer Examples</h1><ul>";
// Iterate over each item in the List and add it to the HTML string
foreach (var item in content)
{
htmlContent += $"<li>{item}</li>";
}
htmlContent += "</ul>";
// Create a PDF from an HTML string using C#
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Export to a file or stream
pdf.SaveAs("output.pdf");
}
static List<string> GenerateHumanizedContent()
{
List<string> content = new List<string>();
// DateTime examples
DateTime pastDate = DateTime.Now.AddDays(-3);
DateTime futureDate = DateTime.Now.AddHours(5);
content.Add($"DateTime.Now: {DateTime.Now}");
content.Add($"3 days ago: {pastDate.Humanize()}");
content.Add($"In 5 hours: {futureDate.Humanize()}");
// TimeSpan examples
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
content.Add($"TimeSpan of 123 minutes: {timeSpan.Humanize()}");
// Number examples
int number = 12345;
content.Add($"Number 12345 in words: {number.ToWords()}");
content.Add($"Ordinal of 21: {21.ToOrdinalWords()}");
// Pluralization examples
string singular = "car";
content.Add($"Plural of 'car': {singular.Pluralize()}");
string plural = "children";
content.Add($"Singular of 'children': {plural.Singularize()}");
// Byte size examples
long bytes = 1048576;
content.Add($"1,048,576 bytes: {bytes.Bytes().Humanize()}");
return content;
}
}
Humanizerは、情報をユーザーフレンドリーで人間が読みやすい形式で表示するアプリケーションを作成しようとする.NET開発者にとって不可欠なライブラリです。 その幅広い機能、日付と時間のヒューマナイゼーションから数値および列挙型のフォーマットまで、はアプリケーションの使いやすさを向上させるための多用途なツールとなっています。 Humanizerを活用することで、開発者はカスタムフォーマットロジックの実装にかかる時間と労力を節約し、アプリケーションがエンドユーザーに対してデータをより効果的に伝えることを確実にすることができます。
同様に、IronPDFは包括的なPDF生成および操作機能を提供しており、C#プロジェクトでPDF文書を作成および管理するための優れた選択肢です。 HumanizerとIronPDFを組み合わせることで、.NETアプリケーションの機能とプレゼンテーションを大幅に向上させることができます。 IronPDFライセンスの詳細についてはIronPDFライセンス情報. さらに詳しくお知りになりたい方はHTMLからPDFへの変換に関する詳細なチュートリアル.