C#で非同期およびマルチスレッドでPDFファイルを生成する方法

How to Generate PDFs with Async and Multithreading

This article was translated from English: Does it need improvement?
Translated
View the article in English

非同期とスレッディングは、IronPDFを使用してC#およびVB.NETで高性能のPDFをバッチで生成する際やパフォーマンスを最適化するために重要です。

クイックスタート: IronPDFでHTMLを非同期にPDF変換

IronPDFを使用してわずか数行のコードで非同期PDF生成を開始できます。

RenderHtmlAsPdfAsyncメソッドを使用して、HTMLコンテンツを効率的にPDFに変換し、アプリケーションのパフォーマンスを最適化できます。 このガイドでは、高性能なPDF生成のための非同期操作の力を活用する方法を示し、バッチ処理やマルチスレッド環境に最適です。 IronPDFの強力な機能の使いやすさと速度を体感してください。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var pdf = await IronPdf.ChromePdfRenderer.RenderHtmlAsPdfAsync("<h1>Hello World!</h1>");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

ミニマルワークフロー(5ステップ)

  1. NuGetからIronPDFをダウンロードして非同期およびマルチスレッドPDF生成を開始します
  2. 変換するHTMLコンテンツを準備します
  3. RenderHtmlAsPdfAsyncメソッドを使用してIronPDFでHTMLを非同期にPDFに変換します
  4. PDF処理におけるマルチスレッド処理のためにParallel.ForEachメソッドの利用を探ります
  5. 異なるPDF生成技術のパフォーマンス比較を確認します


非同期の例

IronPDFは、RenderHtmlAsPdfAsyncメソッドなどのレンダリングメソッドを使用して完全に非同期操作をサポートします。

:path=/static-assets/pdf/content-code-examples/how-to/async-async.cs
using IronPdf;
using System.Threading.Tasks;

// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

string[] htmlStrings = {"<h1>Html 1</h1>", "<h1>Html 2</h1>", "<h1>Html 3</h1>"};

// Create an array to store the tasks for rendering
var renderingTasks = new Task<PdfDocument>[htmlStrings.Length];

for (int i = 0; i < htmlStrings.Length; i++)
{
    int index = i; // Capturing the loop variable
    renderingTasks[i] = Task.Run(async () =>
    {
        // Render HTML to PDF
        return await renderer.RenderHtmlAsPdfAsync(htmlStrings[index]);
    });
}

// Wait for all rendering tasks to complete
// await Task.WhenAll(renderingTasks);
Imports IronPdf
Imports System.Threading.Tasks

' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()

Private htmlStrings() As String = {"<h1>Html 1</h1>", "<h1>Html 2</h1>", "<h1>Html 3</h1>"}

' Create an array to store the tasks for rendering
Private renderingTasks = New Task(Of PdfDocument)(htmlStrings.Length - 1){}

For i As Integer = 0 To htmlStrings.Length - 1
	Dim index As Integer = i ' Capturing the loop variable
	renderingTasks(i) = Task.Run(Async Function()
		' Render HTML to PDF
		Return Await renderer.RenderHtmlAsPdfAsync(htmlStrings(index))
	End Function)
Next i

' Wait for all rendering tasks to complete
' await Task.WhenAll(renderingTasks);
$vbLabelText   $csharpLabel

マルチスレッドの例

IronPDFはスレッドセーフであり、IronPdf.ChromePdfRendererレンダリングエンジンを使用する際にマルチスレッドをサポートします。ただし、マルチスレッドはmacOSマシンでは制限されています。

Parallel.ForEachパターンは、特にPDFのバッチ処理に便利です。

:path=/static-assets/pdf/content-code-examples/how-to/async-multi-thread.cs
using IronPdf;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;

var queue = new List<string>() { "<h1>Html 1</h1>", "<h1>Html 2</h1>", "<h1>Html 3</h1>" };

// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a list to store the rendered PDFs
List<PdfDocument> pdfResults = new List<PdfDocument>();

Parallel.ForEach(queue, html =>
{
    // Render HTML to PDF
    PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

    // You may choose to save the PDF to disk here if needed
    // For this example, we'll store it in the pdfResults list
    lock (pdfResults)
    {
        pdfResults.Add(pdf);
    }
});
Imports IronPdf
Imports System.Collections.Concurrent
Imports System.Collections.Generic
Imports System.Threading.Tasks

