> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/core/forms/fill-fields.md).

# Filling PDF form fields on Server/Desktop

We show how to fill PDF form fields on Server/Desktop. Guide includes sample code.

To fill and set values for existing form fields.

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
PDFDoc doc = new PDFDoc(filename);

// Search for a specific field
Field fld = doc.GetField("employee.name.first");
fld.SetValue("John");
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
PDFDoc doc(filename);

// Search for a specific field
Field fld = doc.GetField("employee.name.first");
fld.SetValue("John");
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
doc := NewPDFDoc(filename)

// Search for a specific field
fld := doc.GetField("employee.name.first")
fld.SetValue("John")
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
PDFDoc doc = new PDFDoc(filename);

// Search for a specific field
Field fld = doc.getField("employee.name.first");
fld.setValue("John");
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function main() {
  const doc = await PDFNet.PDFDoc.createFromURL(filename);

  // Search for a specific field
  const field = await doc.getField("employee.name.first");
  await field.setValueAsString("John");
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val doc = PDFDoc(filename)

// Search for a specific field
val fld = doc.getField("employee.name.first")
fld.setValue("John")
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];

// Search for a specific field
PTField *fld = [doc GetField: @"employee.name.first"];
[fld SetValue: @"John"];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let doc: PTPDFDoc = PTPDFDoc(filepath: filename)

// Search for a specific field
let fld: PTField = doc.getField("employee.name.first")
fld.setValue("John")
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$doc = new PDFDoc($filename);

// Search for a specific field
$fld = $doc->GetField("employee.name.first");
$fld->SetValue("John");
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
doc = PDFDoc(filename)

# Search for a specific field
fld = doc.GetField("employee.name.first")
fld.SetValue("John")
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
doc = PDFDoc.new(filename)

# Search for a specific field
fld = doc.GetField("employee.name.first")
fld.SetValue("John")
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
Dim doc4 As PDFDoc = New PDFDoc(filename)

' Search for a specific field
Dim fld As Field = doc4.GetField("employee.name.first")
fld.SetValue("John")
```

{% endcode %}
{% endtab %}
{% endtabs %}

[PDF Interactive Forms (AcroForms)](/core/get-started/samples/interactiveformstest.md) Full code sample which illustrates some basic PDFNet capabilities related to interactive forms (also known as AcroForms). Code sample is available in C++, C#, Java, Python, Go, PHP, Ruby & VB.

## About filling form fields

Form Fields can be populated using the `Field.SetValue()` method:

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
field.SetValue("New Value");

// Regenerate appearance stream.
field.RefreshAppearance();
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
field.SetValue("New Value");

// Regenerate appearance stream.
field.RefreshAppearance();
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
field.SetValue("New Value")

// Regenerate appearance stream.
field.RefreshAppearance()
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
field.setValue("New Value");

// Regenerate appearance stream.
field.refreshAppearance();
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
field.setValueAsString("New Value");

// Regenerate appearance stream.
field.refreshAppearance();
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
field.setValue("New Value")

// Regenerate appearance stream.
field.refreshAppearance()
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
[field SetValue: @"New Value"];

// Regenerate appearance stream.
[field RefreshAppearance];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
field.SetValue("New Value")

// Regenerate appearance stream.
field.RefreshAppearance()
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$field->SetValue("New Value");

// Regenerate appearance stream.
$field->RefreshAppearance();
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
field.SetValue("New Value")

# Regenerate appearance stream.
field.RefreshAppearance()
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
field.SetValue("New Value")

# Regenerate appearance stream.
field.RefreshAppearance()
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
field.SetValue("New Value")

' Regenerate appearance stream.
field.RefreshAppearance()
```

{% endcode %}
{% endtab %}
{% endtabs %}

Note that, after modifying the Field's value, we refreshed its appearance stream. In the PDF format, Field's value and appearance are two different entities. Therefore, if you don't call `RefreshAppearance()`, the initial value on a PDF page will remain unchanged — it may have retain the old value or it may be blank.

