I have found that if I include the following code it will merge the 2 pdfs .
The following code was something I found a while back when I was trying to merger two different pdf versions together. MYM question is why would I need to do that when the pdfs are being dynamically created at the same time and then merged together, would they be the same version.
static private string WriteCompatiblePdf(string sFilename) { if (!Directory.Exists("c:\\Temp\\Converted\\")) Directory.CreateDirectory("c:\\Temp\\Converted\\"); string sNewPdf = "c:\\Temp\\Converted\\" + Guid.NewGuid().ToString() + ".pdf";
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(sFilename);
// we retrieve the total number of pages int n = reader.NumberOfPages; // step 1: creation of a document-object iTextSharp.text.Document document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(sNewPdf, FileMode.Create)); //write pdf that pdfsharp can understand writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4); // step 3: we open the document document.Open(); iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent; iTextSharp.text.pdf.PdfImportedPage page;
int rotation;
int i = 0; while (i < n) { i++; document.SetPageSize(reader.GetPageSizeWithRotation(i)); document.NewPage(); page = writer.GetImportedPage(reader, i); rotation = reader.GetPageRotation(i); if (rotation == 90 || rotation == 270) { cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height); } else { cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); } } // step 5: we close the document document.Close(); return sNewPdf; }
|