This seems to be a common problem but after a lengthy search I have yet to find a solution that fits my needs. I am using itextsharp to fill out a pdf form from which i create a byte array. (There are multiple pages of the same form with the same information that I combine into a single pdf document). I can do this without issues, the problem is when I display the final pdf document to the user I need to open it in a different window or tab WITHOUT first prompting the user to save or open the file. I also do NOT want to have to save a file on the server and reopen it using filestream. If I change the content-disposition to "inline" the pdf is displayed in the same browser window. This creates problems because the user than has to click the browser back button to navigate back to the site which restarts the form submission process. As it currently sits, my code successfully generates the pdf and prompts them to open or save it. This is the step I need to remove. I seem to remember some java snippet that opened the pdf in a new tab or window but I have been unsuccessful in replicating / finding it. I've also played with different headers but I've hit a wall. Any help would be greatly appreciated.
(I call the go method of my code below from a button click after necessary data validation.)
private void go()
{
List<byte[]> pdfs = new List<byte[]>();
while (PageNumber <= Convert.ToInt32(PageCountLabel.Text))
{
pdfs.Add(PopulatePDF());
}
MemoryStream ms = MergePDFs(pdfs);
//opens pdf in new tab after save/open option
Response.AddHeader("Content-Disposition", "attachment; filename=TheDocument.pdf");
Response.ContentType = "application/pdf";
Response.BinaryWrite(ms.ToArray());
Response.End();
}
//************fills in pdf form****************//
private byte[] PopulatePDF()
{
MemoryStream ms = new MemoryStream();
PdfStamper Stamper = null;
PdfReader Reader = new PdfReader(Server.MapPath("~/PDFTemplates/template1.pdf"));
try
{
string temp = Profile.ToString().ToUpper();
PdfCopyFields Copier = new PdfCopyFields(ms);
Copier.AddDocument(Reader);
Copier.Close();
PdfReader docReader = new PdfReader(ms.ToArray());
ms = new MemoryStream();
Stamper = new PdfStamper(docReader, ms);
AcroFields Fields = Stamper.AcroFields;
//fill form fields here
PageNumber++;
Stamper.FormFlattening = true;
}
finally
{
if (Stamper != null)
{
Stamper.Close();
}
}
return ms.ToArray();
}
//combines pdf pages into single document
private MemoryStream MergePDFs(List<byte[]> pdfs)
{
MemoryStream ms = new MemoryStream();
PdfCopyFields Copier = new PdfCopyFields(ms);
foreach (var pdf in pdfs)
Copier.AddDocument(new PdfReader(pdf));
Copier.Close();
return ms;
}