U.Q
U.Q

Reputation: 21

How to rename PDF form fields using PDF Sharp?

I am using PDF Sharp and have one issue only. I cannot rename form fields. We have a field called 'x' and after an operation is applied to field 'x', it needs to be renamed to field 'y'.

I have seen tons of documentation on how to do this using itextSharp. Unfortunately my firm cannot use them and so I am looking for a solution using PDF Sharp.

Any ideas?

Upvotes: 2

Views: 1935

Answers (3)

Michael
Michael

Reputation: 2955

VB.NET version for PDFsharp 1.50.5147

Dim i = 0
While i < pdfDoc.AcroForm.Fields.Count
  pdfDoc.AcroForm.Fields(i).Elements.SetString("/T", "formField" & i)
  i += 1
End While

Upvotes: 0

Salvador Torres
Salvador Torres

Reputation: 21

This can give you an idea on how to perform the field renaming

var uniqueIndex = Guid.NewGuid();
var fields = pdfDocument.AcroForm.Fields;
var fieldNames = fields.Names;

  for (int idx = 0; idx < fieldNames.Length; ++idx)
  {
      var fieldName = fieldNames[idx];
      var field = fields[fieldName];

      field.Elements.SetName($"/{fieldName}", $"{fieldName}_{uniqueIndex}");
  }

Upvotes: 2

Sergey Lukashov
Sergey Lukashov

Reputation: 21

I was able to rename form field via PdfSharp as follow:

public void RenameAcroField(PdfAcroField field, string newFieldName)
{
    field.Elements.SetString("/T", newFieldName);
}

Little bit tricky but worked for my case. Hope it will help.

Upvotes: 2

Related Questions