尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

DevExpress(WinForms WPF)中文教程 - 如何减小文档文件大小?

DevExpress(WinForms  WPF)中文教程 - 如何减小文档文件大小?
📅 发布时间:2026/7/11 10:53:39

DevExpress拥有.NET开发需要的所有平台控件,包含600多个UI控件、报表平台、DevExpress Dashboard eXpressApp 框架、适用于 Visual Studio的CodeRush等一系列辅助工具。屡获大奖的软件开发平台DevExpress近期重要版本v24.1已正式发布,该版本拥有众多新产品和数十个具有高影响力的功能,可为桌面、Web和移动应用提供直观的解决方案,全面解决各种使用场景问题。

减小文档文件大小可以改善文档导入/处理相关操作,它还可以帮助最小化数据库和云服务器中的文件存储需求。在这篇文章中,我们将使用DevExpress(WinForms & WPF) Word Processing API来减少Microsoft Word文档文件大小的不同策略。

DevExpress新旧版本帮助文档下载可进QQ qun获取:169725316

重要提示:下面列出的策略涉及删除文档内容,删除的内容将无法恢复。

简化文档

虽然显而易见,文档简化是减少/优化文件大小的最佳方法,简化策略包括:

  • 在可能的情况下,使用一组有限的样式来格式化文档内容。
  • 将文档从DOCM格式转换为DOCX格式来消除宏,您也可以使用RichEditDocumentServer.Options.DocumentCapabilities.Macros 选项来禁用宏。
  • 在保存文档之前禁用跟踪更改,RichEditDocumentServer包含RichEditDocumentServer.Options.DocumentCapabilities.TrackChanges属性用于禁用跟踪。
  • 减少图像内容。
  • 使用链接OLE对象代替嵌入OLE对象,如果无法使用链接的OLE对象,可以减小嵌入OLE对象的大小或在保存之前将其删除。有关OLE对象支持的其他信息,请参阅以下文章:OLE Objects in Word Documents
  • 减少字段和内容控件的使用,在保存之前取消链接或删除字段。
  • 用压缩图像替换图表。
  • 删除额外的元数据(XML数据、文档属性、注释、RTF主题数据)。
  • 将长表划分为多个短表,在大多数情况下,长表不会影响文件大小,但会减慢文档呈现和布局计算。
使用OpenXML格式替代传统格式

OpenXML格式(DOCX)是现代的、开放的、跨多个平台兼容的,虽然在某些情况下更有效,但遗留格式(如DOC、RTF)是专有的,灵活性较差。OpenXML文件本质上是带有XML文件和附加资源(如图像和样式)的ZIP存档,因此DOCX文件更容易存储在数据库中,您可以使用RichEditDocumentServer.Save 方法将文档转换为所需的文件格式。

不要嵌入字体

DevExpress Word Processing Document API允许您在文档中嵌入字体,虽然具有嵌入式字体的文档在不同的计算设备上保持外观特征,但这些文档的大小要大得多。如果您的解决方案在受控/托管环境中显示文档,我们建议使用DevExpress DXFontRepository类。有关更多信息,请参阅以下帮助主题:Load and Use Custom Fonts Without Installation on the System

减小图像大小

您可以使用第三方应用程序来压缩文档图像,一旦压缩,只需要调用PictureFormat.SetPicture方法将原始图像替换为其压缩后的等效图像。

下面的代码片段将原始图像替换为压缩后的等效图像:

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer()) { wordProcessor.LoadDocument("doc_with_images.docx"); Document document = wordProcessor.Document; Shape shape = document.Shapes[0]; DXImage sourceImage = shape.PictureFormat.Picture.DXImage; MemoryStream imageStream = new MemoryStream(); sourceImage.Save(stream); //Compress the image saved in the stream //... DXImage compressedImage = DXImage.FromStream(updatedImageStream); shape.PictureFormat.SetPicture(compressedImage); }

另一个技巧是不要裁剪图像,使用保存的预裁剪版本。您可以使用PictureFormat.SourceRect属性在代码中裁剪图像,然后保存输出,PictureFormatSetPicture方法允许您将图像替换为裁剪后的版本。

下面的代码片段裁剪图像,保存它,然后用裁剪后的等效图像替换原始图像:

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer()) { wordProcessor.LoadDocument("CroppedImages.docx"); Document document = wordProcessor.Document; Shape shape = document.Shapes[0]; if (shape.PictureFormat != null) { DXBitmap image = shape.PictureFormat.Picture.DXImage as DXBitmap; var rectOffset = shape.PictureFormat.SourceRect; RectangleF imageRect = new RectangleF(image.Width * rectOffset.LeftOffset, image.Height * rectOffset.TopOffset, image.Width - image.Width * rectOffset.LeftOffset - image.Width * rectOffset.RightOffset, image.Height - image.Height * rectOffset.TopOffset - image.Height * rectOffset.BottomOffset); MemoryStream imageStream = new MemoryStream(); image.Crop(imageRect).Save(imageStream, image.ImageFormat); DocumentImageSource source = DocumentImageSource.FromStream(imageStream); shape.PictureFormat.SetPicture(source); shape.PictureFormat.SourceRect = new RectangleOffset(); } }

