我正在尝试使用以下方法将图像追加到现有PDF。使用iTextSharp将图像追加到PDF页面
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();
}
result
值没有使用,我正在调试它。该值始终为真,因此添加页面正在工作。
生成的PDF与原始大小相同,但不可读。打开时出现无效的根对象错误。
有什么建议吗?
感谢
当与现有的PDF工作,你需要使用一个PdfStamper,请参阅[如何在现有PDF中插入iTextSharp图像?](http://stackoverflow.com/questions/583629/how-can-i-insert-an-image-with-itextsharp-in- an-existing-pdf) – 2013-04-11 13:02:57