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

前端 未结 3 1754
既然无缘
既然无缘 2021-01-18 15:19

\"enter

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

I\'m usin

3条回答
  •  忘掉有多难
    2021-01-18 16:03

    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();
        }
    }
    

提交回复
热议问题