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
LibreOfficeDocumentFileConverter.cs
1using Microsoft.Win32;
2using System.Diagnostics;
3using System.IO.Compression;
4using System.Xml.Linq;
5
7{
12 {
13 private string _libreOfficePath;
14
22 public bool IsAvailable
23 {
24 get
25 {
26 string tmpFile = findLibreOfficePath();
27 if(tmpFile != null)
28 {
29 _libreOfficePath = tmpFile;
30 return true;
31 }
32 return false;
33 }
34 }
35
40 private string? findLibreOfficePath()
41 {
42 object regValue = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\LibreOffice\UNO\InstallPath", "", null);
43 if(regValue == null)
44 {
45 regValue = Registry.GetValue(@"HKEY_LOCAL_MACHINE\WOW6432Node\LibreOffice\UNO\InstallPath", "", null);
46 }
47
48 string sofficePath = Path.Combine(regValue.ToString(), "soffice.exe");
49 if (File.Exists(sofficePath))
50 {
51 return sofficePath;
52 }
53
54 sofficePath = @"C:\Program Files\LibreOffice\program\soffice.exe";
55 if (File.Exists(sofficePath))
56 {
57 return sofficePath;
58 }
59
60 sofficePath = @"C:\Program Files (x86)\LibreOffice\program\soffice.exe";
61 if (File.Exists(sofficePath))
62 {
63 return sofficePath;
64 }
65 return null;
66 }
67
75 public bool Convert(string inputFile, string outputFile)
76 {
77 if(!IsAvailable) { return false; }
78
79 List<string> commandArgs = new List<string>();
80 string convertedFile = "";
81
82 if (string.IsNullOrEmpty(_libreOfficePath) || !File.Exists(_libreOfficePath))
83 {
84 throw new Exception(Properties.Resources.Error_LibreOffice_ApplicationPathError + (string.IsNullOrEmpty(_libreOfficePath) ? "" : (Environment.NewLine + _libreOfficePath)));
85 }
86
87 //Create tmp folder
88 string tmpFolder = Path.Combine(Path.GetDirectoryName(outputFile), "LibreOfficeDocumentConverter_" + Guid.NewGuid().ToString().Substring(0, 10));
89 if (!Directory.Exists(tmpFolder))
90 {
91 Directory.CreateDirectory(tmpFolder);
92 }
93
94 commandArgs.Add("--convert-to");
95
96 if ((inputFile.EndsWith(".html") || inputFile.EndsWith(".htm")) && outputFile.EndsWith(".pdf"))
97 {
98 commandArgs.Add("pdf:writer_pdf_Export");
99 convertedFile = Path.Combine(tmpFolder, Path.GetFileNameWithoutExtension(inputFile) + ".pdf");
100 }
101 else if (inputFile.EndsWith(".docx") && outputFile.EndsWith(".pdf"))
102 {
103 commandArgs.Add("pdf:writer_pdf_Export");
104 convertedFile = Path.Combine(tmpFolder, Path.GetFileNameWithoutExtension(inputFile) + ".pdf");
105 }
106 else if (inputFile.EndsWith(".docx") && (outputFile.EndsWith(".html") || outputFile.EndsWith(".htm")))
107 {
108 commandArgs.Add("html:HTML:EmbedImages");
109 convertedFile = Path.Combine(tmpFolder, Path.GetFileNameWithoutExtension(inputFile) + ".html");
110 }
111 else if ((inputFile.EndsWith(".html") || inputFile.EndsWith(".htm")) && outputFile.EndsWith(".docx"))
112 {
113 commandArgs.Add("docx:\"Office Open XML Text\"");
114 convertedFile = Path.Combine(tmpFolder, Path.GetFileNameWithoutExtension(inputFile) + ".docx");
115 }
116
117 commandArgs.AddRange(new[] { inputFile, "--norestore", "--writer", "--headless", "--outdir", tmpFolder });
118
119 ProcessStartInfo procStartInfo = new ProcessStartInfo(_libreOfficePath);
120 string arguments = "";
121 foreach (var arg in commandArgs) { arguments += arg + " "; }
122 procStartInfo.Arguments = arguments.Trim();
123 procStartInfo.RedirectStandardOutput = true;
124 procStartInfo.UseShellExecute = false;
125 procStartInfo.CreateNoWindow = true;
126 procStartInfo.WorkingDirectory = Environment.CurrentDirectory;
127
128 Process process = new Process() { StartInfo = procStartInfo };
129 process.Start();
130 process.WaitForExit();
131
132 // Check for failed exit code.
133 if (process.ExitCode != 0)
134 {
135 Directory.Delete(tmpFolder, true);
136
137 throw new Exception(string.Format(Properties.Resources.Error_LibreOffice_ConversionFailed, process.ExitCode));
138 }
139 else
140 {
141 if (File.Exists(outputFile))
142 {
143 File.Delete(outputFile);
144 }
145 if (File.Exists(convertedFile))
146 {
147 File.Move(convertedFile, outputFile);
148 }
149 Directory.Delete(tmpFolder, true);
150 return true;
151 }
152 }
153
159 public bool IsDocxCreateWithThisConverter(string docxFile)
160 {
161 ZipArchive zip = ZipFile.OpenRead(docxFile);
162 ZipArchiveEntry zipEntry = zip?.GetEntry("docProps/app.xml");
163 if (zipEntry == null) { return false; }
164
165 Stream zipEntryStream = zipEntry.Open();
166 XDocument zipEntryXml = XDocument.Load(zipEntryStream);
167 XElement zipEntryAppElement = zipEntryXml.Root?.Elements().FirstOrDefault(e => e.Name.LocalName == "Application");
168 string appName = zipEntryAppElement?.Value;
169 return appName.StartsWith("LibreOffice");
170 }
171 }
172}
bool IsDocxCreateWithThisConverter(string docxFile)
Check if the given .docx file was created by Libre Office.
bool Convert(string inputFile, string outputFile)
Convert a document using LibreOffice.
Eine stark typisierte Ressourcenklasse zum Suchen von lokalisierten Zeichenfolgen usw.
static string Error_LibreOffice_ConversionFailed
Sucht eine lokalisierte Zeichenfolge, die LibreOffice failed to convert the .docx file to a ....
static string Error_LibreOffice_ApplicationPathError
Sucht eine lokalisierte Zeichenfolge, die The path to the LibreOffice application is empty or doesn't...