> 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/ios/get-started/samples/officetemplatetest.md).

# OfficeTemplate

Sample Obj-C code for using Apryse SDK to generate a PDF from an Office document template and a JSON string.  Does not require any external dependencies or MS Office licenses.

Sample Obj-C code for using Apryse SDK to generate a PDF from an Office document template and a JSON string. Does not require any external dependencies or MS Office licenses. Learn more about our [iOS SDK](/ios/guides.md) and [Office Template Generation](/core/create/generate-via-template.md).

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

```objc
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

#import <OBJC/PDFNetOBJC.h>
#import <Foundation/Foundation.h>

//------------------------------------------------------------------------------
// The following sample illustrates how to use the PTConvert utility class
// to convert MS Office files to PDF and replace templated tags present in the document
// with content supplied via json
//
// For a detailed specification of the template format and supported features,
// see: https://docs.apryse.com/core/guides/generate-via-template/data-model/
//
// This conversion is performed entirely within the PDFNet and has *no*
// external or system dependencies -- Conversion results will be
// the same whether on Windows, Linux or Android.
//
// Please contact us if you have any questions.
//------------------------------------------------------------------------------

int main(int argc, char *argv[])
{

    @autoreleasepool {

        [PTPDFNet Initialize: 0];

        NSString *input_path = @"../../TestFiles/";
        NSString *output_path = @"../../TestFiles/Output/";
        NSString *input_filename = @"SYH_Letter.docx";
        NSString *output_filename = @"SYH_Letter.pdf";
        NSString *json = [NSString stringWithFormat: @"{\"dest_given_name\": \"Janice N.\", "
            @"\"dest_street_address\": \"187 Duizelstraat\", \"dest_surname\": \"Symonds\", "
            @"\"dest_title\": \"Ms.\", \"land_location\": \"225 Parc St., Rochelle, QC \","
            @"\"lease_problem\": \"According to the city records, the lease was initiated in September 2010 and never terminated\", "
            @"\"logo\": {\"image_url\": \"%@logo_red.png\", \"width\" : 64, \"height\" : 64},"
            @"\"sender_name\": \"Arnold Smith\"}", input_path];

        PTOfficeToPDFOptions* options = [[PTOfficeToPDFOptions alloc] init];

	    // Create a TemplateDocument object from an input office file.
	    PTTemplateDocument *template_doc = [PTConvert CreateOfficeTemplate: [NSString stringWithFormat:@"%@/%@", input_path, input_filename]
		                                              options: options];

	    // Fill the template with data from a JSON string, producing a PDF document.
		PTPDFDoc *pdf_doc = [template_doc FillTemplateJson: json];

	    // Save the PDF to a file.
        [pdf_doc SaveToFile: [NSString stringWithFormat:@"%@/%@", output_path, output_filename]
                     flags: e_ptlinearized];

        NSLog(@"Saved %@\n", output_filename);
        NSLog(@"Done.\n");

        [PTPDFNet Terminate: 0];
        return 0;
    }
}
```

{% endcode %}
{% endtab %}

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

```swift
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import PDFNet
import Foundation

//------------------------------------------------------------------------------
// The following sample illustrates how to use the PTConvert utility class
// to convert MS Office files to PDF and replace templated tags present in the document
// with content supplied via json
//
// For a detailed specification of the template format and supported features,
// see: https://docs.apryse.com/core/guides/generate-via-template/data-model/
//
// This conversion is performed entirely within the PDFNet and has *no*
// external or system dependencies -- Conversion results will be
// the same whether on Windows, Linux or Android.
//
// Please contact us if you have any questions.
//------------------------------------------------------------------------------

func runOfficeToPDFTest() -> Int {
    return autoreleasepool {
        var ret = 0

        do {
            try PTPDFNet.catchException {
                let inputPath: String! = Bundle.main.path(forResource: "SYH_Letter.docx", ofType: "docx")
                let imagePath: String! = Bundle.main.path(forResource: "logo_red.png", ofType: "png")
                let outputPath: String = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("SYH_Letter.pdf").path
                let json: String = String(format:"""
                    "dest_given_name": "Janice N.",
                    "dest_street_address": "187 Duizelstraat",
                    "dest_surname": "Symonds",
                    "dest_title": "Ms.",
                    "land_location": "225 Parc St., Rochelle, QC ",
                    "lease_problem": "According to the city records, the lease was initiated in September 2010 and never terminated",
                    "logo": { "image_url": "%@", "width" : 64, "height":  64 },
                    "sender_name": "Arnold Smith"
                """, imagePath);

                // Start with a PDFDoc (the conversion destination)
                let pdfDoc: PTPDFDoc = PTPDFDoc()

                let options: PTOfficeToPDFOptions = PTOfficeToPDFOptions()
                options.setTemplateParamsJson(json)

		        // perform the conversion with template delimiters and content dictionary
                PTConvert.office(toPDF: pdfDoc, in_filename: inputPath, options: options)

                pdfDoc.save(toFile: outputPath, flags: e_ptremove_unused.rawValue)

                print("Saved: \(outputPath)")
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }

        return ret
    }
}
```

{% 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/ios/get-started/samples/officetemplatetest.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.
