> 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/xamarin/generate/generate-via-template.md).

# Generate PDF in Xamarin

Here is your guide explaining how to generate document accurately and efficiently with Xamarin using Apryse SDK

## Overview

Apryse's office template generation framework enables the dynamic generation of PDFs:

* You provide a [template](/xamarin/generate/data-model.md) in the form of an Office document (such as .docx)
* You provide [replacement data](/xamarin/generate/data-model.md#allowed-formats-for-values) in JSON form
* Apryse SDK will efficiently generate a PDF by merging the data with the template
* Fully featured, with support for loops, conditionals, images, table generation, etc.
* Use your preferred office editor to design attractive templates, and Apryse will take care of the rest
* All logic is entirely contained within the SDK, with no external dependencies

<a href="https://sdk.apryse.com/samples/web/samples/advanced/template-fill/" class="button primary">Launch Online Demo</a>

## Easy to use

The generation framework requires minimal code to get up and running. Simply initiate a normal Office conversion, but with the optional `TemplateParamsJson` parameter set:

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

```csharp
// Create a TemplateDocument object from an input office file.
TemplateDocument template_doc = Convert.CreateOfficeTemplate(input_path, null);

// Fill the template with data from a JSON string, producing a PDF document.
PDFDoc pdf_doc = template_doc.FillTemplateJson(json_data_string);

// Save the PDF to a file.
pdf_doc.Save(output_path, SDFDoc.SaveOptions.e_linearized);
```

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

<a href="/xamarin/get-started/samples.md#officetemplate" class="button primary">Sample Code</a><a href="/xamarin/generate/generate-via-template/apis.md" class="button primary">API Docs</a><a href="/xamarin/generate/data-model.md" class="button primary">Template Specification</a>

## Examples

The following are a series of real examples, generated using the code above. For each example, the template source and the json data source are included below, so that you can try them out yourself.

### Simple text replacement ([.docx](https://www.pdftron.com/docs/template-generation/tg_demo_repeated.docx), [.json](https://www.pdftron.com/docs/template-generation/template_params_utf8.json))

Tags in the template document are replaced with a string from the supplied JSON. To control the style of the text, simply modify the style of the template tag as desired:

![](https://653871032-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgjsBtuYmcKhWOdM9VCg4%2Fuploads%2Fgit-blob-83944acf8c30622179f3a098ac1deabcf42a616c%2Fd890bc566449ac649bfcc57c28f520f9f5049d3b-1440x480.png?alt=media)

{% tabs %}
{% tab title="JSON" %}
{% code lineNumbers="true" %}

```json
{
	"short_text": "Lorem ipsum dolor, .etc"
}
```

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

### Automatic reflow ([.docx](https://www.pdftron.com/docs/template-generation/tg_demo_columns_reflow.docx), [.json](https://www.pdftron.com/docs/template-generation/template_params_utf8.json))

Text reflows according to the rules set in the original template file, and will automatically generate extra pages if needed. Make use of column layouts and justification rules. This example uses a list loop with dynamic item renumbering.

![](https://653871032-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgjsBtuYmcKhWOdM9VCg4%2Fuploads%2Fgit-blob-a9c9c7648ec09f2770c806a37f61a83e6ab3383d%2F0f914a56723bf56c24cd0fb3c791495ce929892d-1440x502.png?alt=media)

{% tabs %}
{% tab title="JSON" %}
{% code lineNumbers="true" %}

```json
{
	"legal_heading":"End-User Agreement",
	"legal_clauses": [
		{"legal_clause":"Sed ut unde omnis iste natus error sit volup tatem..."},
		{"legal_clause":"Lorem ipsum dolor sit amet, consec tetuer adipi s..."},
		{"legal_clause":"pretium quis, sem. Nulla conse quat massa quis eni..."},
		{"legal_clause":"idunt. Cras dapibus. Vivamus elem entum semper nis..."}
	]
}
```

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

### Dynamic tables ([.docx](https://www.pdftron.com/docs/template-generation/tg_demo_styled_table_loop.docx), [.json](https://www.pdftron.com/docs/template-generation/template_params_utf8.json))

Dynamically insert or remove rows into tables using loops and conditionals. Table formatting such as row bands are updated. Especially useful for invoice generation.

![](https://653871032-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgjsBtuYmcKhWOdM9VCg4%2Fuploads%2Fgit-blob-da5d3c7b6a19b19571b85cb2869ed5fb1aba555a%2F74a6dc14372bedfbea43d9a06327bf737706af78-1437x497.png?alt=media)

{% tabs %}
{% tab title="JSON" %}
{% code lineNumbers="true" %}

```json
{
	"items": [
		{"item": "Wireless", "charge": "$42.34"},
		{"item": "Long Distance", "charge": "$12.55"},
		{"item": "Data", "charge": "$14.89"}
	],
	"balance":"$12.52",
	"total_due":"$82.30",
}
```

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

### Insert images ([.docx](https://www.pdftron.com/docs/template-generation/tg_demo_logo.docx), [.json](https://www.pdftron.com/docs/template-generation/template_params_utf8.json))

Insert images into the text at any location using the `image_url` value specifier. Images can be resized as desired, and inserted at any position in the original document.

![](https://653871032-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgjsBtuYmcKhWOdM9VCg4%2Fuploads%2Fgit-blob-44fb31d8dc44fcb20fbd3f6585dac0a7428176be%2F1d6544aac1e26cd8d548bb7daeaa560d0315e010-1440x480.png?alt=media)

{% tabs %}
{% tab title="JSON" %}
{% code lineNumbers="true" %}

```json
{
	"logo":{"image_url":"./logo_red.png", "width":40, "height":40}
}
```

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

### Reflow around figures ([.docx](https://www.pdftron.com/docs/template-generation/tg_demo_reflow.docx), [.json](https://www.pdftron.com/docs/template-generation/template_params_utf8.json))

Automatically flow text around obstacles according to the template.

![](https://653871032-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgjsBtuYmcKhWOdM9VCg4%2Fuploads%2Fgit-blob-e0f33978ee802bb4ad913580efadeec70fdb896a%2F3164d8efc69b1b7ff658d6f7a5a4ffecb7e5db7e-1440x480.png?alt=media)

{% tabs %}
{% tab title="JSON" %}
{% code lineNumbers="true" %}

```json
{
	"long_text": "Sed ut perspiciatis unde omnis iste natus..."
}
```

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

## Apryse benefits

* Securely merge data directly in your application. No need to share confidential data with anyone!
* Use familiar tools (any word processor such as MS Word or Google Docs) to design professional looking templates with no plugins or extra tools required
* Top quality PDF generation of your templates with the market leading Apryse SDK
* Self-contained, with no third-party dependencies
* Generated PDFs have automatically generated accessibility tags

## Further Reading

For more information, see the following resources:

[Detailed specification of the template generation data model](/xamarin/generate/data-model.md) To find out more details about the specification about the data model

[Advanced features](/xamarin/generate/advanced.md) For a specification of advanced features, and greater detail on behavior

[Live Web Sample](https://sdk.apryse.com/samples/web/samples/advanced/template-fill/) To try a live sample of template filling using our WebViewer


---

# 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/xamarin/generate/generate-via-template.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.
