PDFをMemoryStreamに変換 C

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

C# .NETでPDFをメモリーストリームにエクスポートする際、ファイルシステムに触れることなく行うことができます。 これは、System.IO .NETネームスペース内に存在するMemoryStreamオブジェクトを通じて可能です。

IronPDFを始めましょう

今日から無料トライアルでIronPDFをあなたのプロジェクトで使い始めましょう。

最初のステップ:
green arrow pointer



メモリにPDFを保存

IronPdf.PdfDocument は2つの方法のいずれかで直接メモリに保存できます:

:path=/static-assets/pdf/content-code-examples/how-to/pdf-to-memory-stream-to-stream.cs
using IronPdf;
using System.IO;

var renderer = new ChromePdfRenderer();

// Convert the URL into PDF
PdfDocument pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");

// Export PDF as Stream
MemoryStream pdfAsStream = pdf.Stream;

// Export PDF as Byte Array
byte[] pdfAsByte = pdf.BinaryData;
Imports IronPdf
Imports System.IO

Private renderer = New ChromePdfRenderer()

' Convert the URL into PDF
Private pdf As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/")

' Export PDF as Stream
Private pdfAsStream As MemoryStream = pdf.Stream

' Export PDF as Byte Array
Private pdfAsByte() As Byte = pdf.BinaryData
VB   C#

メモリからWebへPDFを提供

PDFをウェブで提供またはエクスポートするには、PDFファイルをHTMLの代わりにバイナリデータとして送信する必要があります。 詳しくはC# での PDF 文書のエクスポートと保存に関するガイド.

こちらは、MVCおよびASP.NETのための簡単な例です:

MVCでPDFをエクスポート

以下のコードスニペットのストリームは、IronPDFから取得されたバイナリデータです。 レスポンスのMIMEタイプは'application/pdf'で、ファイル名として'download.pdf'が指定されています。

return new FileStreamResult(pdfAsStream, "application/pdf")
{
    FileDownloadName = "download.pdf"
};
return new FileStreamResult(pdfAsStream, "application/pdf")
{
    FileDownloadName = "download.pdf"
};
Return New FileStreamResult(pdfAsStream, "application/pdf") With {.FileDownloadName = "download.pdf"}
VB   C#

ASP.NETでPDFをエクスポート

上記の例と同様に、ストリームはIronPDFから取得されたバイナリデータです。 レスポンスはクライアントに送信されるように設定され、フラッシュされます。

Response.Clear();

Response.ContentType = "application/octet-stream";

Context.Response.OutputStream.Write(pdfAsStream, 0, stream.Length);

Response.Flush();
Response.Clear();

Response.ContentType = "application/octet-stream";

Context.Response.OutputStream.Write(pdfAsStream, 0, stream.Length);

Response.Flush();
Response.Clear()

Response.ContentType = "application/octet-stream"

Context.Response.OutputStream.Write(pdfAsStream, 0, stream.Length)

Response.Flush()
VB   C#