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

Muj*_*oqi 6 c# itextsharp

在此输入图像描述

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

我正在使用iTextSharp对象.

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

   pdfFormFields.SetField("Firstname", "Mujeeb");
Run Code Online (Sandbox Code Playgroud)

请帮忙.

Mat*_*ger 9

删除"文本"字段,并将其替换为相同大小和位置的"按钮"字段.如果将按钮设置为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();
    }
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*aas 7

据我所知,您无法在技术上将标准PDF字段设置为图像(尽管您可以使用XFA执行此操作).

然而,解决方法是只创建一个标准的iTextSharp图像并将其缩放到表单字段的尺寸并将其放在字段所在的位置.

下面是一个完整的C#2010 WinForms应用程序,目标是iTextSharp 5.1.1.0,它展示了如何执行此操作.它首先创建一个非常简单的PDF,其上有一个名为"firstName"的表单字段.然后,程序的第二部分获取该字段的位置和尺寸,并将图像放置在适当的位置.有关更多详细信息,请参阅代码中的注释.

using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string baseFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "StartFile.pdf");
            string secondFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "SecondFile.pdf");
            string TestImage = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.jpg");

            //Create a very simple PDF with a single form field called "firstName"
            using (FileStream fs = new FileStream(baseFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document(PageSize.LETTER))
                {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
                    {
                        doc.Open();
                        writer.AddAnnotation(new TextField(writer, new iTextSharp.text.Rectangle(0, 0, 100, 100), "firstName").GetTextField());
                        doc.Close();
                    }
                }
            }


            //Create a second file "filling out" the form above
            using (FileStream fs = new FileStream(secondFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (PdfStamper stamper = new PdfStamper(new PdfReader(baseFile), fs))
                {
                    //GetFieldPositions returns an array of field positions if you are using 5.0 or greater
                    //This line does a lot and should really be broken up for null-checking
                    iTextSharp.text.Rectangle rect = stamper.AcroFields.GetFieldPositions("firstName")[0].position;
                    //Create an image
                    iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(TestImage);
                    //Scale it
                    img.ScaleAbsolute(rect.Width, rect.Height);
                    //Position it
                    img.SetAbsolutePosition(rect.Left, rect.Bottom);
                    //Add it to page 1 of the document
                    stamper.GetOverContent(1).AddImage(img);
                    stamper.Close();
                }
            }

            this.Close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)