假设有这么一段代码:
private void CreatePdf()
{
Document doc=new Document();
MemoryStream ms=new MemoryStream();
PdfWriter writer =PdfWriter.GetInstance(doc,ms);
doc.Open();
doc.Add(new Paragraph(DateTime.Now.ToLongDateString()));
doc.Close();
ViewPdf(ms);
}
private void ViewPdf(Stream fs)
{
byte[] buffer=new byte[fs.Length];
fs.Position=0;
fs.Read(buffer,0,(int)fs.Length);
Response.Clear();
Response.ContentType = "application/pdf";
Response.BinaryWrite(buffer);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
{
Document doc=new Document();
MemoryStream ms=new MemoryStream();
PdfWriter writer =PdfWriter.GetInstance(doc,ms);
doc.Open();
doc.Add(new Paragraph(DateTime.Now.ToLongDateString()));
doc.Close();
ViewPdf(ms);
}
private void ViewPdf(Stream fs)
{
byte[] buffer=new byte[fs.Length];
fs.Position=0;
fs.Read(buffer,0,(int)fs.Length);
Response.Clear();
Response.ContentType = "application/pdf";
Response.BinaryWrite(buffer);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
在调用CreatePdf()的时候碰到了如下错误:
Cannot access a closed Stream.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.ObjectDisposedException: Cannot access a closed Stream.
Source Error:
Line 58: private void ViewPdf(Stream fs) |
问题出在哪里了呢?从错误我可以知道我们准备操作的Stream已经关闭,这是因为iTextSharp自动关闭生成的Stream了,那有没有办法不关闭呢?
看了下面这段代码,也许就不用我说什么了:
private void Page_Load(object sender, System.EventArgs e)
{
//CreatePdf();
EditPDF();
}
private void EditPDF()
{
PdfReader reader =new PdfReader(@"e:\xml2PDF.pdf");
MemoryStream ms=new MemoryStream();
PdfStamper stamper=new PdfStamper(reader,ms);
stamper.Writer.CloseStream=false;
PdfContentByte cb=stamper.GetOverContent(1);
cb.Circle(250,250,50);
cb.SetColorFill(iTextSharp.text.Color.RED);
cb.SetColorStroke(iTextSharp.text.Color.WHITE);
cb.FillStroke();
stamper.Close();
ViewPdf(ms);
}
private void CreatePdf()
{
Document doc=new Document();
MemoryStream ms=new MemoryStream();
PdfWriter writer =PdfWriter.GetInstance(doc,ms);
writer.CloseStream=false;
doc.Open();
doc.Add(new Paragraph(DateTime.Now.ToLongDateString()));
doc.Close();
ViewPdf(ms);
}
private void ViewPdf(Stream fs)
{
byte[] buffer=new byte[fs.Length];
fs.Position=0;
fs.Read(buffer,0,(int)fs.Length);
fs.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.BinaryWrite(buffer);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
{
//CreatePdf();
EditPDF();
}
private void EditPDF()
{
PdfReader reader =new PdfReader(@"e:\xml2PDF.pdf");
MemoryStream ms=new MemoryStream();
PdfStamper stamper=new PdfStamper(reader,ms);
stamper.Writer.CloseStream=false;
PdfContentByte cb=stamper.GetOverContent(1);
cb.Circle(250,250,50);
cb.SetColorFill(iTextSharp.text.Color.RED);
cb.SetColorStroke(iTextSharp.text.Color.WHITE);
cb.FillStroke();
stamper.Close();
ViewPdf(ms);
}
private void CreatePdf()
{
Document doc=new Document();
MemoryStream ms=new MemoryStream();
PdfWriter writer =PdfWriter.GetInstance(doc,ms);
writer.CloseStream=false;
doc.Open();
doc.Add(new Paragraph(DateTime.Now.ToLongDateString()));
doc.Close();
ViewPdf(ms);
}
private void ViewPdf(Stream fs)
{
byte[] buffer=new byte[fs.Length];
fs.Position=0;
fs.Read(buffer,0,(int)fs.Length);
fs.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.BinaryWrite(buffer);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
原来PdfWriter有个熟悉就是让我们设置是否自动关闭Stream的,而默认是关闭的。
2006-12-01更新ViewPdf function
private void ViewPdf(Stream fs)
{
byte[] buffer = new byte[fs.Length];
fs.Position = 0;
fs.Read(buffer, 0, (int)fs.Length);
Response.Clear();
//Response.AddHeader("Content-Disposition", "attachment;FileName=out.pdf");
Response.AddHeader("Content-Length",fs.Length.ToString());
Response.ContentType = "application/pdf";
fs.Close();
Response.BinaryWrite(buffer);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
如果需要下载而不是在IE中看的话,请把Response.AddHeader("Content-Disposition", "attachment;FileName=out.pdf");前的注释去掉,并替换上你想要的名字。{
byte[] buffer = new byte[fs.Length];
fs.Position = 0;
fs.Read(buffer, 0, (int)fs.Length);
Response.Clear();
//Response.AddHeader("Content-Disposition", "attachment;FileName=out.pdf");
Response.AddHeader("Content-Length",fs.Length.ToString());
Response.ContentType = "application/pdf";
fs.Close();
Response.BinaryWrite(buffer);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
如果不指定Response.AddHeader("Content-Length",fs.Length.ToString());,IE会把网页内容也输出,在PDF后面,此时显示生成的PDF被破坏(这个说是IE的一个bug,我也不清楚)
如果附件名为中文:
Response.AddHeader("Content-Disposition", "attachment;FileName="+HttpUtility.UrlEncode("中文.pdf"));
内嵌显示PDF
Response.AddHeader("Content-Disposition", "inline;FileName=out.pdf");
更多这方面的信息google上搜索吧。
另外一个和缓存有关的信息,Cache-Control,需要的自己研究下吧。