.NET ヘルプ

C# 文字列をバブルに変換(開発者向けの仕組み)

チペゴ
チペゴ・カリンダ
2025年4月3日
共有:

イントロダクション

吹き出しは、テキストを強調したり、ドキュメントに注釈を付けたり、PDFでコミック風の効果を作成するための素晴らしい方法です。 報告書にコメントを追加したり、指導ガイドを生成したり、インタラクティブなドキュメントを作成したりする場合、吹き出しはPDFの可読性と視覚的な魅力を向上させることができます。

この記事では、IronPDF を使用して、C# で文字列変数を吹き出しに変換する方法を探ります。 IronPDFは強力な.NETライブラリであり、HTMLとCSSをPDFに簡単に変換できるため、任意のC#文字列からスタイル付きの吹き出しを動的にレンダリングするのに理想的です。 さあ、始めましょう!

IronPDF: 強力な .NET PDF ライブラリ

C# 文字列をバブルに変換(開発者向けの動作方法):図1

Pixabay から追加アップロード

またはここに画像をドラッグアンドドロップします

画像の代替テキストを追加

では、なぜIronPDFなのか? IronPDFは、PDFファイルをプログラムで簡単に操作するために設計された強力なC#ライブラリです。 これにより、HTML画像DOCXファイル、その他から簡単にPDFドキュメントを生成できます。 あるいは、PDFのセキュリティを効率的かつ効果的に処理したり、既存のPDFドキュメントを編集するツールを探しているかもしれません。 どのようなタスクであっても、IronPDFが対応します。IronPDFは包括的なライブラリとして、PDF関連のほぼすべてのタスクにソリューションを提供し、サードパーティのライブラリを必要としません。

プロジェクトの設定

IronPDFのインストール

まず、NuGet を通じて IronPDF をインストールします。 Visual Studio でパッケージ マネージャー コンソールを開き、次を実行します:

Install-Package IronPdf
Install-Package IronPdf
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronPdf
$vbLabelText   $csharpLabel

または、Visual Studio の NuGet パッケージ マネージャー を通してインストールすることもできます。この場合、IronPDF を検索し、「インストール」をクリックしてください。

C# 文字列をバブルに変換(開発者向けの機能説明): 図2

Pixabay から追加アップロード

またはここに画像をドラッグアンドドロップします

画像の代替テキストを追加

インストールが完了したら、C# ファイルに次の名前空間が含まれていることを確認してください。

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

PDFにおける吹き出しの理解

スピーチバブルは通常、HTMLとCSSを使用して作成されます。 それらは、丸みを帯びた端を持つテキストコンテナと、話者の方を指している小さな尻尾で構成されています。 IronPDF を使用すると、これらの吹き出しを HTML 要素として生成し、PDF 内にレンダリングすることができます。

吹き出し用データ型の操作

文字列値の数値型への変換

時には、吹き出しの寸法を動的に設定するために、ユーザー入力をダブル値に変換する必要がある場合があります。 これを達成するために、parseメソッドを使用できます。

string widthInput = "150.5";
double bubbleWidth = double.Parse(widthInput);
string widthInput = "150.5";
double bubbleWidth = double.Parse(widthInput);
Dim widthInput As String = "150.5"
Dim bubbleWidth As Double = Double.Parse(widthInput)
$vbLabelText   $csharpLabel

これにより、ユーザー入力に基づいてバブルを動的にリサイズできます。

表示オプションにブール値を使用する

ブール値を使用して、吹き出しを表示するかどうかを切り替えることができます:

bool showBubble = true;
if (showBubble)
{
    Console.WriteLine("Speech bubble is visible");
}
bool showBubble = true;
if (showBubble)
{
    Console.WriteLine("Speech bubble is visible");
}
Dim showBubble As Boolean = True
If showBubble Then
	Console.WriteLine("Speech bubble is visible")
End If
$vbLabelText   $csharpLabel

IronPDFで文字列をスピーチバブルに変換

バブル用のHTMLテンプレートを作成する

IronPDFはHTMLからPDFへの変換をサポートしているため、HTMLとCSSを使用してシンプルな吹き出しを作成することができます。 文字列変数をPDFドキュメントに変換するには、まず新しいChromePdfRendererインスタンスを作成する必要があります。

