收集 LaTeX 到 PNG 是网页文档、知识基础和数学重量应用的常见要求。 Aspose.Tex for .NET 提供一个简单的转换 API。 .tex
这篇文章显示了一个最小的单个文件转换器和一个强大的集合管道,你可以下载到任何 .NET 应用程序。
原則
- .NET 6 或更晚
- NuGet 包:
Aspose.TeX
- 由 Aspose.TeX 使用的 Object Latex 引擎下编译的 LaTex 来源
ASPOSE.TEX 曝光 TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX)
, ImageDevice
, 和 PngSaveOptions
生产PNG产量。
最小转换: 1 个 LaTeX 文件到 PNG
这个例子转换了 hello-world.ltx
(或 .tex
)到 PNG 在输出文件夹中. 该设备直接写图像文件。
// File: Program.cs
// NuGet: Aspose.TeX
using System;
using System.IO;
using Aspose.TeX;
class Program
{
static void Main()
{
var inputFile = Path.GetFullPath("hello-world.tex"); // or .ltx
var outputDir = Path.GetFullPath("out-png");
Directory.CreateDirectory(outputDir);
// 1) Create conversion options for Object LaTeX
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
// 2) Choose where to write output files
options.OutputWorkingDirectory = new OutputFileSystemDirectory(outputDir);
// 3) Save as PNG (you can set resolution if required)
var png = new PngSaveOptions
{
// Resolution = 300 // uncomment to render at 300 DPI
};
options.SaveOptions = png;
// 4) Run the job. ImageDevice writes PNG files into the output directory.
var device = new ImageDevice();
new TeXJob(inputFile, device, options).Run();
Console.WriteLine("PNG written to: " + outputDir);
}
}
下面是文档模型:创建 TeXOptions
设置 OutputWorkingDirectory
设置 PngSaveOptions
然后跑一 TeXJob
与一 ImageDevice
.
Batch 转换:所有 .tex
文件在文件夹中
序列版本扫描一个输入目录,并将每个 .tex
它还显示如何控制在哪里依赖和包含图形被阅读。
// File: BatchLatexToPng.cs
// NuGet: Aspose.TeX
using System;
using System.IO;
using Aspose.TeX;
public static class BatchLatexToPng
{
public static int Run(string inputDir, string outputDir, int? dpi = null)
{
if (!Directory.Exists(inputDir))
{
Console.Error.WriteLine("Input directory not found: " + inputDir);
return 1;
}
Directory.CreateDirectory(outputDir);
// Configure conversion options once and reuse
var options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
// Where to read auxiliary inputs (e.g., included images)
options.InputWorkingDirectory = new InputFileSystemDirectory(inputDir);
// Where to read extra required packages (if you supply them)
// options.RequiredInputDirectory = new InputFileSystemDirectory(Path.Combine(inputDir, "texmf"));
// Where to write PNG, log, aux, etc.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(outputDir);
// PNG save options (set DPI if needed)
var png = new PngSaveOptions();
if (dpi.HasValue) png.Resolution = dpi.Value;
options.SaveOptions = png;
int ok = 0, err = 0;
var device = new ImageDevice(); // will write images to OutputWorkingDirectory
foreach (var texPath in Directory.GetFiles(inputDir, "*.tex", SearchOption.AllDirectories))
{
try
{
// Optional: set a job name so output files have predictable names
options.JobName = Path.GetFileNameWithoutExtension(texPath);
new TeXJob(texPath, device, options).Run();
Console.WriteLine("OK " + Path.GetRelativePath(inputDir, texPath));
ok++;
}
catch (Exception ex)
{
Console.WriteLine("ERR " + Path.GetRelativePath(inputDir, texPath) + " | " + ex.Message);
err++;
}
}
Console.WriteLine($"Done. Success: {ok}, Failed: {err}");
return err == 0 ? 0 : 2;
}
}
关于Batch工作的关键点:
- 使用
InputWorkingDirectory
包含的资产如\includegraphics{img.png}
. - 使用
RequiredInputDirectory
如果您需要在内置组外提供额外的 LaTeX 包。 - 设置
JobName
根据文件影响输出文件名。
拍摄记忆中的图像而不是写文件
如果您更愿意自行播放 PNG 字节,请让设备通过关闭直接文件写作,将每个页面保存到自己的文件名。
using System;
using System.IO;
using Aspose.TeX;
static void ConvertToStreams(string inputFile, string outputDir)
{
Directory.CreateDirectory(outputDir);
var options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
var png = new PngSaveOptions { DeviceWritesImages = false };
options.SaveOptions = png;
var device = new ImageDevice();
new TeXJob(inputFile, device, options).Run();
for (int i = 0; i < device.Result.Length; i++)
{
var pageBytes = device.Result[i];
var outPath = Path.Combine(outputDir, $"page-{i + 1}.png");
File.WriteAllBytes(outPath, pageBytes);
}
}
这反映了文档的“替代方式”写出主要输出PNG文件。
麻烦和提示
- 解決方案*:增加
PngSaveOptions.Resolution
在网膜屏幕截图或印刷资产中使用更清晰的配方。
- 解決方案*:增加
- 依赖性:在指定的目录中放置自定义包和输入
RequiredInputDirectory
和InputWorkingDirectory
. - 名称:设置
options.JobName
可预测的输出文件名在行程中。 - Logging:发动机编写翻译
.log
到输出目录,这对丢失的包和LaTeX错误的解密有用。
什么时候选择其他格式
PNG 非常适合网页发布和 UI 插入,如果您需要无限振动或规模小文本的 vector 输出,则使用 SVG 转换。 SvgSaveOptions
和一 SvgDevice
,或到 PDF 为付费文件. 转换流保持相同。
使用这些模式,您可以在 .NET 中自动化 LaTeX 到 PNG 规模,同时保持完全控制分辨率、目录和文件处理。