Some test text!

Loading...
Guides

Office template generation in Obj-C

More languages

More languages
C++
C#
C# (.NET Core)
Go
Java
Obj-C
JS (Node.js)
PHP
Python
Ruby
Swift
VB

Sample Obj-C code for using PDFTron 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 Obj-C PDF Library and Office Template Generation.

Get Started Samples Download

To run this sample, get started with a free trial of PDFTron SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2021 by PDFTron Systems 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://www.pdftron.com/documentation/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];

	    // Start with a PDFDoc (the conversion destination)
	    PTPDFDoc* pdfDoc = [[PTPDFDoc alloc] init];

	    PTOfficeToPDFOptions* options = [[PTOfficeToPDFOptions alloc] init];
	    [options SetTemplateParamsJson:json];

	    // perform the conversion with template delimiters and content dictionary
	    [PTConvert OfficeToPDF:pdfDoc
	               in_filename:[NSString stringWithFormat:@"%@/%@", input_path, input_filename]
	               options:options];

	    [pdfDoc SaveToFile: [NSString stringWithFormat:@"%@/%@", output_path, output_filename]
	                 flags: e_ptlinearized];

	    NSLog(@"Saved: %@/%@\n", output_path, output_filename);

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

Upcoming Webinar: How Developers Add Office Viewing Without Downloading | June 9th at 11 am PT

PDFTron SDK

The Platform

NEW

© 2022 PDFTron Systems Inc. All rights reserved.

Privacy

Terms of Use