PDFをMemoryStreamに変換 C
C# .NETでPDFをメモリーストリームにエクスポートする際、ファイルシステムに触れることなく行うことができます。 これは、System.IO .NETネームスペース内に存在するMemoryStreamオブジェクトを通じて可能です。
IronPDFを始めましょう
今日から無料トライアルでIronPDFをあなたのプロジェクトで使い始めましょう。
C#でMemoryStreamをPDFに変換する方法
- MemoryStreamをPDFに変換するためにIronPDF C#ライブラリをダウンロードしてください。
- 既存のPDFを読み込む PdfDocument オブジェクト
- URLやHTML文字列/ファイルから新しいPDFをレンダリングする
- PDFをストリームに変換するには
ストリーム
方法と バイナリデータ プロパティ - MVCやASP.NETを含むWebへのMemoryStreamの提供
メモリにPDFを保存
IronPdf.PdfDocument は2つの方法のいずれかで直接メモリに保存できます:
- IronPdf.PdfDocument.StreamPDFをSystem.IO.MemoryStreamとしてエクスポートします
- IronPdf.PdfDocument.BinaryDataPDF をバイト配列としてエクスポートします(バイト[])
: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
メモリから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"}
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()