IronPDF ハウツー PDFドキュメントの保存とエクスポート C# PDF にエクスポートするコード例チュートリアル カーティス・チャウ 更新日:2026年1月10日 IronPDF をダウンロード NuGet ダウンロード DLL ダウンロード Windows 版 無料トライアル LLM向けのコピー LLM向けのコピー LLM 用の Markdown としてページをコピーする ChatGPTで開く このページについてChatGPTに質問する ジェミニで開く このページについてGeminiに問い合わせる Grokで開く このページについてGrokに質問する 困惑の中で開く このページについてPerplexityに問い合わせる 共有する Facebook で共有 Xでシェア(Twitter) LinkedIn で共有 URLをコピー 記事をメールで送る This article was translated from English: Does it need improvement? Translated View the article in English IronPDF を使用して、BinaryData などの単純なメソッドで C# で HTML コンテンツを PDF にエクスポートします。 このC# PDFライブラリは、開発者がプログラムでHTMLをPDF文書に変換し、Webブラウザに提供したり、ディスクに保存したりすることを可能にします。 IronPDFは、HTMLをPDFとして保存するためにC#を使用できるC# PDFライブラリです。 また、C#/VB開発者がPDFドキュメントをプログラムで編集することも可能です。 レポートの作成、請求書の作成、ウェブページの変換など、IronPDFはC#アプリケーションでのPDF生成のための堅牢なソリューションを提供します。 クイックスタート: IronPDFを使用して C# で HTML を PDF にエクスポートする IronPDFを使用してC#でHTMLコンテンツをPDFにエクスポートします。 このガイドでは、わずか数行のコードでHTMLをPDF文書に変換して保存する方法を紹介します。 IronPDFはPDF生成を簡素化し、開発者がPDFエクスポート機能をアプリケーションに統合することを可能にします。 IronPDF をNuGetパッケージマネージャでインストール PM > Install-Package IronPdf このコード スニペットをコピーして実行します。 new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>HelloPDF</h1>").SaveAs("myExportedFile.pdf"); 実際の環境でテストするためにデプロイする 今日プロジェクトで IronPDF を使い始めましょう無料トライアル Free 30 Day Trial 最小限のワークフロー(5ステップ) NuGetからC# PDFエクスポート・ライブラリをダウンロードしてインストールする。 デジタル署名の方法については、`PdfDocument`ドキュメントを参照してください。 `System.IO.MemoryStream`を使用してPDFをメモリに保存します。 PDFをHTMLではなくバイナリデータとしてWebに提供する。 PDFをファイルとしてエクスポートする PDFを保存するためのさまざまなオプションは何ですか? C#でPDFドキュメントを扱う場合、IronPDFは生成したPDFを保存したりエクスポートするための複数のオプションを提供します。 それぞれのメソッドは、単純なファイルストレージからウェブアプリケーションでのPDF提供まで、異なるユースケースに対応しています。 以下のセクションでは、C#でPDFをエクスポートして保存するために利用可能なオプションについて説明します。 PDFをディスクに保存する方法 PDF をディスクに保存するには、 PdfDocument.SaveAsメソッドを使用します。 これは、デスクトップアプリケーションや、PDFをサーバーに恒久的に保存する必要がある場合に、最も簡単なアプローチです。 // Complete example for saving PDF to disk using IronPdf; // Initialize the Chrome PDF renderer var renderer = new ChromePdfRenderer(); // Create HTML content with styling string htmlContent = @" <html> <head> <style> body { font-family: Arial, sans-serif; margin: 40px; } h1 { color: #333; } .content { line-height: 1.6; } </style> </head> <body> <h1>Invoice #12345</h1> <div class='content'> <p>Date: " + DateTime.Now.ToString("yyyy-MM-dd") + @"</p> <p>Thank you for your business!</p> </div> </body> </html>"; // Render HTML to PDF PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent); // Save to disk with standard method pdf.SaveAs("invoice_12345.pdf"); // Save with password protection for sensitive documents pdf.Password = "secure123"; pdf.SaveAs("protected_invoice_12345.pdf"); // Complete example for saving PDF to disk using IronPdf; // Initialize the Chrome PDF renderer var renderer = new ChromePdfRenderer(); // Create HTML content with styling string htmlContent = @" <html> <head> <style> body { font-family: Arial, sans-serif; margin: 40px; } h1 { color: #333; } .content { line-height: 1.6; } </style> </head> <body> <h1>Invoice #12345</h1> <div class='content'> <p>Date: " + DateTime.Now.ToString("yyyy-MM-dd") + @"</p> <p>Thank you for your business!</p> </div> </body> </html>"; // Render HTML to PDF PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent); // Save to disk with standard method pdf.SaveAs("invoice_12345.pdf"); // Save with password protection for sensitive documents pdf.Password = "secure123"; pdf.SaveAs("protected_invoice_12345.pdf"); $vbLabelText $csharpLabel この方法は、パスワード保護の追加をサポートしています。 エクスポートしたPDFへのデジタル署名については、以下の記事をご覧ください:Digitally Sign a PDF Document'.その他のセキュリティ オプションについては、PDFの権限とパスワードに関するガイドをご覧ください。 C# で PDF ファイルを MemoryStream に保存する方法 (System.IO.MemoryStream) System.IO.MemoryStream を使用して PDF をメモリに保存します。 このアプローチは、PDFデータをメモリ内で操作したり、一時ファイルを作成せずに他のメソッドに渡したりする必要がある場合に最適です。 PDFメモリストリームの操作については、こちらをご覧ください。 // Example: Save PDF to MemoryStream using IronPdf; using System.IO; var renderer = new ChromePdfRenderer(); // Render HTML content PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Monthly Report</h1><p>Sales figures...</p>"); // Get the PDF as a MemoryStream MemoryStream stream = pdf.Stream; // Example: Upload to cloud storage or database // UploadToCloudStorage(stream); // Example: Email as attachment without saving to disk // EmailService.SendWithAttachment(stream, "report.pdf"); // Remember to dispose of the stream when done stream.Dispose(); // Example: Save PDF to MemoryStream using IronPdf; using System.IO; var renderer = new ChromePdfRenderer(); // Render HTML content PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Monthly Report</h1><p>Sales figures...</p>"); // Get the PDF as a MemoryStream MemoryStream stream = pdf.Stream; // Example: Upload to cloud storage or database // UploadToCloudStorage(stream); // Example: Email as attachment without saving to disk // EmailService.SendWithAttachment(stream, "report.pdf"); // Remember to dispose of the stream when done stream.Dispose(); $vbLabelText $csharpLabel バイナリデータとして保存する方法 IronPdf.PdfDocument.BinaryDataプロパティは、PDF ドキュメントをメモリ内のバイナリ データとしてエクスポートします。 これは、データベース・ストレージや、バイト配列を必要とするAPIと統合する場合に特に役立ちます。 これにより、PDF は ByteArray として出力されます。これは、C# では byte [] と表現されます。 // Example: Convert PDF to binary data using IronPdf; var renderer = new ChromePdfRenderer(); // Configure rendering options for better quality renderer.RenderingOptions = new ChromePdfRenderOptions() { MarginTop = 20, MarginBottom = 20, MarginLeft = 10, MarginRight = 10, PaperSize = IronPdf.Rendering.PdfPaperSize.A4 }; // Render content to PDF PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Contract Document</h1>"); // Get binary data byte[] binaryData = pdf.BinaryData; // Example: Store in database // database.StorePdfDocument(documentId, binaryData); // Example: Send via API // apiClient.UploadDocument(binaryData); // Example: Convert PDF to binary data using IronPdf; var renderer = new ChromePdfRenderer(); // Configure rendering options for better quality renderer.RenderingOptions = new ChromePdfRenderOptions() { MarginTop = 20, MarginBottom = 20, MarginLeft = 10, MarginRight = 10, PaperSize = IronPdf.Rendering.PdfPaperSize.A4 }; // Render content to PDF PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Contract Document</h1>"); // Get binary data byte[] binaryData = pdf.BinaryData; // Example: Store in database // database.StorePdfDocument(documentId, binaryData); // Example: Send via API // apiClient.UploadDocument(binaryData); $vbLabelText $csharpLabel バイナリデータ操作を含むより高度なシナリオについては、PDFをMemoryStreamに変換するガイドを参照してください。 ウェブサーバーからブラウザに供給する方法 ウェブにPDFを供給するには、それをHTMLではなくバイナリデータとして送信する必要があります。 これは、ユーザーがブラウザで直接PDFをダウンロードしたり閲覧したりする必要があるウェブアプリケーションには不可欠です。 IronPdfはMVCと従来のASP.NETアプリケーションの両方に統合されています。 MVC PDFエクスポート 最新の MVC アプリケーションでは、FileStreamResult を使用して PDF を簡単に提供できます。 このアプローチは、ASP.NET Core MVCアプリケーションと相性が良いです: // MVC Controller method for PDF export public IActionResult DownloadInvoice(int invoiceId) { // Generate your HTML content string htmlContent = GenerateInvoiceHtml(invoiceId); // Create PDF using IronPDF var renderer = new ChromePdfRenderer(); PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent); // Get the PDF stream MemoryStream stream = pdf.Stream; // Reset stream position stream.Position = 0; // Return file to browser - will prompt download return new FileStreamResult(stream, "application/pdf") { FileDownloadName = $"invoice_{invoiceId}.pdf" }; } // Alternative: Display PDF in browser instead of downloading public IActionResult ViewInvoice(int invoiceId) { var renderer = new ChromePdfRenderer(); PdfDocument pdf = renderer.RenderHtmlAsPdf(GenerateInvoiceHtml(invoiceId)); // Return PDF for browser viewing return File(pdf.BinaryData, "application/pdf"); } // MVC Controller method for PDF export public IActionResult DownloadInvoice(int invoiceId) { // Generate your HTML content string htmlContent = GenerateInvoiceHtml(invoiceId); // Create PDF using IronPDF var renderer = new ChromePdfRenderer(); PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent); // Get the PDF stream MemoryStream stream = pdf.Stream; // Reset stream position stream.Position = 0; // Return file to browser - will prompt download return new FileStreamResult(stream, "application/pdf") { FileDownloadName = $"invoice_{invoiceId}.pdf" }; } // Alternative: Display PDF in browser instead of downloading public IActionResult ViewInvoice(int invoiceId) { var renderer = new ChromePdfRenderer(); PdfDocument pdf = renderer.RenderHtmlAsPdf(GenerateInvoiceHtml(invoiceId)); // Return PDF for browser viewing return File(pdf.BinaryData, "application/pdf"); } $vbLabelText $csharpLabel ASP.NET PDFエクスポート 従来のASP.NET WebFormsアプリケーションでは、Responseオブジェクトを通して直接PDFを提供することができます: // ASP.NET WebForms PDF export protected void ExportButton_Click(object sender, EventArgs e) { // Create your PDF document var renderer = new ChromePdfRenderer(); // Configure rendering options renderer.RenderingOptions = new ChromePdfRenderOptions() { PaperSize = IronPdf.Rendering.PdfPaperSize.Letter, PrintHtmlBackgrounds = true, CreatePdfFormsFromHtml = true }; // Generate PDF from current page or custom HTML PdfDocument MyPdfDocument = renderer.RenderHtmlAsPdf(GetReportHtml()); // 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"; // Add content disposition header for download Response.AddHeader("Content-Disposition", "attachment; filename=report_" + DateTime.Now.ToString("yyyyMMdd") + ".pdf"); // 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(); // End the response Response.End(); } // ASP.NET WebForms PDF export protected void ExportButton_Click(object sender, EventArgs e) { // Create your PDF document var renderer = new ChromePdfRenderer(); // Configure rendering options renderer.RenderingOptions = new ChromePdfRenderOptions() { PaperSize = IronPdf.Rendering.PdfPaperSize.Letter, PrintHtmlBackgrounds = true, CreatePdfFormsFromHtml = true }; // Generate PDF from current page or custom HTML PdfDocument MyPdfDocument = renderer.RenderHtmlAsPdf(GetReportHtml()); // 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"; // Add content disposition header for download Response.AddHeader("Content-Disposition", "attachment; filename=report_" + DateTime.Now.ToString("yyyyMMdd") + ".pdf"); // 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(); // End the response Response.End(); } $vbLabelText $csharpLabel 高度なエクスポート シナリオ バッチ PDF エクスポート 複数のPDFを扱う場合、エクスポートプロセスを最適化することができます: // Batch export multiple PDFs to a zip file public void ExportMultiplePdfsAsZip(List<string> htmlDocuments, string zipFilePath) { using (var zipArchive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create)) { var renderer = new ChromePdfRenderer(); for (int i = 0; i < htmlDocuments.Count; i++) { // Render each HTML document PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlDocuments[i]); // Add to zip archive var entry = zipArchive.CreateEntry($"document_{i + 1}.pdf"); using (var entryStream = entry.Open()) { pdf.Stream.CopyTo(entryStream); } } } } // Batch export multiple PDFs to a zip file public void ExportMultiplePdfsAsZip(List<string> htmlDocuments, string zipFilePath) { using (var zipArchive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create)) { var renderer = new ChromePdfRenderer(); for (int i = 0; i < htmlDocuments.Count; i++) { // Render each HTML document PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlDocuments[i]); // Add to zip archive var entry = zipArchive.CreateEntry($"document_{i + 1}.pdf"); using (var entryStream = entry.Open()) { pdf.Stream.CopyTo(entryStream); } } } } $vbLabelText $csharpLabel ユーザー権限に基づく条件付きエクスポート // Export with different options based on user role public byte[] ExportPdfWithPermissions(string htmlContent, UserRole userRole) { var renderer = new ChromePdfRenderer(); PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent); // Apply security based on user role if (userRole == UserRole.Guest) { // Restrict printing and copying for guests pdf.SecuritySettings.AllowUserPrinting = false; pdf.SecuritySettings.AllowUserCopyPasteContent = false; } else if (userRole == UserRole.Standard) { // Allow printing but not editing pdf.SecuritySettings.AllowUserPrinting = true; pdf.SecuritySettings.AllowUserEditing = false; } return pdf.BinaryData; } // Export with different options based on user role public byte[] ExportPdfWithPermissions(string htmlContent, UserRole userRole) { var renderer = new ChromePdfRenderer(); PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent); // Apply security based on user role if (userRole == UserRole.Guest) { // Restrict printing and copying for guests pdf.SecuritySettings.AllowUserPrinting = false; pdf.SecuritySettings.AllowUserCopyPasteContent = false; } else if (userRole == UserRole.Standard) { // Allow printing but not editing pdf.SecuritySettings.AllowUserPrinting = true; pdf.SecuritySettings.AllowUserEditing = false; } return pdf.BinaryData; } $vbLabelText $csharpLabel PDFエクスポートのベストプラクティス 本番アプリケーションでPDFをエクスポートする場合は、以下のベストプラクティスを考慮してください: 1.メモリ管理:大きなPDFやトラフィックの多いアプリケーションでは、メモリリークを防ぐためにPDFオブジェクトやストリームを適切に破棄してください。 パフォーマンスを向上させるには、async メソッドの使用を検討してください。 2.エラー処理:PDFをエクスポートするときは、特にネットワークの問題が発生する可能性のあるWebアプリケーションでは、常に適切なエラー処理を実装してください。 3.圧縮:大きなPDFの場合は、PDF圧縮を使用して、ユーザーに提供する前にファイルサイズを小さくしてください。 4.メタデータ: より良い文書管理のために、タイトル、作成者、作成日などの適切なPDFメタデータを設定します。 5.クロスプラットフォーム互換性:エクスポート機能が異なるプラットフォーム間で動作するようにします。 IronPDF は、Linux、および macOS をサポートしています。 結論 IronPDFは単純なファイル保存から複雑なウェブサーバーシナリオまで、C#アプリケーションでPDFをエクスポートするための包括的なオプションを提供します。 ユースケースに適したエクスポート方法を使用することで、セキュリティとパフォーマンスの基準を維持しながら、PDFドキュメントを効率的に生成し、ユーザーに配信することができます。 よくある質問 C#でHTMLコンテンツをPDFにエクスポートするにはどうすればよいですか? IronPDFのChromePdfRendererクラスを使ってC#でHTMLをPDFにエクスポートすることができます。レンダラーのインスタンスを作成し、RenderHtmlAsPdf()メソッドを使ってHTMLコンテンツを変換し、SaveAs()メソッドを使って保存するだけです。IronPDFはHTML文字列、ファイル、URLを直接PDFドキュメントに変換することを容易にします。 C#を使用してPDFを保存するには、どのような方法がありますか? IronPDFはPDFを保存するための複数のメソッドを提供します:ディスクに保存するSaveAs()、Webアプリケーションで一時ファイルを作成せずにPDFを提供するStream、そしてバイト配列としてPDFを取得するBinaryDataです。IronPDFの各メソッドは、単純なファイル保存から動的なウェブ配信まで、異なるユースケースに対応します。 PDFをディスクではなくメモリに保存できますか? はい、IronPDFはSystem.IO.MemoryStreamを使用してPDFをメモリに保存することができます。これはサーバー上に一時ファイルを作成することなくPDFを直接ユーザーに提供したいウェブアプリケーションに便利です。Streamプロパティを使用するか、PDFをバイナリデータに変換することができます。 PDFを保存するときにパスワード保護を追加する方法を教えてください。 IronPDFは保存前にPdfDocumentオブジェクトにPasswordプロパティを設定することでパスワード保護を可能にします。パスワード文字列をpdf.Passwordに代入し、SaveAs()を使用して開くためにパスワードを必要とする保護されたPDFファイルを作成するだけです。 PDFをディスクに保存せずに、ウェブブラウザに直接提供することはできますか? はい、IronPDFはPDFをバイナリデータとして直接ウェブブラウザに提供することができます。BinaryDataプロパティを使ってPDFをバイト配列として取得し、Webアプリケーションのレスポンスストリームを通して提供することができます。 1行でHTMLをPDFに変換して保存する最も簡単な方法は何ですか? IronPDFは1行で解決できるソリューションを提供します: new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("Your HTML").SaveAs("output.pdf").これはレンダラーを作成し、HTMLをPDFに変換し、ディスクに保存します。 カーティス・チャウ 今すぐエンジニアリングチームとチャット テクニカルライター Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。 レビュー済み Jeffrey T. Fritz プリンシパルプログラムマネージャー - .NETコミュニティチーム Jeffはまた、.NETとVisual Studioチームのプリンシパルプログラムマネージャーです。彼は.NET Conf仮想会議シリーズのエグゼクティブプロデューサーであり、週に二回放送される開発者向けライブストリーム『Fritz and Friends』のホストを務め、テクノロジーについて話すことや視聴者と一緒にコードを書くことをしています。Jeffはワークショップ、プレゼンテーション、およびMicrosoft Build、Microsoft Ignite、.NET Conf、Microsoft MVPサミットを含む最大のMicrosoft開発者イベントのコンテンツを企画しています。 準備はできましたか? Nuget ダウンロード 17,803,474 | バージョン: 2026.3 リリース 無料トライアル NuGet 無料版 総ダウンロード数: 17,803,474 ライセンスを見る まだスクロールしていますか? すぐに証拠が欲しいですか? PM > Install-Package IronPdf サンプルを実行するHTML が PDF に変換されるのを確認します。 NuGet 無料版 総ダウンロード数: 17,803,474 ライセンスを見る