using IronPdf;
class Program
{
    static void Main()
    {
        ChromePdfRenderer renderer = new ChromePdfRenderer();
        string htmlContent = "" +
    "<div class='bubble'>Hello, this is a speech bubble!</div>" +
    "<style>" +
    ".bubble { display: inline-block; background: #f0f0f0; border-radius: 10px; padding: 10px 15px; position: relative; font-family: Arial, sans-serif; }" +
    ".bubble::after { content: ''; position: absolute; bottom: -10px; left: 20px; border-width: 10px; border-style: solid; border-color: #f0f0f0 transparent transparent transparent; }" +
    "</style>";
        PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
        pdf.SaveAs("speechBubble.pdf");
    }
}
using IronPdf;
class Program
{
    static void Main()
    {
        ChromePdfRenderer renderer = new ChromePdfRenderer();
        string htmlContent = "" +
    "<div class='bubble'>Hello, this is a speech bubble!</div>" +
    "<style>" +
    ".bubble { display: inline-block; background: #f0f0f0; border-radius: 10px; padding: 10px 15px; position: relative; font-family: Arial, sans-serif; }" +
    ".bubble::after { content: ''; position: absolute; bottom: -10px; left: 20px; border-width: 10px; border-style: solid; border-color: #f0f0f0 transparent transparent transparent; }" +
    "</style>";
        PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
        pdf.SaveAs("speechBubble.pdf");
    }
}
Imports IronPdf
Friend Class Program
	Shared Sub Main()
		Dim renderer As New ChromePdfRenderer()
		Dim htmlContent As String = "" & "<div class='bubble'>Hello, this is a speech bubble!</div>" & "<style>" & ".bubble { display: inline-block; background: #f0f0f0; border-radius: 10px; padding: 10px 15px; position: relative; font-family: Arial, sans-serif; }" & ".bubble::after { content: ''; position: absolute; bottom: -10px; left: 20px; border-width: 10px; border-style: solid; border-color: #f0f0f0 transparent transparent transparent; }" & "</style>"
		Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
		pdf.SaveAs("speechBubble.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

PDF出力

C# 文字列をバブルに変換する(開発者のための作業方法):図3 - C# 文字列を吹き出しに変換したPDF出力

Pixabay から追加アップロード

またはここに画像をドラッグアンドドロップします

代替テキストをクリア

ご覧のとおり、PDFドキュメント内でスピーチバブルをレンダリングするために使用されるHTMLおよびCSSコンテンツを含む文字列変数を作成しました。 次に、ChromePdfRenderer クラスの RenderHtmlAsPdf メソッドを使用して、この文字列をPDFドキュメントとしてレンダリングし、保存します。

これらの手順に従うことで、テキスト「Hello, this is a speech bubble!」を含む新しいPDFドキュメントが生成され、単純な文字列からPDFを生成する基本を習得したことになります。

スピーチバブルのカスタマイズ

PDFに基本的な吹き出しを追加する以上のことをしたい場合はどうすればよいでしょうか。 CSSを使用して吹き出しをカスタマイズする方法を見てみましょう。 バブルの色、サイズ、位置は、CSSを調整することで変更できます。 背景色とテキストサイズを変更する例を示します:

.bubble {
  background: #ffcc00;
  color: #333;
  font-size: 16px;
}
.bubble {
  background: #ffcc00;
  color: #333;
  font-size: 16px;
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

動的なテキストが必要な場合は、静的なテキストをC#変数に置き換えることができ、最終的なコードは次のようになります:

using IronPdf;
class Program
{
    static void Main()
    {
        ChromePdfRenderer renderer = new ChromePdfRenderer();
        string userInput = "This is a custom speech bubble!";
        string dynamicHtml = $"<div class='bubble'>{userInput}</div>" + "<style>" + ".bubble {background: #ffcc00; color: #333; font-size: 16px; }" + "</style>";
        PdfDocument pdf = renderer.RenderHtmlAsPdf(dynamicHtml);
        pdf.SaveAs("speechBubble.pdf");
    }
}
using IronPdf;
class Program
{
    static void Main()
    {
        ChromePdfRenderer renderer = new ChromePdfRenderer();
        string userInput = "This is a custom speech bubble!";
        string dynamicHtml = $"<div class='bubble'>{userInput}</div>" + "<style>" + ".bubble {background: #ffcc00; color: #333; font-size: 16px; }" + "</style>";
        PdfDocument pdf = renderer.RenderHtmlAsPdf(dynamicHtml);
        pdf.SaveAs("speechBubble.pdf");
    }
}
Imports IronPdf
Friend Class Program
	Shared Sub Main()
		Dim renderer As New ChromePdfRenderer()
		Dim userInput As String = "This is a custom speech bubble!"
		Dim dynamicHtml As String = $"<div class='bubble'>{userInput}</div>" & "<style>" & ".bubble {background: #ffcc00; color: #333; font-size: 16px; }" & "</style>"
		Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(dynamicHtml)
		pdf.SaveAs("speechBubble.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

PDF出力

C# 文字列をバブルに変換 (開発者向けの仕組み): 図4 - カスタマイズされたPDF吹き出し出力

Pixabay から追加アップロード

またはここに画像をドラッグアンドドロップします

代替テキストをクリア

高度な機能

既存のPDFにバブルを重ねる

時には、新しいPDFを生成するのではなく、既存のPDFに吹き出しを追加したい場合があります。 IronPDFを使用すると、透かしの形でHTML要素を既存のPDFにオーバーレイできます。

using IronPdf;
class Program
{
    public static void Main()
    {
        PdfDocument pdf = PdfDocument.FromFile("existing.pdf");
        string newBubble = "<div class='bubble'>New Comment</div>" + "<style>" +
    ".bubble { display: inline-block; background: #f0f0f0; border-radius: 10px; padding: 10px 15px; position: relative; font-family: Arial, sans-serif; }" +
    ".bubble::after { content: ''; position: absolute; bottom: -10px; left: 20px; border-width: 10px; border-style: solid; border-color: #f0f0f0 transparent transparent transparent; }" +
    "</style>";
        pdf.ApplyWatermark(newBubble);
        pdf.SaveAs("updated.pdf");
    }
}
using IronPdf;
class Program
{
    public static void Main()
    {
        PdfDocument pdf = PdfDocument.FromFile("existing.pdf");
        string newBubble = "<div class='bubble'>New Comment</div>" + "<style>" +
    ".bubble { display: inline-block; background: #f0f0f0; border-radius: 10px; padding: 10px 15px; position: relative; font-family: Arial, sans-serif; }" +
    ".bubble::after { content: ''; position: absolute; bottom: -10px; left: 20px; border-width: 10px; border-style: solid; border-color: #f0f0f0 transparent transparent transparent; }" +
    "</style>";
        pdf.ApplyWatermark(newBubble);
        pdf.SaveAs("updated.pdf");
    }
}
Imports IronPdf
Friend Class Program
	Public Shared Sub Main()
		Dim pdf As PdfDocument = PdfDocument.FromFile("existing.pdf")
		Dim newBubble As String = "<div class='bubble'>New Comment</div>" & "<style>" & ".bubble { display: inline-block; background: #f0f0f0; border-radius: 10px; padding: 10px 15px; position: relative; font-family: Arial, sans-serif; }" & ".bubble::after { content: ''; position: absolute; bottom: -10px; left: 20px; border-width: 10px; border-style: solid; border-color: #f0f0f0 transparent transparent transparent; }" & "</style>"
		pdf.ApplyWatermark(newBubble)
		pdf.SaveAs("updated.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

PDF出力

C# 文字列を吹き出しに変換(開発者向けの仕組み):図 5 - 既存のPDFに吹き出しを追加した出力

Pixabay から追加アップロード

またはここに画像をドラッグアンドドロップします

代替テキストをクリア

上記のコード例でわかるように、まず既存のPDFドキュメントをPdfDocument.FromFile()で読み込み、そこに新しい吹き出しを追加する予定です。 次に、シンプルなHTMLとCSSを使用して、HTMLコンテンツのnewBubble文字列表現で吹き出しを作成しました。 最後に、この新しいバブルをPDFに適用するために必要だったのは、ApplyWatermark メソッドを利用することだけでした。

IronPDF の透かしツールのようなツールを使用すると、開発者は既存の PDF ドキュメントに HTML コンテンツを容易に適用できます。

データからスピーチバブルを生成する

ユーザー入力、データベース、またはAPIに基づいて動的に吹き出しを作成する必要がある場合は、データをループして複数の吹き出しを生成できます。

using IronPdf;
class Program
{
    static void Main()
    {
    ChromePdfRenderer renderer = new ChromePdfRenderer();
        List<string> messages = new List<string> { "Hello!", "How are you?", "This is IronPDF!" };
        string htmlBubbles = "";
        foreach (var msg in messages)
        {
            htmlBubbles += $"<div class='bubble'>{msg}</div>";
        }
        var pdf = renderer.RenderHtmlAsPdf(htmlBubbles);
        pdf.SaveAs("updated.pdf");
    }
}
using IronPdf;
class Program
{
    static void Main()
    {
    ChromePdfRenderer renderer = new ChromePdfRenderer();
        List<string> messages = new List<string> { "Hello!", "How are you?", "This is IronPDF!" };
        string htmlBubbles = "";
        foreach (var msg in messages)
        {
            htmlBubbles += $"<div class='bubble'>{msg}</div>";
        }
        var pdf = renderer.RenderHtmlAsPdf(htmlBubbles);
        pdf.SaveAs("updated.pdf");
    }
}
Imports IronPdf
Friend Class Program
	Shared Sub Main()
	Dim renderer As New ChromePdfRenderer()
		Dim messages As New List(Of String) From {"Hello!", "How are you?", "This is IronPDF!"}
		Dim htmlBubbles As String = ""
		For Each msg In messages
			htmlBubbles &= $"<div class='bubble'>{msg}</div>"
		Next msg
		Dim pdf = renderer.RenderHtmlAsPdf(htmlBubbles)
		pdf.SaveAs("updated.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

PDF出力

C# Convert String to Bubble(開発者向けの仕組み):図6 - データから吹き出しを生成するための出力PDFファイル

Pixabay から追加アップロード

またはここに画像をドラッグアンドドロップします

代替テキストをクリア

このコードは、foreachループを使用してリストから文字列を吹き出しに変換します。このようなメソッドを使用してPDFドキュメントの文字列を吹き出しに変換することで、チャットログや通知、あるいは自動レポートなどのデータを簡単に表示可能な吹き出しにすることができます。

カルチャー固有の書式情報の処理

ユーザー入力を解析する際には、特に数値に関して、文化特有の書式情報を考慮する必要があります。

using System.Globalization;
string value = "1,234.56";
double number = double.Parse(value, CultureInfo.InvariantCulture);
using System.Globalization;
string value = "1,234.56";
double number = double.Parse(value, CultureInfo.InvariantCulture);
Imports System.Globalization
Private value As String = "1,234.56"
Private number As Double = Double.Parse(value, CultureInfo.InvariantCulture)
$vbLabelText   $csharpLabel

これにより、地域設定に関係なく、一貫した数値フォーマットが保証されます。

吹き出し処理における整数値の使用

整数変数の割り当て

スピーチバブルのカウンターを格納するために、int変数を宣言できます。

int i = 0;
for (i = 0; i < 5; i++)
{
    Console.WriteLine($"Generating speech bubble {i + 1}");
}
int i = 0;
for (i = 0; i < 5; i++)
{
    Console.WriteLine($"Generating speech bubble {i + 1}");
}
Dim i As Integer = 0
For i = 0 To 4
	Console.WriteLine($"Generating speech bubble {i + 1}")
Next i
$vbLabelText   $csharpLabel

文字列を整数値に解析

文字列入力をintの結果に解析する必要がある場合、parseメソッドを使用できます。

string input = "42";
int result = int.Parse(input);
string input = "42";
int result = int.Parse(input);
Dim input As String = "42"
Dim result As Integer = Integer.Parse(input)
$vbLabelText   $csharpLabel

これにより、テキスト入力が有効な形式に変換され、使用可能な数値形式の変数として表されます。

スピーチバブルジェネレータクラスの作成

コードを整理するために、スピーチバブル生成のためのパブリッククラスを定義することができます。

public class SpeechBubbleGenerator
{
    public string GenerateBubble(string text)
    {
        return $"<div class='bubble'>{text}</div>";
    }
}
public class SpeechBubbleGenerator
{
    public string GenerateBubble(string text)
    {
        return $"<div class='bubble'>{text}</div>";
    }
}
Public Class SpeechBubbleGenerator
	Public Function GenerateBubble(ByVal text As String) As String
		Return $"<div class='bubble'>{text}</div>"
	End Function
End Class
$vbLabelText   $csharpLabel

このクラスを使用することで、複数の吹き出しを効率的に作成できます。

結論

吹き出しはPDFに明確さとスタイルを加え、注釈、コメント、インタラクティブなドキュメントに理想的です。 IronPDFを使用することで、C#を活用してカスタマイズと自動化を行いながら、HTMLとCSSでこれらのバブルを簡単に生成できます。 既存のPDFにオーバーレイする場合でも、動的なドキュメントを作成する場合でも、IronPDFは柔軟かつ効率的なアプローチを提供し、文字列をPDFドキュメントの読みやすい吹き出しに変換するのが簡単です。

.NETで強力なPDFソリューションをお探しなら、IronPDFを試して、動的で視覚的に魅力的なコンテンツでPDFを強化し始めましょう!

チペゴ
ソフトウェアエンジニア
チペゴは優れた傾聴能力を持ち、それが顧客の問題を理解し、賢明な解決策を提供する助けとなっています。彼は情報技術の学士号を取得後、2023年にIron Softwareチームに加わりました。現在、彼はIronPDFとIronOCRの2つの製品に注力していますが、顧客をサポートする新しい方法を見つけるにつれて、他の製品に関する知識も日々成長しています。Iron Softwareでの協力的な生活を楽しんでおり、さまざまな経験を持つチームメンバーが集まり、効果的で革新的な解決策を提供することに貢献しています。チペゴがデスクを離れているときは、良い本を楽しんだり、サッカーをしていることが多いです。
< 以前
HTMLプレティファイア(開発者向けの仕組み)
次へ >
C# Directory.GetFiles(動作の仕組み:開発者向けガイド)