Solution to the "Cannot access a closed Stream" Issue in the iTextSharp application (with how to display the PDF Stream on the page)

Source: Internet
Author: User

Suppose there is a piece of code: private void CreatePdf ()
{
Document doc = new Document ();
MemoryStream MS = new MemoryStream ();
Author writer = author writer. 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 ();
}

When CreatePdf () is called, the following error occurs:
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)
Line 59: {
Line 60: byte[] buffer=new byte[fs.Length];
Line 61: fs.Position=0;
Line 62: fs.Read(buffer,0,(int)fs.Length);


Where is the problem? From the error, I can know that the Stream we are preparing to operate has been disabled. This is because iTextSharp has automatically disabled the generated Stream. Is there any way to disable it?
After reading the following code, you may not need to say anything: 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;
Required contentbyte 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 ();
Author writer = author writer. 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 ();
}

It turns out that the familiarity with volume writer is that we can set whether to automatically disable Stream, which is disabled by default.

Updated 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=outposition ");
Response. AddHeader ("Content-Length", fs. Length. ToString ());
Response. ContentType = "application/pdf ";
Fs. Close ();

Response. BinaryWrite (buffer );
Response. OutputStream. Flush ();
Response. OutputStream. Close ();
}

If you need to download the file, instead of reading it in IE, delete the comments in front of Response. AddHeader ("Content-Disposition", "attachment?filename=out=") and replace the name you want.

If Response. addHeader ("Content-Length", fs. length. toString (); IE will output the webpage content. After the PDF file, the generated PDF file is damaged (this is a bug in IE, which I don't know)
If the attachment name is Chinese:
Response. AddHeader ("Content-Disposition", "attachment; FileName =" + HttpUtility. UrlEncode (" 文 "));

Embedded PDF
Response. AddHeader ("Content-Disposition", "inline?filename=out= ");

Search for more information on google.

Another information related to the Cache, Cache-Control, needs to be studied by yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.