开发者

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开发者_JS百科 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!


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.


I have never done that, but this is perfectly possible. You'll need to use System.Diagnostics.Process class to run pdflatex.exe

The rest is about choosing the way this should run. You probably have some options here:

If pdflatex supports writing output to "standard output" you can intercept that and do with the content whatever you want.

Another option is using some temporary folder to write the file to and subscribing to notifications about folder changes (asynchronous, you can run multiple instances of pdflatex) or simply waiting for the process to finish (synchronous, you can run only one instance of pdflatex at a time).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