I am creating a pdf and would like to add existing pdfs and/or images. I have the following code that works well for the images, but I am having trouble with the pdf section as the existing pdf is not displayed in the new pdf, while the images are fine. I found the following question but my code looks similar. Any ideas as to what I am missing?
using (MemoryStream ms = new MemoryStream())
{
PdfWriter pWriter = PdfWriter.GetInstance(myDoc, ms);
myDoc.Open();
int index = 0;
iTextSharp.text.Image img;
foreach (var buf in bufList)
{
if (uploadType[index] == 0)
{
PdfContentByte pdfContentByte = pWriter.DirectContent;
PdfReader reader = new PdfReader(buf);
int pageCount = reader.NumberOfPages;
myDoc.SetPageSize(reader.GetPageSizeWithRotation(1));
for (int pageNum = 1; pageNum <= pageCount; pageNum++)
{
myDoc.NewPage();
PdfImportedPage importedPage = pWriter.GetImportedPage(reader, pageNum);
pdfContentByte.AddTemplate(importedPage, 0, 0);
}
reader.Close();
}
else
{
myDoc.NewPage();
img = iTextSharp.text.Image.GetInstance(buf);
img.ScaleToFit(612f, 792f);
img.Alignment = iTextSharp.text.Image.ALIGN_CENTER | iTextSharp.text.Image.ALIGN_MIDDLE;
myDoc.Add(img);
}
index++;
}
pWriter.CloseStream = false;
myDoc.Close();
ms.Position = 0;
}