[RESOLVED] PDF cannot be read correctly!
I create a PDF and save it in my database in the form of a byte[] through the use of a webservice. I then load the byte[] and generate a PDF. A new window opens but I get a message from Acrobat Reader saying the PDF could not be read correctly. I am not sure if the mistake lies in saving the PDF as a byte[] or in reading it....
This is how I save the PDF as byte[]:
Code:
protected void ConvertPDFToByteArray()
{
try
{
string path = Server.MapPath("~/pdf/test.pdf");
string path2 = Server.MapPath("~/pdf/byte.pdf");
//This code reads in a pdf, then saves it with a different file name.
fileArray = File.ReadAllBytes(path);
File.WriteAllBytes(path2, fileArray);
// Save Byte[] to db through webservice
SetArchive();
}
catch (Exception e)
{
SData.tCurrentError = e.GetType();
Response.Redirect("Error.aspx", true);
}
}
This is how I read the byte[] from the webservice:
Code:
protected void GenerateArchivePDF(int value)
{
GetReport(selectedReportID);
// Create PDF on current path with doc name "test.pdf"
string PDFpath = Server.MapPath("~/pdf/ArchivBeurteilungsbogen.pdf");
using (stream = new FileStream(PDFpath, FileMode.Create))
{
buffer = archive.PDFBitArray;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=/pdf/ArchivBeurteilungsbogen.pdf");
Response.AddHeader("Content-Length", buffer.Length.ToString());
Response.OutputStream.Write(buffer, 0, buffer.Length);
//Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
// To open in a new window: by saving pdf first
Response.Write("<script type='text/javascript'>");
Response.Write("window.open('/pdf/ArchivBeurteilungsbogen.pdf','_blank')");
Response.Write("</script>");
}
}
Can you see any mistakes?
Re: PDF cannot be read correctly!
I think it is because you append javascript code into to same stream from which Acrobat reads the PDF. Omit it, just send the bytes.
Re: PDF cannot be read correctly!
Sorry, the problem lay with that I had put the value null in the database for the byte[] I was reading out. I created a new PDF, consisting of a couple of dividers and some text, and saved it in the db in its place. I then managed to open the PDF using exactly the same methods as above. The strange thing is that only one of the dividers appeared, the rest of the dividers and the text didnt appear! Is there an error with my saving method?