[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

iTextSharp .BinaryWrite Error?

Posted on 2011-04-26
6
Medium Priority
?
2,063 Views
Last Modified: 2012-05-11
I keep receiving this error when I don't comment-out "Response.BinaryWrite(output.ToArray());"...

        var document = new Document(PageSize.A4, 50, 50, 25, 25);
        MemoryStream output = new MemoryStream();
        var writer = PdfWriter.GetInstance(document, output);
        document.Open();

        //add text

        Response.Clear();
        //Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + orderNbr + ".pdf");
        Response.AddHeader("Content-Type", "application/pdf");
        Response.BinaryWrite(output.ToArray());
        Response.End();

Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near '%PDF-1.4
%¿¿4 0 obj
'.
0
Comment
Question by:pointeman
  • 3
  • 3
6 Comments
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35471961
I think that you need to use a separece request for that, maybe you can use a Handler (ashx file), like this:
<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler
{
    public void ProcessRequest(HttpContext context) {
        context.Response.ClearHeaders();
        context.Response.Cache.SetNoStore();
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.ContentType = "application/pdf";
        
        using (System.IO.MemoryStream ms = new System.IO.MemoryStream)
        {
            // Create your pdf doc.
            var doc1 = new Document();
            // Save it to a memory stream.
            PdfWriter.GetInstance(doc1, ms);
            doc1.Open();
            doc1.Add(new Paragraph("My first PDF"));
            doc1.Close();
    
            context.Response.BinaryWrite(ms);
        }
        
        context.Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Open in new window

0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35471992
What I see is that your response is begin made from a ajax asynchronous request, you need to send it to the main web browser window.
0
 

Author Comment

by:pointeman
ID: 35475270
>>ajax asynchronous request...

Q. How can you tell?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:pointeman
ID: 35475367
I found this concerning PageRequestManagerParserErrorException.

Yes I am using an updatePanel. I will place the 'PDF button' elsewhere on the web page.

http://weblogs.asp.net/leftslipper/archive/2007/02/26/sys-webforms-pagerequestmanagerparsererrorexception-what-it-is-and-how-to-avoid-it.aspx
0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 35475832
Exactly, that will fix your problem.

Q. How can you tell?
Because this:

Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near

That error comes from your web browser client logic, not your server, it is telling that don't understand that data, that is because it is expecting a valid JSON response from the server.
0
 

Author Closing Comment

by:pointeman
ID: 35477777
I should of guessed, had problems with UpdatePanel before concerning posting data.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question