如果需要使用大图像,并且应用程序架构允许您单独存储图像,则可以采用以下解决方案。迭代文档的形状集合,并将所有图像保存到具有唯一标识符的数据库中。完成后,用空图像或DOCVARIABLE字段(用于动态图像替换)替换原始文档图像,或者删除图像并用书签标记其在文档中的位置。通过使用此策略,您将能够保存文档的轻量级版本,并在必要时恢复原始文档图像:

Document document = wordProcessor.Document; // iterate through document images, save them to the database // and replace original images with an empty image int imageID = 1; // generate an image ID as you require DocumentImageSource emptyImageSource = DocumentImageSource.FromImage(new DXBitmap(1, 1)); for (int i = document.Shapes.Count - 1; i >= 0; i--) { Shape shape = document.Shapes[i]; if (shape.PictureFormat != null) { DXBitmap image = shape.PictureFormat.Picture.DXImage as DXBitmap; using (MemoryStream imageStream = new MemoryStream()) { image.Save(imageStream, image.ImageFormat); byte[] imageBytes = imageStream.ToArray(); // save image bytes to the database with the specified image ID // ... // change the image name (if required) to identify it later shape.Name = "Image " + imageID.ToString(); // replace the current image with the empty image shape.PictureFormat.SetPicture(emptyImageSource); } imageID++; } } // save the document with dummy images using (MemoryStream documentStream = new MemoryStream()) document.SaveDocument(documentStream, DocumentFormat.OpenXml); //... // restore document images richEditControl.LoadDocument(documentStream, DocumentFormat.OpenXml); Document document = richEditControl.Document; for (int i = document.Shapes.Count - 1; i >= 0; i--) { Shape shape = document.Shapes[i]; if (shape.PictureFormat != null) { string imageName = shape.Name; // extract the required image from the database by name byte[] imageBytes = ...; using(MemoryStream imageStream = new MemoryStream(imageBytes)) { // replace the empty image with the original image DocumentImageSource imageSource = DocumentImageSource.FromStream(imageStream); shape.PictureFormat.SetPicture(imageSource); } } }

相关新闻

  • 售前PPT制作全攻略:一套让你少走弯路的实战打法
  • DarkHole-2 靶机渗透:从 Git 泄露到 Python Sudo 提权的 5 步完整复现
  • 多层级网络架构中,智能合约的行为模式识别与技术取证方法。

最新新闻

  • Kiro实战指南:用Opus 4.6实现低成本高精度代码理解与自动化
  • Vue 3 + Three.js r164 项目实战:5步封装可复用3D场景组件(含响应式)
  • Typora插件:解锁PDF批量导出与高级自定义功能终极指南
  • 2026年7月泉州戴尔售后本地化服务全景|交通指引气候养护到店全攻略.txt - 品牌引荐
  • 5分钟快速上手Czkawka:终极免费重复文件清理工具完全指南
  • 秋叶ComfyUI整合包:8GB显存运行Wan2.2视频生成模型实战指南

日新闻

  • OpenClaw本地部署:一键直连微信的私有化AI Agent实战指南
  • Kubernetes 系列【10】控制器:ReplicaSet(副本集)
  • 怎么寄快递才能便宜呢?2026年7月寄快递省钱攻略 - 生活情报姬

周新闻

  • 基于YOLOv12的番茄成熟度智能检测系统开发
  • 终极RimWorld模组管理指南:用RimSort告别模组冲突烦恼
  • AI Agent框架开发:从理论到实践的完整指南

月新闻

  • 2026年6月公司网站搭建最新热门渠道测评:四大低成本/零代码平台对比+避坑
  • 【Linux】Linux arm 编译QT程序,出现expected “}“报错
  • 【MATLAB例程】四基站二维AOA定位与距离辅助增强对比仿真。基于角度观测和测距修正的固定目标平面定位精度分析

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号