Note that there are some explanatory texts on larger screens.

plurals
  1. PODo you need Adobe PDF installed on server to work with iTextSharp?
    primarykey
    data
    text
    <p>I've developed a solution on my development machine where it:</p> <ol> <li>Opens PDFs for a file path server side via C#</li> <li>Merges them together</li> <li>Does a <code>Response.BinaryWrite</code> to push to a browser the merged PDF</li> </ol> <p>Works great on local DEV. When pushed to server, it gets some 'binary gibberish' in the browser window. </p> <p>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...</p> <p>Here is some sample code:</p> <pre><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&lt;string&gt; PDFs = new List&lt;string&gt;(); 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 &lt; 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)) &gt; 0) sum += count; // sum is a buffer offset for next reading } finally { fileStream.Close(); } return buffer; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload