我如何可以将图像设置为现有的PDF文件生成PDF场? [英] How can i set an image to a pdf field in existing pdf file?

查看:181
本文介绍了我如何可以将图像设置为现有的PDF文件生成PDF场?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何能我在图像中现有的PDF文件中设置为PDF场?

How can i set an image to a pdf field in existing pdf file?

我使用iTextSharp的对象。

I'm using the iTextSharp object.

设置文本字段工作正常。 。这没有问题。

Setting the text field is working fine. No problem in it.

   pdfFormFields.SetField("Firstname", "Mujeeb");

请帮忙。

推荐答案

删除文本字段,并使用相同的大小和位置的按钮现场更换。如果设置按钮为READ_ONLY那么它不能被按下,它看起来像一个静态图像。这样可以使你想添加,而不是作为一个字段批注将其添加到网页内容的图像。

Remove the Text field and replace it with a Pushbutton field of the same size and position. If you set the Pushbutton to READ_ONLY then it can't be pressed and it will look like a static image. This keeps the image you're trying to add as a field annotation instead of adding it to the page content.

void ConvertTextFieldToImage(string inputFile, string fieldName, string imageFile, string outputFile)
{
    using (PdfStamper stamper = new PdfStamper(new PdfReader(inputFile), File.Create(outputFile)))
    {
        AcroFields.FieldPosition fieldPosition = stamper.AcroFields.GetFieldPositions(fieldName)[0];

        PushbuttonField imageField = new PushbuttonField(stamper.Writer, fieldPosition.position, fieldName);
        imageField.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
        imageField.Image = iTextSharp.text.Image.GetInstance(imageFile);
        imageField.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
        imageField.ProportionalIcon = false;
        imageField.Options = BaseField.READ_ONLY;

        stamper.AcroFields.RemoveField(fieldName);
        stamper.AddAnnotation(imageField.Field, fieldPosition.page);

        stamper.Close();
    }
}

这篇关于我如何可以将图像设置为现有的PDF文件生成PDF场?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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