-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample.cs
65 lines (54 loc) · 2.09 KB
/
sample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Requires office.dll and word interop pia.dll from GAC
using Microsoft.Office.Interop.Word;
using System;
using System.IO;
using System.Linq;
namespace OfficePIATest
{
internal static class Program
{
[STAThread]
public static void Main(string[] args)
{
var input = args.ElementAtOrDefault(0) ?? string.Empty;
var output = args.ElementAtOrDefault(1) ?? string.Empty;
if (!File.Exists(input))
{
Console.Error.WriteLine("Cannot open the file `{0}`.", input);
Environment.Exit(1);
return;
}
if (string.IsNullOrWhiteSpace(output))
{
Console.Error.WriteLine("Invalid output path.");
Environment.Exit(2);
return;
}
var outputDir = Path.GetDirectoryName(output);
if (!Directory.Exists(outputDir))
Directory.CreateDirectory(outputDir);
var wordApp = new Application();
Console.Out.WriteLine("word app initialized.");
Document wordDocument = null;
var t = new System.Threading.Tasks.Task(() => wordDocument = wordApp.Documents.Open(input));
t.Start();
t.Wait();
if (wordDocument == null)
{
Console.Error.WriteLine("Word document is null reference.");
Environment.Exit(3);
return;
}
else
Console.Out.WriteLine("Word document opened.");
wordDocument.ExportAsFixedFormat(output, WdExportFormat.wdExportFormatPDF);
Console.Out.WriteLine("Export performed.");
wordDocument.Close(WdSaveOptions.wdDoNotSaveChanges,
WdOriginalFormat.wdOriginalDocumentFormat,
false); //Close document
Console.Write("Closing document");
wordApp.Quit(); //Important: When you forget this Word keeps running in the background
Console.Out.WriteLine("Quitting word app.");
}
}
}