在 C#/WPF 应用程序中使用 pdflatex.exe 将 TeX 转换为 PDF

发布于 2024-11-04 17:53:44 字数 210 浏览 5 评论 0原文

有人曾经在 C#/WPF 应用程序中使用 pdflatex.exe 从 TeX 文档创建过 PDF 文档吗?我有我的 TeX 文档,我想将其转换为 PDF 并在应用程序中显示它,但是,我不确定如何执行此操作,而且我在网上几乎找不到任何关于执行此类操作的信息。有谁知道做这样的事情的最佳方法是什么(通过 C# 应用程序中的 pdflatex.exe 将 TeX 文档转换为 PDF)?

多谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终陌 2024-11-11 17:53:44

这是我使用的代码。它假设源代码与可执行文件位于同一文件夹中,并且如果需要,则包括运行 BibTeX(如果需要,只需排除第二个进程)。

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();

是的,这可以稍微清理一下,当然,如果不调用 BibTeX,则可以在循环中调用(建议使用 3 次,以确保所有引用都正确)。此外,没有异常处理,因此您可能需要在进程调用等周围添加 try / catch 块。

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.

时光病人 2024-11-11 17:53:44

我从来没有这样做过,但这完全有可能。您需要使用 System.Diagnostics.Process运行 pdflatex.exe 的类

剩下的就是选择它的运行方式。您可能在这里有一些选择:

如果 pdflatex 支持将输出写入“标准输出”,您可以拦截它并按照您想要的方式处理内容。

另一种选择是使用某个临时文件夹写入文件并订阅有关文件夹更改的通知(异步,您可以运行 pdflatex 的多个实例)或只是等待该过程完成(同步,您只能运行一个 pdflatex 实例)一次)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文