加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:13:21 所属栏目:百科 来源:网络整理
导读:如何在现有pdf文件中将图像设置为pdf字段? 我正在使用iTextSharp对象. 设置文本字段工作正常.没问题. pdfFormFields.SetField("Firstname","Mujeeb"); 请帮忙. 解决方法 删除“文本”字段,并将其替换为相同大小和位置的“按钮”字段.如果将按钮设置为READ_O
如何在现有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();
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读