42 object regValue = Registry.GetValue(
@"HKEY_LOCAL_MACHINE\SOFTWARE\LibreOffice\UNO\InstallPath",
"",
null);
45 regValue = Registry.GetValue(
@"HKEY_LOCAL_MACHINE\WOW6432Node\LibreOffice\UNO\InstallPath",
"",
null);
48 string sofficePath = Path.Combine(regValue.ToString(),
"soffice.exe");
49 if (File.Exists(sofficePath))
54 sofficePath =
@"C:\Program Files\LibreOffice\program\soffice.exe";
55 if (File.Exists(sofficePath))
60 sofficePath =
@"C:\Program Files (x86)\LibreOffice\program\soffice.exe";
61 if (File.Exists(sofficePath))
75 public bool Convert(
string inputFile,
string outputFile)
79 List<string> commandArgs =
new List<string>();
80 string convertedFile =
"";
82 if (
string.IsNullOrEmpty(_libreOfficePath) || !File.Exists(_libreOfficePath))
88 string tmpFolder = Path.Combine(Path.GetDirectoryName(outputFile),
"LibreOfficeDocumentConverter_" + Guid.NewGuid().ToString().Substring(0, 10));
89 if (!Directory.Exists(tmpFolder))
91 Directory.CreateDirectory(tmpFolder);
94 commandArgs.Add(
"--convert-to");
96 if ((inputFile.EndsWith(
".html") || inputFile.EndsWith(
".htm")) && outputFile.EndsWith(
".pdf"))
98 commandArgs.Add(
"pdf:writer_pdf_Export");
99 convertedFile = Path.Combine(tmpFolder, Path.GetFileNameWithoutExtension(inputFile) +
".pdf");
101 else if (inputFile.EndsWith(
".docx") && outputFile.EndsWith(
".pdf"))
103 commandArgs.Add(
"pdf:writer_pdf_Export");
104 convertedFile = Path.Combine(tmpFolder, Path.GetFileNameWithoutExtension(inputFile) +
".pdf");
106 else if (inputFile.EndsWith(
".docx") && (outputFile.EndsWith(
".html") || outputFile.EndsWith(
".htm")))
108 commandArgs.Add(
"html:HTML:EmbedImages");
109 convertedFile = Path.Combine(tmpFolder, Path.GetFileNameWithoutExtension(inputFile) +
".html");
111 else if ((inputFile.EndsWith(
".html") || inputFile.EndsWith(
".htm")) && outputFile.EndsWith(
".docx"))
113 commandArgs.Add(
"docx:\"Office Open XML Text\"");
114 convertedFile = Path.Combine(tmpFolder, Path.GetFileNameWithoutExtension(inputFile) +
".docx");
117 commandArgs.AddRange(
new[] { inputFile,
"--norestore",
"--writer",
"--headless",
"--outdir", tmpFolder });
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;
128 Process process =
new Process() { StartInfo = procStartInfo };
130 process.WaitForExit();
133 if (process.ExitCode != 0)
135 Directory.Delete(tmpFolder,
true);
141 if (File.Exists(outputFile))
143 File.Delete(outputFile);
145 if (File.Exists(convertedFile))
147 File.Move(convertedFile, outputFile);
149 Directory.Delete(tmpFolder,
true);
161 ZipArchive zip = ZipFile.OpenRead(docxFile);
162 ZipArchiveEntry zipEntry = zip?.GetEntry(
"docProps/app.xml");
163 if (zipEntry ==
null) {
return false; }
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");