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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import PDFNet
7import Foundation
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
24func runOfficeToPDFTest() -> Int {
25 return autoreleasepool {
26 var ret = 0
27
28 do {
29 try PTPDFNet.catchException {
30 let inputPath: String! = Bundle.main.path(forResource: "SYH_Letter.docx", ofType: "docx")
31 let imagePath: String! = Bundle.main.path(forResource: "logo_red.png", ofType: "png")
32 let outputPath: String = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("SYH_Letter.pdf").path
33 let json: String = String(format:"""
34 "dest_given_name": "Janice N.",
35 "dest_street_address": "187 Duizelstraat",
36 "dest_surname": "Symonds",
37 "dest_title": "Ms.",
38 "land_location": "225 Parc St., Rochelle, QC ",
39 "lease_problem": "According to the city records, the lease was initiated in September 2010 and never terminated",
40 "logo": { "image_url": "%@", "width" : 64, "height": 64 },
41 "sender_name": "Arnold Smith"
42 """, imagePath);
43
44 // Start with a PDFDoc (the conversion destination)
45 let pdfDoc: PTPDFDoc = PTPDFDoc()
46
47 let options: PTOfficeToPDFOptions = PTOfficeToPDFOptions()
48 options.setTemplateParamsJson(json)
49
50 // perform the conversion with template delimiters and content dictionary
51 PTConvert.office(toPDF: pdfDoc, in_filename: inputPath, options: options)
52
53 pdfDoc.save(toFile: outputPath, flags: e_ptremove_unused.rawValue)
54
55 print("Saved: \(outputPath)")
56 }
57 } catch let e as NSError {
58 print("\(e)")
59 ret = 1
60 }
61
62 return ret
63 }
64}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales