-
February 9th, 2010, 04:45 AM
#1
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>");
}
}
-
February 9th, 2010, 08:38 AM
#2
Re: Open PDF in new window
What does the error message say?
It's not a bug, it's a feature!
-
February 10th, 2010, 05:01 AM
#3
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
}
-
February 10th, 2010, 05:13 AM
#4
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!
-
February 10th, 2010, 06:21 AM
#5
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...
-
February 10th, 2010, 06:46 AM
#6
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!
-
February 10th, 2010, 08:58 AM
#7
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();
}
}
-
February 10th, 2010, 09:08 AM
#8
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!
-
February 10th, 2010, 09:28 AM
#9
Re: Open PDF in new window
Originally Posted by stepsh
This is my updated code:
So is it working now?
-
February 10th, 2010, 09:30 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|