I've developed a solution on my development machine where it:
- Opens PDFs for a file path server side via C#
- Merges them together
- Does a
Response.BinaryWrite
to push to a browser the merged PDF
Works great on local DEV. When pushed to server, it gets some 'binary gibberish' in the browser window.
Adobe or Foxit Reader is NOT installed on the server, however it is installed on my local dev machine. My understanding is that iTextSharp allowed you to not need PDF Readers installed at all, but does it? Or maybe this is an IIS thing where .pdf is not listed as a filetype...
Here is some sample code:
// First set up the response and let the browser know a PDF is coming
context.Response.Buffer = true;
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-Disposition", "inline");
List<string> PDFs = new List<string>();
PDFs.Add(@"c:\users\shane\documents\visual studio 2010\Projects\PDFMultiPrintTester\PDFMultiPrintTester\TEST1.pdf");
PDFs.Add(@"c:\users\shane\documents\visual studio 2010\Projects\PDFMultiPrintTester\PDFMultiPrintTester\TEST2.pdf");
PDFs.Add(@"c:\users\shane\documents\visual studio 2010\Projects\PDFMultiPrintTester\PDFMultiPrintTester\TEST3.pdf");
// Second, some setup stuff
System.IO.MemoryStream MemStream = new System.IO.MemoryStream();
iTextSharp.text.Document doc = new iTextSharp.text.Document();
iTextSharp.text.pdf.PdfReader reader = default(iTextSharp.text.pdf.PdfReader);
int numberOfPages = 0;
int currentPageNumber = 0;
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, MemStream);
doc.Open();
iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
iTextSharp.text.pdf.PdfImportedPage page = default(iTextSharp.text.pdf.PdfImportedPage);
int rotation = 0;
foreach (string f in PDFs)
{
// Third, append all the PDFs--THIS IS THE MAGIC PART
byte[] sqlbytes = null;
sqlbytes = ReadFile(f);
reader = new iTextSharp.text.pdf.PdfReader(sqlbytes);
numberOfPages = reader.NumberOfPages;
currentPageNumber = 0;
while ((currentPageNumber < numberOfPages))
{
currentPageNumber += 1;
doc.SetPageSize(PageSize.LETTER);
doc.NewPage();
page = writer.GetImportedPage(reader, currentPageNumber);
rotation = reader.GetPageRotation(currentPageNumber);
if ((rotation == 90) | (rotation == 270))
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(currentPageNumber).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
}
// Finally Spit the stream out
if (MemStream == null)
{
context.Response.Write("No Data is available for output");
}
else
{
doc.Close();
context.Response.BinaryWrite(MemStream.GetBuffer());
context.Response.End();
MemStream.Close();
}
}
}
public static byte[] ReadFile(string filePath)
{
byte[] buffer;
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
try
{
int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = 0; // total number of bytes read
// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
}
finally
{
fileStream.Close();
}
return buffer;
}