ASP.Net MVC 3
I have an Action that returns a FileStreamResult after it imports a PDF document and stamps it with a watermark. Since it is possible to have a file not found error, how do I return a view instead of a filestream?
To complicate things I am using Philip Hutchison's jQuery PDFObject (http://pdfobject.com) to call the action and render it in a DIV so I cannot redirect on the server side.
To reiterate: It's a jQuery link on the page that fills a DIV with the results from a PDF filestream. The only 'hack' thing that I can think of is to send an Error.pdf file.
Your thoughts?
I ended up creating an error PDF memory stream.
MemoryStream ms = new MemoryStream();
try
{
PdfReader reader = new PdfReader(readerURL);
StampWatermark(WO, ms, reader);
}
catch (Exception ex)
{
RenderErrorPDF(WO, ms, ex);
}
byte[] byteinfo = ms.ToArray();
ms.Write(byteinfo, 0, byteinfo.Length);
ms.Position = 0;
ms.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(ms, "application/pdf");
RenderErrorPDF method
private static void RenderErrorPDF(WorkOrder WO, MemoryStream ms, Exception ex)
{
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, false);
var doc = new Document(new Rectangle(792f, 540f));
var wr = PdfWriter.GetInstance(doc, ms);
doc.Open();
doc.Add(new Paragraph("There was an error rendering the file that you requested...", new Font(bfTimes, 24f)));
doc.Add(new Paragraph(string.Format("\r\rFile: {0}", WO.DRAWING_FILE)));
doc.Add(new Paragraph(string.Format("\r\rError: {0}", ex.Message)));
wr.CloseStream = false;
doc.Close();
}