Private queue = New List(Of String)() From {"<h1>Html 1</h1>", "<h1>Html 2</h1>", "<h1>Html 3</h1>"}

' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()

' Create a list to store the rendered PDFs
Private pdfResults As New List(Of PdfDocument)()

Parallel.ForEach(queue, Sub(html)
	' Render HTML to PDF
	Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

	' You may choose to save the PDF to disk here if needed
	' For this example, we'll store it in the pdfResults list
	SyncLock pdfResults
		pdfResults.Add(pdf)
	End SyncLock
End Sub)
$vbLabelText   $csharpLabel

パフォーマンス比較

比較を行いましょう。 WaitForクラスによる複雑なHTMLレンダリングのシミュレーションで、5秒の遅延がレンダリングに追加されます。 以下は、上記のさまざまな技術を使用したパフォーマンスの比較表です。

通常レンダリング 非同期レンダリング マルチスレッドレンダリング
15.75秒 05.59秒 05.68秒

よくある質問

C#でHTMLを非同期でPDFに変換するにはどうすればよいですか?

IronPDFのRenderHtmlAsPdfAsyncメソッドを使用して、C#でHTMLを非同期でPDFに変換できます。これにより、メインスレッドをブロックせずに効率的なPDF生成が可能になります。

PDF処理にマルチスレッドを使用する利点は何ですか?

IronPDFを使用したPDF処理におけるマルチスレッドは、複数のPDF変換を同時に処理できるため、バッチ処理に理想的であり、複数のCPUコアを活用して性能を向上させます。

IronPDFは非同期およびマルチスレッドの両方のPDF生成に使用できますか?

はい、IronPDFは非同期およびマルチスレッドのPDF生成をサポートしています。非同期操作にはRenderHtmlAsPdfAsync、マルチスレッド処理にはParallel.ForEachを使用できます。

IronPDFはPDF生成においてスレッドセーフですか?

はい、IronPDFはスレッドセーフで、特にIronPdf.ChromePdfRendererレンダリングエンジンを使用するときに安全ですが、macOSにはいくつかの制限があります。

非同期PDF生成のパフォーマンスはマルチスレッド生成とどのように比較されますか?

パフォーマンス比較では、IronPDFを使用した非同期PDF生成は通常約5.59秒かかり、マルチスレッド生成は約5.68秒かかります。どちらも通常のレンダリング(15.75秒)よりもかなり速いです。

IronPDFを使用したPDF処理におけるマルチスレッドの実用的なユースケースは何ですか?

IronPDFを使用したマルチスレッドの実用的なユースケースはバッチ処理で、多数のHTMLファイルを同時にPDFに変換し、リソースの使用を最適化し、処理時間を短縮します。

MacOSでIronPDFを使用する際の特定の考慮事項はありますか?

IronPDFはマルチスレッドをサポートしていますが、macOSでこの機能を使用する際には制限が記録されています。特定の環境でマルチスレッドアプリケーションをテストおよび検証することが重要です。

IronPDF は .NET 10 と完全に互換性がありますか?

はい。IronPDF は .NET 10 をサポートしています。ランタイム、Web、デスクトップ、コンテナー化された環境を含む .NET 10 プロジェクトですぐに使用でき、.NET 10 で導入されたパフォーマンスと言語の改善の恩恵を受けることができます。
出典: IronPDF の機能ページ、.NET 10 の機能に関する記事。

Chipego
ソフトウェアエンジニア
Chipegoは、顧客の問題を理解し、知的な解決策を提供するのに役立つ、自然な聞くスキルを持っています。彼は2023年に情報技術の理学士を取得後、Iron Softwareチームに加わりました。IronPDFとIronOCRはChipegoが注力している2つの製品ですが、彼の全製品に対する知識は日々成長しています。彼は顧客をサポートする新しい方法を見つける中で、それを増やしています。Iron Softwareのコラボレーションの生活は、彼にとって非常に魅力的で、各部門のチームメンバーが多様な経験を持ち寄り、効果的で革新的なソリューションを提供しています。Chipegoは、デスクを離れると、良い本を読んだり、サッカーを楽しんだりすることがよくあります。
準備はいいですか?
Nuget ダウンロード 16,154,058 | バージョン: 2025.11 ただ今リリースされました