One approach used by other PDF libraries is to let the PDF viewer automatically pre-generate appearance streams by setting the 'NeedAppearances' flag in AcroForm dictionary:

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
doc.GetAcroForm().PutBool("NeedAppearances", true);
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
doc.GetAcroForm().PutBool("NeedAppearances", true);
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
doc.GetAcroForm().PutBool("NeedAppearances", true)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
doc.getAcroForm().putBool("NeedAppearances", true);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
doc.getAcroForm().putBool("NeedAppearances", true);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
doc.getAcroForm().putBool("NeedAppearances", true);
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
[doc GetAcroForm] PutBool: @"NeedAppearances" value: true];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
doc.getAcroForm().putBool("NeedAppearances", value: true)
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$doc->GetAcroForm()->PutBool("NeedAppearances", true);
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
doc.GetAcroForm().PutBool("NeedAppearances", True)
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
doc.GetAcroForm().PutBool("NeedAppearances", true)
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
doc.GetAcroForm().PutBool("NeedAppearances", true)
```

{% endcode %}
{% endtab %}
{% endtabs %}

This will force viewer applications to auto-generate appearance streams every time the document is opened. This method is unreliable — Acrobat does not always generate appearance streams correctly. Another disadvantage of this approach is that the user will always be prompted to save the document even if the document was never modified.

`Field.GetValueAsString()` returns the field's value as a string. The value returned varies based on the field type. A text field type varies depending on the field type. A text field will return a string:

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
if (type == Field.FieldType.e_text && field.GetValue())
{
  Console.WriteLine("Field value: {0}", field.GetValueAsString());
}
else
{
  Console.WriteLine("Field is blank");
}
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
if (type == Field.Type.e_text && field.GetValue())
{
  printf("Field value: %s", field.GetValueAsString());
}
else
{
  printf("Field is blank");
}
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
if type == Field.Type.e_text && field.GetValue()
{
  fmt.Println("Field value: " + field.GetValueAsString())
}
else
{
  fmt.Println("Field is blank")
}
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
if (type == Field.e_text && field.getValue())
{
  println("Field value: %s", field.getValueAsString());
}
else
{
  println("Field is blank");
}
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
if (type === PDFNet.Field.Type.e_text && field.getValue()) {
  console.log("Field value: %s", field.getValueAsString());
} else {
  console.log("Field is blank");
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
if (type == Field.e_text && field.getValue())
{
  println("Field value: ${field.getValueAsString()}")
}
else
{
  println("Field is blank")
}
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
if (type == e_pttext && [field GetValue])
{
  printf("Field value: %s", [field GetValueAsString];
}
else
{
  printf("Field is blank");
}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
if (type == e_pttext && field.getValue())
{
  print("Field value: \(field.getValueAsString())")
}
else
{
  print("Field is blank")
}
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
if (type == Field::e_text && $field->GetValue())
{
  printf("Field value: %s", $field->GetValueAsString());
}
else
{
  printf("Field is blank");
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
if type == Field::e_text and field.GetValue():
  print("Field value: %s" % (field.GetValueAsString()))
else:
  print("Field is blank")
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
if type == Field::e_text && field.GetValue()
  puts "Field value: %s" % [field.GetValueAsString()]
else
  puts "Field is blank"
end
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
if type == Field.Type.e_text And field.GetValue() Then
  Console.WriteLine("Field value: {0}", field.GetValueAsString())
Else
  Console.WriteLine("Field is blank")
End If
```

{% endcode %}
{% endtab %}
{% endtabs %}

Other field types, such as check boxes and radio buttons, can also return text from `GetValueAsString()`. Similarly, the `Field.GetValueAsString()` method is available.

## Access interactive fields

The form shown in the following figure consists of a number of Fields:

![](https://3779731113-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fziw3GiL98Xfj63F3He8h%2Fuploads%2Fgit-blob-d05d655d676590f4499e02d95420b4bfe8c76f2b%2Fd34fbd5ce2b4792f76058f58f4e5d391153db51e-450x434.gif?alt=media)

Every field has its name and value, as well as its annotation appearance.

In the Apryse SDK, Fields are accessed through `FieldIterators`.

For example, the list of all Fields present in the document can be traversed using the following code snippet:

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
FieldIterator itr;
for(itr=doc.GetFieldIterator(); itr.HasNext(); itr.Next())
{
  Field field = itr.Current();
  Console.WriteLine("Field name: {0}",field.GetName());
}
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
FieldIterator itr;
for(itr=doc.GetFieldIterator(); itr.HasNext(); itr.Next())
{
  Field field = itr.Current();
  printf("Field name: %s", field.GetName());
}
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
itr := doc.GetFieldIterator()
for itr.HasNext() {
  field := itr.Current()
  fmt.Println("Field name: " + field.GetName())
}
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
FieldIterator itr = doc.getFieldIterator();
while (itr.hasNext())
{
    Field field = itr.next();
    println("Field name: %s", field.GetName());
}
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
const itr = await doc.getFieldIteratorBegin();
for (; await itr.hasNext(); itr.next()) {
  const field = await itr.current();
  println("Field name: %s", field.GetName());
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val itr = doc.fieldIterator()
while (itr.hasNext())
{
    val current = itr.next()
    println("Field name: ${field.GetName()}")
}
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTFieldIterator * itr = [doc GetFieldIterator];
for(; [itr HasNext]; [itr Next])
{
  PTField *field = [[itr Current] GetName];
  printf("Field name: %s", [field UTF8String]);
}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let itr: PTFieldIterator = doc.getFieldIterator()
while itr.hasNext()
{
    let field: PTField = itr.current()
    print("Field name: \(field.getName()!)")
    itr.Next()
}
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$itr = $doc->GetFieldIterator();
for(; $itr->HasNext(); $itr->Next())
{
  $field = $itr->Current();
  printf("Field name: %s", $field->GetName());
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
itr = doc.GetFieldIterator()
while itr.HasNext():
  field = itr.Current()
  print("Field name: %s" % (field.GetName())
  itr.Next()
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
itr = doc.GetFieldIterator()
while itr.HasNext do
  field = itr.Current()
  puts "Field name: %s" % [field.GetName()]
end
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
Dim itr As FieldIterator = doc4.GetFieldIterator()
While itr.HasNext()
  Dim field As Field = itr.Current()
  Console.WriteLine("Field name: {0}", field.GetName())
  itr.Next()
End While
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/core/forms/fill-fields.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
