C++中压缩Word的代码

用于优化Word内容和大小的高性能C++库,保留原始质量和基线数据

这是在C++中优化和压缩Word的专业解决方案。使用C++最大限度地提高Word优化/压缩速度。免费在线试用!

查看代码片段

在 C++ 中压缩 Word 文件

使大的 Word 文件更小而不损失质量。删除不必要和未使用的数据。在代码中缩小 Word 文件的大小。 C++ 库为开发人员提供了一个集成的 API 来优化 Word 内容。

使用 C++ 减小 Word 文件的大小

C++ 库是一个独立的解决方案,不需要安装任何第三方软件。

使用 'Aspose.Words for C++' 可以轻松实现无损压缩和深度内容优化。以下示例演示了如何优化 C++ 中的 C++ Word 文件的内容:

C++中用于压缩DOCX文件的代码示例
上传要优化/压缩的文件
运行代码
从列表中选择目标格式
using namespace Aspose::Words;

auto doc = MakeObject<Document>(u"Input.docx");
doc->Cleanup();

auto nodes = doc->GetChildNodes(NodeType::Shape, true);
for (const auto& shape : System::IterateOver<Shape>(nodes))
{
    if (shape->get_HasImage())
    {
        // 由开发人员选择用于图像压缩的库。
        auto image = 
           System::Drawing::Image::FromStream(shape->get_ImageData()->ToStream());

        // ...
        // 压缩图像并将其设置回形状。
        shape->get_ImageData()->SetImage("yourCompressedImage");
    }
}

auto saveOptions = MakeObject<OoxmlSaveOptions>();
saveOptions->set_CompressionLevel(CompressionLevel::Maximum);

doc->Save(u"Output.docx", saveOptions);
using namespace Aspose::Words; auto doc = MakeObject<Document>(u"Input.docx"); doc->Cleanup(); auto nodes = doc->GetChildNodes(NodeType::Shape, true); for (const auto& shape : System::IterateOver<Shape>(nodes)) { if (shape->get_HasImage()) { // 由开发人员选择用于图像压缩的库。 auto image = System::Drawing::Image::FromStream(shape->get_ImageData()->ToStream()); // ... // 压缩图像并将其设置回形状。 shape->get_ImageData()->SetImage("yourCompressedImage"); } } auto saveOptions = MakeObject<PdfSaveOptions>(); saveOptions->set_CacheBackgroundGraphics(true); doc->Save(u"Output.docx", saveOptions); using namespace Aspose::Words; auto doc = MakeObject<Document>(u"Input.docx"); doc->Cleanup(); auto nodes = doc->GetChildNodes(NodeType::Shape, true); for (const auto& shape : System::IterateOver<Shape>(nodes)) { if (shape->get_HasImage()) { // 由开发者选择用于图像压缩的库。 auto image = System::Drawing::Image::Fromstream(shape->get_ImageData()->ToStream()); // ... // 压缩图像并将其设置回形状。 shape->get_ImageData()->SetImage("yourCompressedImage"); } } doc->Save(u"Output.docx"); using namespace Aspose::Words; auto doc = MakeObject<Document>(u"Input.docx"); doc->Cleanup(); auto nodes = doc->GetChildNodes(NodeType::Shape, true); for (const auto& shape : System::IterateOver<Shape>(nodes)) { if (shape->get_HasImage()) { // 由开发人员选择用于图像压缩的库。 auto image = System::Drawing::Image::FromStream(shape->get_ImageData()->ToStream()); // ... // 压缩图像并将其设置回形状。 shape->get_ImageData()->SetImage("yourCompressedImage"); } } auto saveOptions = MakeObject<OoxmlSaveOptions>(); saveOptions->set_CompressionLevel(CompressionLevel::Maximum); doc->Save(u"Output.docx", saveOptions); using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto shape = builder->InsertImage(u"Input.docx"); // 更新页面设置以裁剪文档以适合图片的大小。 auto pageSetup = builder->get_PageSetup(); pageSetup->set_PageWidth(shape->get_Width()); pageSetup->set_PageHeight(shape->get_Height()); pageSetup->set_TopMargin(0); pageSetup->set_LeftMargin(0); pageSetup->set_BottomMargin(0); pageSetup->set_RightMargin(0); auto saveOptions = MakeObject<ImageSaveOptions>(SaveFormat::Docx); doc->Save(u"Output.docx", saveOptions);
运行代码

如何压缩Word

  1. 安装Aspose.Words for C++ 。
  2. 将库引用(导入库)添加到您的C++项目。
  3. 在C++中打开源Word文件。
  4. 在不损失质量的情况下压缩Word 。
  5. 调用Document.Cleanup()方法。结果代码可能因输出文件格式而异。
  6. 将压缩结果作为Word文件获取。

C++库用于优化和压缩WORD个文件

有三个选项可以将 Aspose.Words for C++ 安装到您的开发人员环境中。请选择符合您需求的一种,并按照分步说明进行操作:

系统要求

您可以使用这个 C++ 库在 Microsoft Windows 、Linux 和 macOS 操作系统上开发软件:

  • Linux 需要 GCC >= 6.3.0 和 Clang >= 3.9.1
  • Xcode >= 12.5.1 ,macOS 需要 Clang 和 libc++

如果您为 Linux 或 macOS 开发软件,请查看产品文档中 fontconfig 和 mesa-glu 开源包)的信息。

其他支持的文件格式优化

您可以优化许多其他文件格式的文件:

5%

订阅 Aspose 产品更新

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

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