public IHttpActionResult DownloadPDF()
{
var stream = CreatePdf();
return ResponseMessage(new HttpResponseMessage
{
Content = new StreamContent(stream)
{
Headers =
{
ContentType = new MediaTypeHeaderValue("application/pdf"),
ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "myfile.pdf"
}
}
},
StatusCode = HttpStatusCode.OK
});
}
Here is the CreatePdf method:
private Stream CreatePdf()
{
using (var document = new Document(PageSize.A4, 50, 50, 25, 25))
{
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
writer.CloseStream = false;
document.Open();
document.Add(new Paragraph("Hello World"));
document.Close();
output.Seek(0, SeekOrigin.Begin);
return output;
}
}
PDFをダウンロードできますが、コンテキストは空です。ここで私はメモリストリームを使用していると私はまた、それぞれのフォルダでファイルストリームのダウンロードを試みたが、私はダウンロードしたファイルを開くしようとするとまた、コンテンツが空です。誰かが私がここで逃しているものを助けることができますか?ここでWebAPI 2のitextsharpを使用してダウンロードしたPDFの空のコンテンツ
はまた、ドキュメントを閉じるドキュメント – Nkosi
を閉じる前に、ライターをフラッシュしてみなければなりません出力を返す前の最後のもの – Nkosi