当前位置: 首页 > news >正文

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

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); } } }
http://www.jsqmd.com/news/1166883/

相关文章:

  • RVC 与 GPT-SoVITS 对比评测:5秒 vs 30分钟素材,音色克隆效果实测
  • 如何设置价格查看权限?吗?一文搞懂(附详细解答)
  • 多层级网络架构中,智能合约的行为模式识别与技术取证方法。
  • 售前PPT制作全攻略:一套让你少走弯路的实战打法
  • Windows Precision Touchpad驱动:让苹果触控板在Windows系统上完美工作
  • DarkHole-2 靶机渗透:从 Git 泄露到 Python Sudo 提权的 5 步完整复现
  • ArcGIS Pro 3.2 三维模型与矢量叠加:5步解决高程错位与视觉贴合问题
  • OBS多平台直播插件终极指南:5分钟学会obs-multi-rtmp一键推流
  • 为什么选择gift-book?5大核心优势让红白喜事礼金管理更简单
  • AirDropPlus 终极指南:5个维度解析Windows与iOS跨平台文件传输的技术突破
  • PIC18F2455与CMT-8540S-SMT在嵌入式音频中的应用
  • Rustic与rust-mode深度对比:为什么它是Emacs Rust开发的最佳选择?
  • TMC7300与PIC18LF25K80驱动有刷直流电机方案解析
  • vllm0.9.2的cpu版本用docker部署qwen3:0.6B
  • 为什么你的V7生成图仍显“塑料感”?——V7纹理增强模块的3层权重调控逻辑(附官方未公开CLI参数)
  • VS2015/2017/vsCode C++ 报错解决方案 笔记
  • (4-2)A*
  • ptpd2提示failed to join the multicast group (strerror: No buffer space available)
  • 排序算法-冒泡排序
  • 9大网盘直链解析工具LinkSwift:彻底告别下载限速的终极解决方案
  • MJ V6出图崩坏?紧急修复清单:3分钟定位--tile错配、--stylize漂移、--v 6.2内核兼容性漏洞
  • GitHub加速终极指南:如何让你的下载速度提升10倍以上?
  • 如何将微信对话转化为永久数字资产:WeChatMsg数据管理完全指南
  • 2026淮海经济区多场景腻子粉建材采购供应商指南 - 起跑123
  • 5分钟解锁Kodi播放115网盘高清视频的完美方案
  • Animal-AI环境实战:基于动物认知的AI智能体测试与强化学习训练
  • H3C 云化园区网络 V100R023C10 部署:基于意图的 3 步配置与自动化运维实战
  • 【Midjourney批量生成黄金法则】:20年AI图像工程师亲授17种高效批量提示词架构与错误规避清单
  • 【RPA已死?AI Agent崛起真相】:Gartner认证架构师独家披露——传统RPA在2024 Q3后将面临3类不可逆失效场景
  • 抖音下载终极神器:douyin-downloader一键批量保存去水印工具全解析