OfficeTemplate

Sample 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 Server SDK and Office Template Generation.

1//
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3//
4
5using System;
6using System.Drawing;
7using System.Drawing.Drawing2D;
8
9using pdftron;
10using pdftron.Common;
11using pdftron.Filters;
12using pdftron.SDF;
13using pdftron.PDF;
14
15namespace OfficeTemplateTestCS
16{
17 /// <summary>
18 ///---------------------------------------------------------------------------------------
19 /// The following sample illustrates how to use the PDF::Convert utility class
20 /// to convert MS Office files to PDF and replace templated tags present in the document
21 /// with content supplied via json
22 ///
23 /// For a detailed specification of the template format and supported features,
24 /// see: https://docs.apryse.com/core/guides/generate-via-template/data-model/
25 ///
26 /// This conversion is performed entirely within the PDFNet and has *no* external or
27 /// system dependencies dependencies
28 ///
29 /// Please contact us if you have any questions.
30 ///---------------------------------------------------------------------------------------
31 /// </summary>
32
33
34
35 class Class1
36 {
37 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
38 static Class1() { }
39
40 static String input_path = "../../../../TestFiles/";
41 static String output_path = "../../../../TestFiles/Output/";
42 static String json = "{\"dest_given_name\": \"Janice N.\", \"dest_street_address\": \"187 Duizelstraat\", \"dest_surname\": \"Symonds\", \"dest_title\": \"Ms.\", \"land_location\": \"225 Parc St., Rochelle, QC \"," +
43 "\"lease_problem\": \"According to the city records, the lease was initiated in September 2010 and never terminated\", \"logo\": {\"image_url\": \"" + input_path + "logo_red.png\", \"width\" : 64, \"height\" : 64}," +
44 "\"sender_name\": \"Arnold Smith\"}";
45
46 static String input_filename = "SYH_Letter.docx";
47 static String output_filename = "SYH_Letter.pdf";
48
49 /// <summary>
50 /// The main entry point for the application.
51 /// </summary>
52 static void Main(string[] args)
53 {
54 PDFNet.Initialize(PDFTronLicense.Key);
55
56 try
57 {
58 // Create a TemplateDocument object from an input office file.
59 using (TemplateDocument template_doc = pdftron.PDF.Convert.CreateOfficeTemplate(input_path + input_filename, null))
60 {
61 // Fill the template with data from a JSON string, producing a PDF document.
62 PDFDoc pdfdoc = template_doc.FillTemplateJson(json);
63
64 // Save the PDF to a file.
65 pdfdoc.Save(output_path + output_filename, SDFDoc.SaveOptions.e_linearized);
66
67 // And we're done!
68 Console.WriteLine("Saved " + output_filename);
69 }
70 }
71 catch (pdftron.Common.PDFNetException e)
72 {
73 Console.WriteLine(e.Message);
74 }
75 catch (Exception e)
76 {
77 Console.WriteLine("Unrecognized Exception: " + e.Message);
78 }
79
80 PDFNet.Terminate();
81 Console.WriteLine("Done.");
82 }
83 }
84}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales