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 Xamarin SDK and Office Document Conversion Library.
1//
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.Drawing;
7
8using pdftron;
9using pdftron.Common;
10using pdftron.Filters;
11using pdftron.SDF;
12using pdftron.PDF;
13
14using NUnit.Framework;
15
16namespace MiscellaneousSamples
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 dependencies
25 ///
26 /// Please contact us if you have any questions.
27 ///---------------------------------------------------------------------------------------
28 /// </summary>
29
30
31
32 [TestFixture]
33 public class OfficeToPDFTest
34 {
35
36 const string input_path = "TestFiles/";
37
38 static void SimpleConvert(String input_filename, String output_filename)
39 {
40 // Start with a PDFDoc (the conversion destination)
41 using (PDFDoc pdfdoc = new PDFDoc())
42 {
43 // perform the conversion with no optional parameters
44 pdftron.PDF.Convert.OfficeToPDF(pdfdoc, Utils.GetAssetTempFile(input_path + input_filename), null);
45
46 // save the result
47 pdfdoc.Save(Utils.CreateExternalFile(output_filename), SDFDoc.SaveOptions.e_linearized);
48
49 // And we're done!
50 Console.WriteLine("Saved " + output_filename);
51 }
52 }
53
54 static void FlexibleConvert(String input_filename, String output_filename)
55 {
56 // Start with a PDFDoc (the conversion destination)
57 using (PDFDoc pdfdoc = new PDFDoc())
58 {
59 OfficeToPDFOptions options = new OfficeToPDFOptions();
60 // create a conversion object -- this sets things up but does not yet
61 // perform any conversion logic.
62 // in a multithreaded environment, this object can be used to monitor
63 // the conversion progress and potentially cancel it as well
64 DocumentConversion conversion = pdftron.PDF.Convert.StreamingPDFConversion(
65 pdfdoc, Utils.GetAssetTempFile(input_path + input_filename), options);
66
67 // actually perform the conversion
68 // this particular method will not throw on conversion failure, but will
69 // return an error status instead
70 if (conversion.TryConvert() == DocumentConversionResult.e_document_conversion_success)
71 {
72 int num_warnings = conversion.GetNumWarnings();
73
74 // print information about the conversion
75 for (int i = 0; i < num_warnings; ++i)
76 {
77 Console.WriteLine("Warning: " + conversion.GetWarningString(i));
78 }
79
80 // save the result
81 pdfdoc.Save(Utils.CreateExternalFile(output_filename), SDFDoc.SaveOptions.e_linearized);
82 // done
83 Console.WriteLine("Saved " + output_filename);
84 }
85 else
86 {
87 Console.WriteLine("Encountered an error during conversion: " + conversion.GetErrorString());
88 }
89 }
90 }
91
92 /// <summary>
93 /// The main entry point for the application.
94 /// </summary>
95 [Test]
96 public static void Sample()
97 {
98
99 try
100 {
101 Utils.GetAssetTempFile(input_path + "pdftron_smart_substitution.plugin");
102 var res = Utils.GetAssetTempFile(input_path + "pdftron_layout_resources.plugin");
103 res = res.Replace("pdftron_layout_resources.plugin", "");
104 PDFNet.SetResourcesPath(res);
105
106 // first the one-line conversion method
107 SimpleConvert("Fishermen.docx", "Fishermen.pdf");
108
109 // then the more flexible line-by-line conversion API
110 FlexibleConvert("the_rime_of_the_ancient_mariner.docx", "the_rime_of_the_ancient_mariner.pdf");
111
112 // conversion of RTL content
113 FlexibleConvert("factsheet_Arabic.docx", "factsheet_Arabic.pdf");
114 }
115 catch (pdftron.Common.PDFNetException e)
116 {
117 Console.WriteLine(e.Message);
118 Assert.True(false);
119 }
120 catch (Exception e)
121 {
122 Console.WriteLine("Unrecognized Exception: " + e.Message );
123 Assert.True(false);
124 }
125
126 Console.WriteLine("Done.");
127 }
128 }
129}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales