I am attempting to append images to an existing PDF using the following.
public static byte[] Append(byte[] inputPdf, params Image[] images)
{
var ms = new MemoryStream();
ms.Write(inputPdf, 0, inputPdf.Length);
ms.Seek(0, SeekOrigin.Begin);
using (Document pdf = new Document(iTextSharp.text.PageSize.A4, 10, 10, 10, 10))
using (PdfWriter writer = PdfWriter.GetInstance(pdf, ms))
{
pdf.Open();
foreach (var image in images)
{
var result = pdf.NewPage();
ImageFormat format = image.PixelFormat == PixelFormat.Format1bppIndexed
|| image.PixelFormat == PixelFormat.Format4bppIndexed
|| image.PixelFormat == PixelFormat.Format8bppIndexed
? ImageFormat.Tiff
: ImageFormat.Jpeg;
var pdfImage = iTextSharp.text.Image.GetInstance(image, format);
pdfImage.Alignment = Element.ALIGN_CENTER;
pdfImage.ScaleToFit(pdf.PageSize.Width, pdf.PageSize.Height);
pdf.Add(pdfImage);
}
pdf.Close();
}
ms.Flush();
return ms.GetBuffer();
}
The result
value is not used, I was debugging it. The value is always true, so the add page is working.
The resulting PDF is the same size as the original, but is not readable. I get invalid root object errors when opening it.
Any suggestions?
Thanks