【问题标题】:append image to pages of PDF using iTextSharp使用 iTextSharp 将图像附加到 PDF 页面
【发布时间】:2013-04-11 11:53:24
【问题描述】:

我正在尝试使用以下方法将图像附加到现有 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 大小相同,但不可读。打开时出现无效的根对象错误。

有什么建议吗?

谢谢

【问题讨论】:

标签: c# image pdf itextsharp


【解决方案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();
}

【讨论】:

    【解决方案2】:

    您错误地假设可以将两个 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?

    【讨论】:

    • 我正在尝试向 PDF 添加新页面,而不是在 pdf 上标记现有页面。
    • 我知道,但您仍然需要 PdfStamper 及其 insertPage() 方法(阅读免费章节的第 6.3.4 节)。您当然不想将一个文件的字节连接到另一个文件的字节。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 2020-02-25
    • 2011-05-06
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    相关资源
    最近更新 更多