Replace field with image

This code sample opens a PDF with a textfield and replaces this textfield with an image. The image has the same width and its aspect ratio is preserved. The top-left corner of the image is placed at the top-left corner of the field.

Replace Field

// open textField.pdf
using (FileStream fileIn = new FileStream("textfield.pdf", FileMode.Open))
{
  Document pdf = new Document(fileIn);

  // get the widget of field 'textField'
  TextField field = pdf.Fields["textField"] as TextField;
  Widget widget = field.Widgets[0];

  // create a new image an place it over the widget
  ImageShape image = new ImageShape("pyramid.png");
  image.Width = widget.Width;
  image.KeepAspectRatio = true;
  image.Transform = new TranslateTransform(widget.Left, widget.Top - image.Height);
  pdf.Pages[0].Overlay.Add(image);

  // remove the widget when saving
  widget.Persistency = WidgetPersistency.Remove;
                
  // save as image.pdf
  using (FileStream fileOut = new FileStream("image.pdf", FileMode.Create))
  {
    pdf.Write(fileOut);
  }
}
Download PDFKit.NET 5.0
We will send you a download link
Why do we ask your email address?
We send tips that speed up your evaluation
We let you know about bug fixes
You can always unsubscribe with one click
We never share your address with a 3rd party
Thank you for your download

We have sent an email with a download link. Alternatively, you may want to use the NuGet package manager to install our library.

Nuget ID

Use the NugetID and start right away, or download the package and install it handmatically