• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2102
  • Last Modified:

iTextSharp .BinaryWrite Error?

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
pointeman
Asked:
pointeman
  • 3
  • 3
1 Solution
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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
 
pointemanAuthor Commented:
>>ajax asynchronous request...

Q. How can you tell?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
pointemanAuthor Commented:
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
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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
 
pointemanAuthor Commented:
I should of guessed, had problems with UpdatePanel before concerning posting data.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now