如何使用 C++ 将 RTF 转换为 PDF

本快速教程介绍了如何使用 C++** 将 RTF 转换为 PDF 的过程,提供了在 C++ 中配置环境和可运行代码的说明以及有关必要的头文件和命名空间的信息。在使用 C++** 编写 **RTF 到 PDF 转换器时,我们将学习源 RTF 文件的加载,然后通过设置渲染内容的颜色模式来创建自定义的 PDF 文件。最后,由 C++ 代码生成的输出 PDF 文件将保存在磁盘上。

使用 C++ 将 RTF 转换为 PDF 的步骤

  1. 使用 NugGet 包管理器将 Aspose.Words.Cpp 库的引用添加到应用程序中
  2. 包括所需的头文件和必要的命名空间
  3. 将源 RTF 文件加载到 Document 类对象中,以便在 C++ 中转换为 PDF
  4. 创建 PdfSaveOptions 类对象以自定义输出 PDF 文件
  5. 设置输出 PDF 文件颜色模式以将内容渲染为 PDF
  6. 使用所需的配置保存输出 PDF 文件

这些步骤提供了环境配置所需资源的参考,以及使用 C++* 将富文本文档转换为 PDF 的有序步骤序列。一旦添加了所有必需的头文件并包含了必要的命名空间,我们将使用 Document 类对象加载源 RTF 文件,该对象可以通过多种方式加载它,例如从流加载或使用 LoadOptions 类对象来控制文件加载过程。最后,通过设置颜色模式属性将自定义的 PDF 文件保存在磁盘上,但是 PdfSaveOptions 类中还有许多其他选项可用。

使用 C++ 将 RTF 更改为 PDF 的代码

#include <Aspose.Words.Cpp/Document.h>
#include <Aspose.Words.Cpp/Saving/PdfSaveOptions.h>
#include <Aspose.Words.Cpp/License.h>
using namespace System;
using System::MakeObject;
using System::SharedPtr;
using namespace Aspose::Words;
using namespace Aspose::Words::Saving;
class RtfToPdfConvertor
{
public:
static void RtfToPdfConversion()
{
try {
// Set File name and path of license file
System::String licenseFileName = u"Aspose.Total.NET.lic";
// Create an instance of Aspose.Words license to avoid watermark in output PDF
SharedPtr<Aspose::Words::License> wordsLicense = System::MakeObject<Aspose::Words::License>();
// Set the above mentioned license
wordsLicense->SetLicense(licenseFileName);
// Load the sample RTF file into Document class object
SharedPtr<Document> RtfDocumentUsingCPP = MakeObject<Document>(u"SampleRtf.rtf");
// Create an object off PdfSaveOptions class
SharedPtr<PdfSaveOptions> pdfSaveOptions = MakeObject<PdfSaveOptions>();
// Set the output PDF color mode for rendering the contents
pdfSaveOptions->set_ColorMode(ColorMode::Grayscale);
// Save RTF to PDF in C++ using Save method
RtfDocumentUsingCPP->Save(u"RTF_To_PDF_using_CPP.pdf", pdfSaveOptions);
}
catch (Exception e)
{
}
}
};

此代码使用 C++* 通过创建和使用 PdfSaveOptions 类对象将 *富文本格式转换为 PDF。我们使用了 PdfSaveOptions 类,其中包含许多其他属性,例如 set_Compliance() 设置输出 PDF 标准合规级别,set_DigitalSignatureDetails() 设置签署 PDF 的详细信息,set_DisplayDocTitle() 设置文档标题,同时在查看器中显示举几个例子。

本教程教我们使用 C++ 将 RTF 导出为 PDF 的过程。如果您有兴趣了解将 Word 转换为 PDF 的过程,请参阅文章 如何使用 C++ 将 Word 转换为 PDF

 简体中文