优化和压缩C++中的文件

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

使用C++获得最大的文档处理速度。这是用C++压缩 Word、PDF、Web 文档的强大解决方案。免费在线试用!

查看代码片段

在 C++ 中压缩 Word 和 HTML 文档

在不损失质量的情况下使大文件变小。删除不必要和未使用的数据。在 C++ 中缩小 Word 和 HTML 文件的大小。我们的 C++ 库为开发人员提供了一个集成的 API 来优化各种格式的文档。

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

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

通过优化高分辨率图像以及包含此类图像的文档,可以获得最大的效果。我们的 C++ 库将帮助您压缩 HTML、DOCX、DOC、RTF、ODT、EPUB、HTML、JPG、PNG 文件。

使用 'Aspose.Words for C++' 可以轻松优化文档而不降低质量。以下示例显示了如何在 C++ 中缩小 Word 文档的大小:

C++优化文件内容和大小的代码示例
上传要优化/压缩的文件
运行代码
从列表中选择目标格式
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);
运行代码

C++如何压缩PDF、 Word 、JPG等文件格式

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

C++库来优化和压缩文件

有三个选项可以将 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. 版权所有。