C#でPDFをエクスポートする方法 | IronPDF

C# Export to PDF Code Example Tutorial

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

IronPDFは、HTMLをPDFとして保存するためにC#を使用できるC# PDFライブラリです。 また、C#/VB開発者がPDFドキュメントをプログラムで編集することも可能です。

クイックスタート: IronPDFを使用してHTMLをC#でPDFにエクスポート

IronPDFを使用して、C#でHTMLコンテンツを簡単にPDFにエクスポートします。 このクイックガイドは、HTMLをPDFドキュメントに変換して、わずか数行のコードで保存する方法を示しています。 IronPDFはPDF生成を簡素化し、開発者が手間をかけずにアプリケーションにPDFエクスポート機能を簡単に統合できるようにします。 さあ、どれほど簡単に始められるかを確認してください!

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.

    new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>HelloPDF</h1>").SaveAs("myExportedFile.pdf");
  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からC# PDFエクスポートライブラリをダウンロードしてインストール
  2. エクスポートしたPDFにデジタル署名を行うためのメソッドを発見するためにPdfDocumentのドキュメントを探索
  3. System.IO.MemoryStreamを使用してメモリにPDFを保存
  4. HTMLではなくバイナリデータとしてウェブにPDFを供給
  5. ファイルとしてPDFをエクスポート


PDFの保存オプション

PDFをディスクに保存する方法

PDFをディスクに保存するためにPdfDocument.SaveAs方法を使用します。

この方法がパスワード保護の追加をサポートしていることがわかります。 エクスポートしたPDFにデジタル署名を行う方法についてさらに学ぶために、次の記事を参照してください: 'PDFドキュメントに電子署名をする.'

C#でメモリーストリームにPDFファイルを保存する方法 (System.IO.MemoryStream)

IronPdf.PdfDocument.Streamプロパティは、System.IO.MemoryStreamを使用してメモリにPDFを保存します。

バイナリデータとして保存する方法

IronPdf.PdfDocument.BinaryDataプロパティは、PDFドキュメントをメモリ内のバイナリデータとしてエクスポートします。

これは、C#でbyte []として表現されるByteArrayとしてPDFを出力します。

ウェブサーバーからブラウザに供給する方法

ウェブにPDFを供給するには、それをHTMLではなくバイナリデータとして送信する必要があります。

MVC PDFエクスポート

// Sends 'stream' to the client as a file download with the specified name.
return new FileStreamResult(stream, "application/pdf")
{
    FileDownloadName = "file.pdf"
};
// Sends 'stream' to the client as a file download with the specified name.
return new FileStreamResult(stream, "application/pdf")
{
    FileDownloadName = "file.pdf"
};
' Sends 'stream' to the client as a file download with the specified name.
Return New FileStreamResult(stream, "application/pdf") With {.FileDownloadName = "file.pdf"}
$vbLabelText   $csharpLabel

ASP.NET PDFエクスポート

// Retrieves the PDF binary data
byte[] Binary = MyPdfDocument.BinaryData;

// Clears the existing response content
Response.Clear();

// Sets the response content type to 'application/octet-stream', suitable for PDF files
Response.ContentType = "application/octet-stream";

// Writes the binary data to the response output stream
Context.Response.OutputStream.Write(Binary, 0, Binary.Length);

// Flushes the response to send the data to the client
Response.Flush();
// Retrieves the PDF binary data
byte[] Binary = MyPdfDocument.BinaryData;

// Clears the existing response content
Response.Clear();

// Sets the response content type to 'application/octet-stream', suitable for PDF files
Response.ContentType = "application/octet-stream";

// Writes the binary data to the response output stream
Context.Response.OutputStream.Write(Binary, 0, Binary.Length);

// Flushes the response to send the data to the client
Response.Flush();
' Retrieves the PDF binary data
Dim Binary() As Byte = MyPdfDocument.BinaryData

' Clears the existing response content
Response.Clear()

' Sets the response content type to 'application/octet-stream', suitable for PDF files
Response.ContentType = "application/octet-stream"

' Writes the binary data to the response output stream
Context.Response.OutputStream.Write(Binary, 0, Binary.Length)

' Flushes the response to send the data to the client
Response.Flush()
$vbLabelText   $csharpLabel

よくある質問

C#でPDFをエクスポートする方法は?

C#でPDFをエクスポートするには、IronPDFライブラリを使用します。まず、NuGetからC# PDFエクスポートライブラリをダウンロードしてインストールします。その後、`PdfDocument`メソッドを探索して、PDFをディスクやメモリに保存し、ウェブブラウザに配信します。

IronPDFを使ってPDFを保存するためのオプションは何ですか?

IronPDFでは、いくつかの方法でPDFを保存できます:PdfDocument.SaveAsを使用してディスクに保存する、System.IO.MemoryStreamを使用してメモリに保存する、PdfDocument.BinaryDataを使用してバイナリデータとしてエクスポートする。

C#でPDFをディスクに保存するにはどうすればいいですか?

IronPDFのPdfDocument.SaveAsメソッドを使用して、PDFをディスクに保存します。このメソッドは、ドキュメントにパスワード保護を追加する機能もサポートしています。

C#でPDFをMemoryStreamに保存するにはどうすればいいですか?

IronPDFでは、IronPdf.PdfDocument.Streamプロパティを使用して、System.IO.MemoryStreamを利用してPDFをメモリに保存し、PDFデータを効率的に扱います。

C#でバイナリデータとしてPDFをエクスポートするにはどうすればいいですか?

IronPDFのPdfDocument.BinaryDataプロパティを使用すると、byte[]配列としてメモリ内のバイナリデータとしてPDFドキュメントをエクスポートできます。

IronPDFを使用して、ウェブサーバーからブラウザにPDFを配信するにはどうすればいいですか?

IronPDFを使用してウェブサーバーからブラウザにPDFを配信するには、バイナリデータとして送信する必要があります。MVCではFileStreamResultを使用し、ASP.NETではバイナリデータを直接レスポンスストリームに書き込みます。

IronPDFでPDFを扱う際のSystem.IO.MemoryStreamの役割は何ですか?

IronPDFではSystem.IO.MemoryStreamを使用してPDFファイルをメモリに保存し、即時のディスク保存を必要としないPDFデータの効率的な管理を実現します。

IronPDFを使ってMVCでPDFをファイルダウンロードとして送信する方法は?

MVCアプリケーションでは、FileStreamResultクラスを使用してPDFをファイルダウンロードとして送信できます。これにより、指定されたファイル名でクライアントにPDFデータをストリームします。

.NET 10 互換性: IronPDF は .NET 10 プロジェクトで完全に使用できますか?

はい。IronPDF は .NET 10 を完全にサポートしており、特別な構成や回避策を必要とせずに、プラットフォーム (Windows、Linux、macOS) 間で .NET 10 プロジェクトをすぐに使用できます。

Curtis Chau
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

によってレビュー

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 70

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 70
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

">

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 84

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 84
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once