Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pdflatex.exe to Convert TeX to PDF within a C#/WPF Application

Has anyone ever created a PDF document from a TeX document using pdflatex.exe in their C#/WPF application? I have my TeX document and I want to convert it to PDF and display it within the application, however, I'm unsure how to go about doing this and there's virtually nothing that I can find online about doing something like this. Does anyone know what the best way to do something like this is (convert a TeX document to PDF via pdflatex.exe within a C# application)?

Thanks a lot!

like image 310
JToland Avatar asked Oct 11 '22 03:10

JToland


1 Answers

Here's the code I used. It assumes the source is in the same folder as the executable, and includes running BibTeX if you need it (just exclude the second process if needed).

string filename = "<your LaTeX source file>.tex";
Process p1 = new Process();
p1.StartInfo.FileName = "<your path to>\pdflatex.exe";
p1.StartInfo.Arguments = filename;
p1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p1.StartInfo.RedirectStandardOutput = true;
p1.StartInfo.UseShellExecute = false;

Process p2 = new Process();
p2.StartInfo.FileName = "<your path to>\bibtex.exe";
p2.StartInfo.Arguments = Path.GetFileNameWithoutExtension(filename);
p2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p2.StartInfo.RedirectStandardOutput = true;
p2.StartInfo.UseShellExecute = false;

p1.Start();
var output = p1.StandardOutput.ReadToEnd();
p1.WaitForExit();

p2.Start();
output = p2.StandardOutput.ReadToEnd();
p2.WaitForExit();

p1.Start();
output = p1.StandardOutput.ReadToEnd();
p1.WaitForExit();

p1.Start();
output = p1.StandardOutput.ReadToEnd();
p1.WaitForExit();

Yes, this could be cleaned up a little, and certainly without the call to BibTeX could just be called in a loop (3 times is the recommended number to make sure all the references are correct). Also, there's no exception handling, so you might want to add try / catch blocks around the process calls, etc.

like image 57
Darren Oster Avatar answered Oct 14 '22 06:10

Darren Oster