在 C++ SDK 中打开和阅读 Word 或 PDF 文件

使用 REST API 在 C++ 中以编程方式打开和读取文档

只需几行 C++ 代码即可轻松打开 Word、PDF 文档。我们的 C++ 库将为您提供以编程方式访问文档内容所需的一切。

查看代码片段

在 C++ 中打开和阅读文档

如果您想打开一个文本文件并以纯文本格式处理数据,您可以使用 C++ 语言和操作系统的最基本功能来完成。至于阅读以多种文件格式(DOCX、DOC、PDF、RTF、ODT、HTML 等)呈现的办公文档,这个任务更难解决,因为这些文档是由复杂的树状数据描述的DOM 模型中的结构。

我们的 C++ 库是一个基于云的解决方案,可让您以任何办公室和 Web 格式打开文档。这种分布式方法使您可以灵活地使用 C++ 语言进行软件开发,因为所有资源密集型文档处理都将在 Aspose 服务器上进行。您只需要获取私钥即可在 C++ 代码中访问 Cloud API。

在 C++ 中以编程方式读取 Word、PDF 文档

您可以迭代文档元素,也可以在 DOM 树中搜索特定元素。我们提供了广泛的 C++ 函数,用于处理文档结构和文档节点中包含的数据。

以下 C++ 代码示例将帮助您快速了解如何加载文档并对其执行一些操作。

上传您要访问的文档
运行代码
using namespace aspose::words::cloud;

auto config = std::make_shared<ApiConfiguration>(L"####-####-####-####-####", 
   L"##################");
auto wordsApi = std::make_shared<WordsApi>(config);
std::wstring fileName = L"Input.docx";

auto requestFileContent = std::shared_ptr<std::istream>(
   new std::ifstream(
      std::filesystem::path(getFileTextUtf16(fileName)), std::istream::binary));
std::shared_ptr<requests::UploadFileRequest> uploadDocumentRequest(
    new requests::UploadFileRequest(
        requestFileContent, std::make_shared<std::wstring>(fileName)
    )
);
wordsApi->uploadFile(uploadDocumentRequest);

auto requestParagraph = std::make_shared<aspose::words::cloud::models::ParagraphInsert>();
requestParagraph->setText(std::make_shared<std::wstring>(
   L"Reading and writing to the file in the cloud occurs automatically."));
std::shared_ptr<requests::InsertParagraphRequest> insertParagraphRequest(
    new requests::InsertParagraphRequest(
        std::make_shared<std::wstring>(fileName), requestParagraph
    )
);
wordsApi->insertParagraph(insertParagraphRequest);

std::shared_ptr<requests::DownloadFileRequest> downloadDocumentRequest(
    new requests::DownloadFileRequest(
        std::make_shared<std::wstring>(fileName)
    )
);
wordsApi->downloadFile(downloadDocumentRequest);
运行代码
  
将代码C++复制到剪贴板

C++中如何阅读Word、PDF文档

  1. 为 C++ 安装"Aspose.Words Cloud"
  2. 将库引用(导入库)添加到您的 C++ 项目
  3. 阅读文档

安装 C++ 库以阅读文档

从 GitHub 克隆 Aspose.Words Cloud SDK for C++ 源代码。您可以在 "How to use the SDK" 部分找到有关构建和配置 SDK 的详细信息。

要快速获取必要的安全凭证并访问我们的 REST API,请按照文档中的这些 Instructions 进行操作。

系统要求

请参阅 Repository Documentation 以查看详细信息。

5%

订阅 Aspose 产品更新

获取直接发送到您的邮箱的每月通讯和优惠。

© Aspose Pty Ltd 2001-2025. 版权所有。