微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何将图像设置为现有pdf文件中的pdf字段?

如何在现有pdf文件中将图像设置为pdf字段?

我正在使用iTextSharp对象.

设置文本字段工作正常.没问题.

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

请帮忙.

解决方法

删除“文本”字段,并将其替换为相同大小和位置的“按钮”字段.如果将按钮设置为READ_ONLY,则无法按下它,它看起来像静态图像.这会将您尝试添加的图像保留为字段注释,而不是将其添加页面内容中.
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();
    }
}

原文地址:https://www.jb51.cc/csharp/97844.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