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

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

问题描述

我有一个现有的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."

如果重要,我使用的是Topaz签名板创建我的签名,它有自己的安全性。仅仅复制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文档中查看转换矩阵

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

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

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