CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2010
    Posts
    130

    Question Open PDF in new window

    Hello,

    I tried all the following methods to open my newly created PDF in a new window but none of them worked! I added break points on all the statements in the "finally" block and managed to successfully jump to the end before then being redirected to my error page. Can you spot the mistake?

    Code:
    public void CreatePDF()
            {
                doc = new Document(PageSize.A4, 80, 50, 30, 65);
                string PDFpath = Server.MapPath("~/test.pdf");
    
                try
                {
                    FileStream stream = new FileStream(PDFpath, FileMode.Create);
                    writer = PdfWriter.GetInstance(doc, stream);
                    doc.Open();
    
                    FormatPDF();
    
                    // First read the PDF before stamping it
                    PdfReader pdfReader = new PdfReader(Request.MapPath("~/test.pdf"));
                    PdfStamper pdfStamper = new PdfStamper(pdfReader, Response.OutputStream);
                    pdfStamper.FormFlattening = true;
    
                    // Close the PDFStamper to enable the PDF to stream
                    pdfStamper.Close();
                }
                catch (Exception)
                {
                    SData.sLatestErr = Convert.ToString(Server.GetLastError());
                    Response.Redirect("Error.aspx", true);
                }
                finally
                {
                    doc.Close();
                    Response.Redirect("~/test.pdf");
    
                    // TRY SAVING DATA
    
    
                    // To open in a new window
    
                    // METHOD 1
                    //Response.Write("<script>");
                    //Response.Write("window.open('~/test.pdf','_blank')");
                    //Response.Write("</script>"); 
                    //Response.Redirect("");
    
                    // METHOD 2
                    //Response.Write("<script type='text/javascript'>");
                    //Response.Write("window.open('test.pdf');");
                    //Response.Write("</script>");
    
                    // METHOD 3
                    //string url = "test.pdf";
                    //StringBuilder sb = new StringBuilder();
                    //sb.Append("<script type = 'text/javascript'>");
                    //sb.Append("window.open('");
                    //sb.Append(url);
                    //sb.Append("');");
                    //sb.Append("</script>");
                    //ClientScript.RegisterStartupScript(this.GetType(),"script", sb.ToString());
    
                    // METHOD 4
                    //Response.Write("<script type='text/javascript'>detailedresults=window.open('~/test.pdf');</script>");
                }
            }

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Open PDF in new window

    What does the error message say?
    It's not a bug, it's a feature!

  3. #3
    Join Date
    Jan 2010
    Posts
    130

    Question Re: Open PDF in new window

    Opening a new window might not have been a problem afterall...

    On debugging I receive an exception message stating that the directory path could not be found. The problem lies here:

    Code:
     stream = new FileStream(PDFpath, FileMode.Create);
    Do you think my file path could be too long? Or that it didnt properly dispose of the document the previous time? I added the following to my code to tackle this:

    Code:
       finally
                {
                    // Try
                    stream.Close();
                    stream.Dispose();
    
                    doc.Close();
                    
                    // Response Redirect new window code comes here
                 }

  4. #4
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Open PDF in new window

    You could put the stream object inside a using block to correct this:

    Code:
    using(stream = new FileStream(PDFpath, FileMode.Create))
    {
       //do stuff
    }
    This will automatically close and dispose the stream, thus flushing the contents to the filesystem.
    It's not a bug, it's a feature!

  5. #5
    Join Date
    Jan 2010
    Posts
    130

    Re: Open PDF in new window

    Thanks, that seems to have solved something. I however still receive an IO error message stating that it cannot access my data because it is being used by another process. I have changed the name of the filepath multiple times to check that I dont have a second existing file with a similar name but that didnt help...

  6. #6
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Open PDF in new window

    Is the error message regarding the PDF file?
    If so, I'd recommend using a tool like Process Explorer from SysInternals to see which process has a handle to the file. If the process is your application, you'll need to go over the code that handles this file to make sure there are no open handles when trying to access it.
    It's not a bug, it's a feature!

  7. #7
    Join Date
    Jan 2010
    Posts
    130

    Re: Open PDF in new window

    I'll keep that in mind.

    The cause of this error message was that I didnt close the document before I read it. My original problem however remains: that I cannot open the PDF in a new window. Instead a dialog box appears stating that the data is being downloaded...

    This is my updated code:
    Code:
    public void CreatePDF()
            {
                //Document is inbuilt class, available in iTextSharp
                doc = new Document(PageSize.A4, 80, 50, 30, 65);
    
                // Create PDF on current path with doc name "test.pdf"
                string PDFpath = Server.MapPath("~/pdf/AzubiBeurteilungsbogen.pdf");
    
                using (stream = new FileStream(PDFpath, FileMode.Create))
                {
                    writer = PdfWriter.GetInstance(doc, stream);
                    doc.Open();
    
                    FormatPDF();
                    doc.Close();
    
                    // First read the PDF before stamping it
                    PdfReader pdfReader = new PdfReader(Request.MapPath("~/pdf/AzubiBeurteilungsbogen.pdf"));
    
                    // Determine page number
                    int pageNumber = 1;
                    Rectangle size = pdfReader.GetPageSizeWithRotation(pageNumber);
    
                    // Ensure the PDF stays flat, i.e. read only
                    PdfStamper pdfStamper = new PdfStamper(pdfReader, Response.OutputStream);
                    pdfStamper.FormFlattening = true;
    
                    // Close the PDFStamper to enable the PDF to stream
                    pdfStamper.Close();
    
                    // To open in a new window
                    Response.Write("<script type='text/javascript'>");
                    Response.Write("window.open('/pdf/AzubiBeurteilungsbogen.pdf','_blank')");
                    Response.Write("</script>");
    
                    // For security
                    stream.Close();
                    stream.Dispose();
                }
            }

  8. #8
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Open PDF in new window

    Javascript really isn't my ballgame ... neither is ASP.NET, actually

    I'm sure one of the gurus will be able to help though
    It's not a bug, it's a feature!

  9. #9
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Open PDF in new window

    Quote Originally Posted by stepsh
    This is my updated code:
    So is it working now?

  10. #10
    Join Date
    Jan 2010
    Posts
    130

    Re: Open PDF in new window

    Unfortunately not... It's strange that I am receiving a dialog box stating it's DOWNLOADING the data when I just want to open it...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured