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. Learn more about our iOS SDK and Office Template Generation.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9//------------------------------------------------------------------------------
10// The following sample illustrates how to use the PTConvert utility class
11// to convert MS Office files to PDF and replace templated tags present in the document
12// with content supplied via json
13//
14// For a detailed specification of the template format and supported features,
15// see: https://docs.apryse.com/core/guides/generate-via-template/data-model/
16//
17// This conversion is performed entirely within the PDFNet and has *no*
18// external or system dependencies -- Conversion results will be
19// the same whether on Windows, Linux or Android.
20//
21// Please contact us if you have any questions.
22//------------------------------------------------------------------------------
23
24int main(int argc, char *argv[])
25{
26
27 @autoreleasepool {
28
29 [PTPDFNet Initialize: 0];
30
31 NSString *input_path = @"../../TestFiles/";
32 NSString *output_path = @"../../TestFiles/Output/";
33 NSString *input_filename = @"SYH_Letter.docx";
34 NSString *output_filename = @"SYH_Letter.pdf";
35 NSString *json = [NSString stringWithFormat: @"{\"dest_given_name\": \"Janice N.\", "
36 @"\"dest_street_address\": \"187 Duizelstraat\", \"dest_surname\": \"Symonds\", "
37 @"\"dest_title\": \"Ms.\", \"land_location\": \"225 Parc St., Rochelle, QC \","
38 @"\"lease_problem\": \"According to the city records, the lease was initiated in September 2010 and never terminated\", "
39 @"\"logo\": {\"image_url\": \"%@logo_red.png\", \"width\" : 64, \"height\" : 64},"
40 @"\"sender_name\": \"Arnold Smith\"}", input_path];
41
42 PTOfficeToPDFOptions* options = [[PTOfficeToPDFOptions alloc] init];
43
44 // Create a TemplateDocument object from an input office file.
45 PTTemplateDocument *template_doc = [PTConvert CreateOfficeTemplate: [NSString stringWithFormat:@"%@/%@", input_path, input_filename]
46 options: options];
47
48 // Fill the template with data from a JSON string, producing a PDF document.
49 PTPDFDoc *pdf_doc = [template_doc FillTemplateJson: json];
50
51 // Save the PDF to a file.
52 [pdf_doc SaveToFile: [NSString stringWithFormat:@"%@/%@", output_path, output_filename]
53 flags: e_ptlinearized];
54
55 NSLog(@"Saved %@\n", output_filename);
56 NSLog(@"Done.\n");
57
58 [PTPDFNet Terminate: 0];
59 return 0;
60 }
61}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales