将输出写入 ZIP

通过 C++ 将 TeX 文件转换结果保存为 ZIP

 

TeX 是一种数据标记语言,也是计算机排版系统(也称为出版排版系统)的核心。虽然它通常被称为文件格式,但它实际上是一种用于创建数学、技术和其他复杂文档的编程语言和解释器引擎。然而,一旦创建了文件,可能需要将其转换为更流行的格式,以便能够在任何设备和平台上使用结果。

Aspose.TeX API 解决方案提供将 TeX 文件转换并保存为 ZIP 压缩包的功能。此处提供的代码片段显示了如何将 TeX 文件转换为 PDF 并将输出保存为 zip 文件。该 API 转换器可以使用 C++ 语言转换 TeX 文件,并可用于创建跨平台应用程序或集成到您的 C++ 项目中。

要运行示例,您需要 Aspose.TeX for C++ API,这是适用于 C++ 平台的配置丰富且易于使用的文档操作和转换工具。您可以通过在 NuGet 包管理器中搜索或在包管理器控制台中使用 Install-Package Aspose.TeX 命令来安装 Aspose.TeX API。

Package Manager Console Command


    PM> Install-Package Aspose.TeX

这段代码正在转换一个 TeX 文件并将其作为 ZIP 压缩包中的 PDF 保存。按照以下步骤处理此类转换:

  1. 创建一个 TeXOptions 类实例。使用它通过 OutputWorkingDirectory 属性指定输出的 ZIP 压缩包工作目录。
  2. 使用 OutputConsoleTerminal 类将控制台指定为输出终端。
  3. 使用 PdfSaveOptions 类定义保存输出的选项。
  4. 使用针对 PdfDeviceTeXJob 方法运行 TeX 到 PDF 的转换。
  5. 细化输出。
  6. 使用 OutputZipDirectory 类完成输出 ZIP 压缩包。代码使用 try-catch 块来处理过程中可能发生的任何异常。

使用 ZIP 目录进行输出的 C++ 代码

    using Aspose::TeX::IO;
    using Aspose::TeX::Presentation::Pdf;
    using System::IO;
    // Open a stream on a ZIP archive that will serve as the input working directory.
    {
        System::SharedPtr<System::IO::Stream> inZipStream = System::IO::File::Open(System::IO::Path::Combine(RunExamples::InputDirectory, u"zip-in.zip"), System::IO::FileMode::Open);
        // Clearing resources under 'using' statement
        System::Details::DisposeGuard<1> __dispose_guard_1({ inZipStream});
        // ------------------------------------------

        try{
            System::SharedPtr<System::IO::Stream> outZipStream = System::IO::File::Open(System::IO::Path::Combine(RunExamples::OutputDirectory, u"zip-pdf-out.zip"), System::IO::FileMode::Create);
            // Clearing resources under 'using' statement
            System::Details::DisposeGuard<1> __dispose_guard_0({ outZipStream});
            // ------------------------------------------

            try
            {
                // Create typesetting options for default ObjectTeX format on ObjectTeX engine extension.
                System::SharedPtr<TeXOptions> options = TeXOptions::ConsoleAppOptions(TeXConfig::ObjectTeX());
                // Specify a ZIP archive working directory for the input.
                options->set_InputWorkingDirectory(System::MakeObject<InputZipDirectory>(inZipStream, u"in"));
                // Specify a ZIP archive working directory for the output.
                options->set_OutputWorkingDirectory(System::MakeObject<OutputZipDirectory>(outZipStream));
                // Specify the console as an output terminal.
                options->set_TerminalOut(System::MakeObject<OutputConsoleTerminal>());
                // Default. Not necessary to specify.

                // Create and specify saving options.
                options->set_SaveOptions(System::MakeObject<PdfSaveOptions>());
                // Run typesetting.
                Aspose::TeX::TeX::Typeset(u"hello-world", System::MakeObject<PdfDevice>(), options);

                // For the consequent output to look right. 
                options->get_TerminalOut()->get_Writer()->WriteLine();

                // Finalize the output ZIP archive.
                (System::DynamicCast<Aspose::TeX::IO::OutputZipDirectory>(options->get_OutputWorkingDirectory()))->Finish();
            }
            catch(...)
            {
                __dispose_guard_0.SetCurrentException(std::current_exception());
            }
        }
        catch(...)
        {
            __dispose_guard_1.SetCurrentException(std::current_exception());
        }
    }

TeX What is TeX File Format

TeX 是一种排版系统和编程语言,用于创建高质量文档,尤其是数学公式。TeX 源文件为纯文本,由 TeX 引擎(如 pdfTeX、XeTeX、LuaTeX)处理,可生成 PDF、DVI 等输出。