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 Server SDK and Office Document Conversion Library.

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 OfficeToPDFTestCS
16{
17 /// <summary>
18 ///---------------------------------------------------------------------------------------
19 /// The following sample illustrates how to use the PDF::Convert utility class to convert
20 /// .docx files to PDF
21 ///
22 /// This conversion is performed entirely within the PDFNet and has *no* external or
23 /// system dependencies dependencies
24 ///
25 /// Please contact us if you have any questions.
26 ///---------------------------------------------------------------------------------------
27 /// </summary>
28
29
30
31 class Class1
32 {
33 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
34 static Class1() {}
35
36 static String input_path = "../../../../TestFiles/";
37 static String output_path = "../../../../TestFiles/Output/";
38
39 static void SimpleConvert(String input_filename, String output_filename)
40 {
41 // Start with a PDFDoc (the conversion destination)
42 using (PDFDoc pdfdoc = new PDFDoc())
43 {
44 // perform the conversion with no optional parameters
45 pdftron.PDF.Convert.OfficeToPDF(pdfdoc, input_path + input_filename, null);
46
47 // save the result
48 pdfdoc.Save(output_path + output_filename, SDFDoc.SaveOptions.e_linearized);
49
50 // And we're done!
51 Console.WriteLine("Saved " + output_filename);
52 }
53 }
54
55 static void FlexibleConvert(String input_filename, String output_filename)
56 {
57 // Start with a PDFDoc (the conversion destination)
58 using (PDFDoc pdfdoc = new PDFDoc())
59 {
60 OfficeToPDFOptions options = new OfficeToPDFOptions();
61 options.SetSmartSubstitutionPluginPath(input_path);
62 // create a conversion object -- this sets things up but does not yet
63 // perform any conversion logic.
64 // in a multithreaded environment, this object can be used to monitor
65 // the conversion progress and potentially cancel it as well
66 DocumentConversion conversion = pdftron.PDF.Convert.StreamingPDFConversion(
67 pdfdoc, input_path + input_filename, options);
68
69 // actually perform the conversion
70 // this particular method will not throw on conversion failure, but will
71 // return an error status instead
72 if (conversion.TryConvert() == DocumentConversionResult.e_document_conversion_success)
73 {
74 int num_warnings = conversion.GetNumWarnings();
75
76 // print information about the conversion
77 for (int i = 0; i < num_warnings; ++i)
78 {
79 Console.WriteLine("Warning: " + conversion.GetWarningString(i));
80 }
81
82 // save the result
83 pdfdoc.Save(output_path + output_filename, SDFDoc.SaveOptions.e_linearized);
84 // done
85 Console.WriteLine("Saved " + output_filename);
86 }
87 else
88 {
89 Console.WriteLine("Encountered an error during conversion: " + conversion.GetErrorString());
90 }
91 }
92 }
93
94 /// <summary>
95 /// The main entry point for the application.
96 /// </summary>
97 static void Main(string[] args)
98 {
99 PDFNet.Initialize(PDFTronLicense.Key);
100
101 try
102 {
103 // first the one-line conversion method
104 SimpleConvert("Fishermen.docx", "Fishermen.pdf");
105
106 // then the more flexible line-by-line conversion API
107 FlexibleConvert("the_rime_of_the_ancient_mariner.docx", "the_rime_of_the_ancient_mariner.pdf");
108
109 // conversion of RTL content
110 FlexibleConvert("factsheet_Arabic.docx", "factsheet_Arabic.pdf");
111 }
112 catch (pdftron.Common.PDFNetException e)
113 {
114 Console.WriteLine(e.Message);
115 }
116 catch (Exception e)
117 {
118 Console.WriteLine("Unrecognized Exception: " + e.Message );
119 }
120
121 PDFNet.Terminate();
122 Console.WriteLine("Done.");
123 }
124 }
125}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales