OfficeToPDF

Sample C# code for using Apryse SDK to convert Office documents to PDF (including Word, Excel, PowerPoint and Publisher) without needing any external dependencies or MS Office licenses. Office to PDF conversion can be performed on a Linux or Windows server to automate Office-centric workflows, or entirely in the user's client (web browser, mobile device). The conversion functionality can be combined with our Viewer to display or annotate Office files (docx, xlsx, pptx) on all major platforms, including Web, Android, iOS, Xamarin, UWP, and Windows. Learn more about our UWP SDK and Office Document Conversion Library.

1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.PDF;
11using pdftron.SDF;
12
13using pdftron.Common;
14using PDFNetUniversalSamples.ViewModels;
15
16namespace PDFNetSamples
17{
18 /// <summary>
19 ///---------------------------------------------------------------------------------------
20 /// The following sample illustrates how to use the PDF::Convert utility class to convert
21 /// .docx files to PDF
22 ///
23 /// This conversion is performed entirely within the PDFNet and has *no* external or
24 /// system dependencies
25 ///
26 /// Please contact us if you have any questions.
27 ///---------------------------------------------------------------------------------------
28 /// </summary>
29
30 public sealed class OfficeToPDFTest : Sample
31 {
32 public OfficeToPDFTest() :
33 base("OfficeToPDF", "The following sample illustrates how to use the PDF::Convert utility class to convert .docx files to PDF")
34 {
35 }
36
37 public override IAsyncAction RunAsync()
38 {
39 return Task.Run(new System.Action(async () =>
40 {
41 WriteLine("--------------------------------");
42 WriteLine("Starting OfficeToPDF Test...");
43 WriteLine("--------------------------------\n");
44
45 try
46 {
47 // first the one-line conversion method
48 await SimpleConvert("Fishermen.docx", "Fishermen.pdf");
49
50 // then the more flexible line-by-line conversion API
51 await FlexibleConvert("the_rime_of_the_ancient_mariner.docx", "the_rime_of_the_ancient_mariner.pdf");
52
53 // conversion of RTL content
54 await FlexibleConvert("factsheet_Arabic.docx", "factsheet_Arabic.pdf");
55 }
56 catch (Exception e)
57 {
58 WriteLine("Unrecognized Exception: " + e.Message);
59 }
60
61 WriteLine("--------------------------------");
62 WriteLine("Done OfficeToPDF Test.");
63 WriteLine("--------------------------------\n");
64
65 })).AsAsyncAction();
66 }
67
68 async Task<bool> SimpleConvert(String input_filename, String output_filename)
69 {
70 // Make sure all files exist
71 if (!File.Exists(Path.Combine(InputPath, input_filename)))
72 {
73 WriteLine("File: " + input_filename + " not found!");
74 return false;
75 }
76
77 // Start with a PDFDoc (the conversion destination)
78 using (PDFDoc doc = new PDFDoc())
79 {
80 // perform the conversion with no optional parameters
81 pdftron.PDF.Convert.OfficeToPDF(doc, Path.Combine(InputPath, input_filename), null);
82
83 // save the result
84 await doc.SaveAsync(Path.Combine(OutputPath, output_filename), SDFDocSaveOptions.e_linearized);
85
86 WriteLine("Saved " + output_filename);
87
88 await AddFileToOutputList(Path.Combine(OutputPath, output_filename));
89
90 return true;
91 }
92 }
93
94 async Task<bool> FlexibleConvert(String input_filename, String output_filename)
95 {
96 // Make sure all files exist
97 if (!File.Exists(Path.Combine(InputPath,input_filename)))
98 {
99 WriteLine("File: " + input_filename + " not found!");
100 return false;
101 }
102
103 // Start with a PDFDoc (the conversion destination)
104 using (PDFDoc doc = new PDFDoc())
105 {
106 // Instanciate convertion options
107 OfficeToPDFOptions options = new OfficeToPDFOptions();
108
109 // Smart Substitutions requires a plugin file, here we set the path to it
110 options.SetSmartSubstitutionPluginPath(
111 System.IO.Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Resources"));
112
113 // create a conversion object -- this sets things up but does not yet
114 // perform any conversion logic.
115 // in a multithreaded environment, this object can be used to monitor
116 // the conversion progress and potentially cancel it as well
117 DocumentConversion conversion =
118 pdftron.PDF.Convert.StreamingPDFConversion(doc, Path.Combine(InputPath, input_filename), options);
119
120 // actually perform the conversion
121 // this particular method will not throw on conversion failure, but will
122 // return an error status instead
123 if (conversion.TryConvert() == DocumentConversionResult.e_document_conversion_success)
124 {
125 var num_warnings = conversion.GetNumWarnings();
126
127 // print information about the conversion
128 for (uint i = 0; i < num_warnings; ++i)
129 {
130 WriteLine("Warning: " + conversion.GetWarningString(i));
131 }
132
133 // save the result
134 await doc.SaveAsync(Path.Combine(OutputPath, output_filename), SDFDocSaveOptions.e_linearized);
135
136 WriteLine("Saved " + output_filename);
137
138 await AddFileToOutputList(Path.Combine(OutputPath, output_filename));
139
140 return true;
141 }
142
143 return false;
144 }
145 }
146 }
147}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales