如何使用 iTextSharp 在现有 PDF 中插入图像? [英] How can I insert an image with iTextSharp in an existing PDF?

查看:33
本文介绍了如何使用 iTextSharp 在现有 PDF 中插入图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的 PDF,我可以使用 FdFWriter 输入到文本框.它运作良好.现在我有一个图像.我已阅读文档并查看了许多示例,但它们都创建了新文档并插入了图像.我想获取现有的 PDF 并将图像插入图像字段或作为按钮的图标图像.我试过了,但它破坏了文档.

I have an existing PDF and I can use FdFWriter to input to text boxes. It works well. Now I have an image. I have read the documentation and looked at many examples but they all create new documents and insert an image. I want to take an existing PDF and insert an image into either an image field or as the icon image of a button. I have tried but it corrupts the document.

我需要能够获取现有文档并在其上放置图像.我不想打开、阅读、替换和删除原件.此原始更改和名称原始"仅表示此上下文中的源文件.有很多这样的 PDF 文件需要图片.

I need to be able to take an existing document and put an image on it. I do not want to open, read, replace, and delete the original. This original changes and the name "original" only means the source file in this context. There are many PDF files like this that need an image.

感谢您的帮助.

编辑 - 我非常感谢下面的代码.它工作得很好,但对我来说问题是现有的 PDF 上有数字签名.当文档像这样复制(到 result.pdf 中)时,这些签名虽然仍然存在,但具有不同的字节数或其他已损坏的项目.这意味着签名在显示在 result.pdf 上时,旁边有一个图标,表示签名无效".

Edit - I am very thankful for the code below. It works great, but the problem for me is that the existing PDF has digital signatures on it. When the document is copied like this (into result.pdf) those signatures, while still present, have a different byte count or other item that is corrupted. This means the signatures, while they show up on result.pdf, have an icon next to them that state "invalid signature."

以防万一,我使用黄玉签名板来创建我的签名,它有自己的安全性.仅复制 PDF 不会损坏它,但下面的过程会.

In case it matters I am using a Topaz signature pad to create my signatures, which has it's own security. Merely copying the PDF will not corrupt it but the process below will.

我试图将图像放在现有文档上,而不是它的副本,这在这种情况下很重要.

I am trying to put the image on the existing document, not a copy of it, which in this case matters.

另外,签名是指手写,而不是密码.

Also, by signature, I mean handwritten, not pin numbers.

再次感谢.

编辑 - PdfSignatureAppearance 可以用于此吗?

EDIT - Can PdfSignatureAppearance be used for this?

编辑 - 我似乎可以做到:

EDIT - I seem to be able to do it with:

var stamper = new PdfStamper(reader, outputPdfStream,'1',true);

var stamper = new PdfStamper(reader, outputPdfStream,'1',true);

推荐答案

如果您想更改现有 PDF 文件的内容并添加额外内容,例如水印、页码、额外标题,PdfStamper 就是您需要的对象.我已成功使用以下代码将图像插入到现有 pdf 文件中的给定绝对位置:

If you want to change the contents of an existing PDF file and add extra content such as watermarks, pagenumbers, extra headers, PdfStamper is the object you need. I have successfully used the following code to insert an image into an existing pdf file to a given absolute position:

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

class Program
{
    static void Main(string[] args)
    {
        using (Stream inputPdfStream = new FileStream("input.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
        using (Stream inputImageStream = new FileStream("some_image.jpg", FileMode.Open, FileAccess.Read, FileShare.Read))
        using (Stream outputPdfStream = new FileStream("result.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
        {
            var reader = new PdfReader(inputPdfStream);
            var stamper = new PdfStamper(reader, outputPdfStream);
            var pdfContentByte = stamper.GetOverContent(1);

            Image image = Image.GetInstance(inputImageStream);
            image.SetAbsolutePosition(100, 100);
            pdfContentByte.AddImage(image);
            stamper.Close();
        }
    }
}

当您插入图像时,您可以调整其大小.您可以查看 iTextSharp 文档中的 transformation matrix.

When you insert the image you have the possibility to resize it. You can take a look at transformation matrix in the iTextSharp documentation.

这篇关于如何使用 iTextSharp 在现有 PDF 中插入图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