从内存中打开PDF文档 [英] opening PDF document from memory
本文介绍了从内存中打开PDF文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以使用iTextSharp在内存中创建PDF文档,让用户可以选择打开或保存?如果打开则会在浏览器窗口中打开。
目前我唯一将它保存到磁盘。
编辑:
<好吧,我已经怀疑了。我最终不得不将文件写入文件夹,但它只是暂时的,因为每次都被覆盖。以下是它的价值解决方案:
private void GeneratePDF(){$ b
$ b var doc1 =新文件();
string path = Server.MapPath(〜/ pdfs /);
string filepath = path +Doc1.pdf;
PdfWriter.GetInstance(doc1,new FileStream(filepath,FileMode.Create));
doc1.Open();
doc1.Add(新段落(新文件));
doc1.Add(new Paragraph(DateTime.Now.ToString()));
doc1.Close();
Response.Buffer = false; // transmitfile self buffers
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType =application / pdf;
Response.AddHeader(Content-Disposition,attachment; filename = myPDF.pdf);
Response.TransmitFile(filepath);
Response.End();
}
解决方案
您可以将PDF保存到内存流并将其写入浏览器,如下所示。
protected void Page_Load (对象发送者,EventArgs e)
{
MemoryStream ms;
using(ms = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(myPdfDoc,ms);
Response.ContentType =application / pdf;
Response.OutputStream.Write(ms.GetBuffer(),0,ms.GetBuffer()。Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
}
It is possible to create a PDF document in memory with iTextSharp that gives the user a choice to "open" or "save"?, and if it opens then it opens in a browser window.
At the moment the only I have save it to disk.
EDIT:
ok I've got it sussed. I did end up having to write the file to a folder, but it is only temporary as gets overwritten every time. Here is the solution for what it's worth:
private void GeneratePDF() {
var doc1 = new Document();
string path = Server.MapPath("~/pdfs/");
string filepath = path + "Doc1.pdf";
PdfWriter.GetInstance(doc1, new FileStream(filepath, FileMode.Create));
doc1.Open();
doc1.Add(new Paragraph("A new Document"));
doc1.Add(new Paragraph(DateTime.Now.ToString()));
doc1.Close();
Response.Buffer = false; //transmitfile self buffers
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=myPDF.pdf");
Response.TransmitFile(filepath);
Response.End();
}
解决方案
You could save the PDF to a memorystream and write it out to the browser like this.
protected void Page_Load(object sender, EventArgs e)
{
MemoryStream ms;
using (ms = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(myPdfDoc, ms);
Response.ContentType = "application/pdf";
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
}
这篇关于从内存中打开PDF文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文