Re: Open PDF in new window
What does the error message say?
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
}
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.
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...
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.
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();
}
}
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
Re: Open PDF in new window
Quote:
Originally Posted by stepsh
This is my updated code:
So is it working now?
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...