Vereinsmeisterschaften  22aa7800eae54b428d40e835886cefe1fdefdfdf
This is a software that can be used to manage the internal competition of the swimming club Illertissen called "Vereinsmeisterschaften".
Loading...
Searching...
No Matches
MsWordDocumentFileConverter.cs
1using System.IO.Compression;
2using System.Xml.Linq;
3using Microsoft.Win32;
4
6{
11 {
17 public bool IsAvailable
18 {
19 get
20 {
21 object regValue = Registry.GetValue(@"HKEY_CLASSES_ROOT\Word.Application\CurVer", "", null); // Format will be e.g. "Word.Application.14"
22 return regValue != null;
23 }
24 }
25
32 public bool Convert(string inputFile, string outputFile)
33 {
34 if (!IsAvailable) { return false; }
35
36 string convertedFile = "";
37
38 //Create tmp folder
39 string tmpFolder = Path.Combine(Path.GetDirectoryName(outputFile), "MSWordDocumentConverter_" + Guid.NewGuid().ToString().Substring(0, 10));
40 if (!Directory.Exists(tmpFolder))
41 {
42 Directory.CreateDirectory(tmpFolder);
43 }
44
45 dynamic wordApp = null;
46 dynamic document = null;
47
48 try
49 {
50 if (inputFile.EndsWith(".docx") && outputFile.EndsWith(".docx"))
51 {
52 // No conversion necessary. Only copy the inputFile to the outputFile.
53 File.Copy(inputFile, outputFile, true);
54 return true;
55 }
56 else if (inputFile.EndsWith(".docx") && outputFile.EndsWith(".pdf"))
57 {
58 convertedFile = Path.Combine(tmpFolder, Path.GetFileNameWithoutExtension(inputFile) + ".pdf");
59
60 // Microsoft.Office.Interop.Word doesn't work correct. So use a dynamic Instance as workaround.
61 Type t = Type.GetTypeFromProgID("Word.Application", true);
62 wordApp = Activator.CreateInstance(t);
63 wordApp.Visible = false;
64 document = wordApp.Documents.Open(inputFile);
65 document.ExportAsFixedFormat(convertedFile, 17); // 17 = WdExportFormat.wdExportFormatPDF
66
67 if (File.Exists(outputFile))
68 {
69 File.Delete(outputFile);
70 }
71 if (File.Exists(convertedFile))
72 {
73 File.Move(convertedFile, outputFile);
74 }
75 return true;
76 }
77 else
78 {
79 // conversion of these file types isn't supported.
80 return false;
81 }
82 }
83 catch (Exception ex)
84 {
85 throw new Exception(string.Format(Properties.Resources.Error_MSWord_ConversionFailed, ex.Message));
86 }
87 finally
88 {
89 Directory.Delete(tmpFolder, true);
90 document?.Close(false);
91 wordApp?.Quit(false);
92 }
93 }
94
100 public bool IsDocxCreateWithThisConverter(string docxFile)
101 {
102 ZipArchive zip = ZipFile.OpenRead(docxFile);
103 ZipArchiveEntry zipEntry = zip?.GetEntry("docProps/app.xml");
104 if (zipEntry == null) { return false; }
105
106 Stream zipEntryStream = zipEntry.Open();
107 XDocument zipEntryXml = XDocument.Load(zipEntryStream);
108 XElement zipEntryAppElement = zipEntryXml.Root?.Elements().FirstOrDefault(e => e.Name.LocalName == "Application");
109 string appName = zipEntryAppElement?.Value;
110 return appName.Contains("Microsoft Office Word");
111 }
112 }
113}
bool Convert(string inputFile, string outputFile)
Convert a document using Word.
bool IsDocxCreateWithThisConverter(string docxFile)
Check if the given .docx file was created by Microsoft Word.
Eine stark typisierte Ressourcenklasse zum Suchen von lokalisierten Zeichenfolgen usw.
static string Error_MSWord_ConversionFailed
Sucht eine lokalisierte Zeichenfolge, die Microsoft Word failed to convert the .docx file to a ....