使用iTextSharp将图像附加到PDF页面

时间:2013-04-11 11:53:25

标签: c# image 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值,我正在调试它。该值始终为true,因此添加页面正在运行。

生成的PDF与原始PDF大小相同,但不可读。打开它时,我得到无效的根对象错误。

有什么建议吗?

由于

2 个答案:

答案 0 :(得分:1)

方法1(不含PdfStamper)

using (var ms = new MemoryStream())
{
        var pdf = new PdfReader(inputPdf);
        var doc = new Document(pdf.GetPageSizeWithRotation(1));
        using (var writer = PdfWriter.GetInstance(doc, ms))
        {
            doc.Open();

            for (int page = 0; page < pdf.NumberOfPages; page++)
            {
                doc.SetPageSize(pdf.GetPageSizeWithRotation(page + 1));
                doc.NewPage();
                var pg = writer.GetImportedPage(pdf, page + 1);
                int rotation = pdf.GetPageRotation(page + 1);
                if (rotation == 90 || rotation == 270)
                    writer.DirectContent.AddTemplate(
                        pg, 0, -1f, 1f, 0, 0, pdf.GetPageSizeWithRotation(page).Height);
                else
                    writer.DirectContent.AddTemplate(pg, 1f, 0, 0, 1f, 0, 0);
            }
            foreach (var image in images)
            {
                doc.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(doc.PageSize.Width - 10, doc.PageSize.Height - 10);
                doc.Add(pdfImage);
            }
            doc.Close();
            writer.Close();
        }
        ms.Flush();
        return ms.GetBuffer();
}

方法2(使用PdfStamper)

var pdfReader = new PdfReader(inputPdf);
using (var ms = new MemoryStream())
{
        using (var stamp = new PdfStamper(pdfReader, ms))
        {
            foreach (var image in images)
            {
                var size = pdfReader.GetPageSize(1);
                var page = pdfReader.NumberOfPages + 1;
                stamp.InsertPage(page, pdfReader.GetPageSize(1));
                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.SetAbsolutePosition(0, size.Height - pdfImage.Height);
                pdfImage.ScaleToFit(size.Width, size.Height);
                stamp.GetOverContent(page).AddImage(pdfImage);
            }
        }
        ms.Flush();
        return ms.GetBuffer();
}

答案 1 :(得分:0)

您错误地假设您可以将两个PDF文档的字节粘在一起。

您有一个如下所示的PDF:

%PDF-1.6
%âãÏÓ
1 0 obj <<
... PDF syntax
%%EOF

另一个看起来像这样:

%PDF-1.6
%âãÏÓ
1 0 obj <<
... PDF syntax
%%EOF

导致文件如下所示:

%PDF-1.6
%âãÏÓ
1 0 obj <<
... PDF syntax
%%EOF
%PDF-1.6
%âãÏÓ
1 0 obj <<
... PDF syntax
%%EOF

你真的不应该期待这个工作!请先阅读本书的chapter 6,然后阅读有关被叫PdfStamper的内容。然后转到这个问题:How can I insert an image with iTextSharp in an existing PDF?