没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|行业资讯|编辑:龚雪|2024-07-09 10:37:08.557|阅读 35 次
概述:本文主要介绍使用DevExpress(WinForms & WPF)组件时如何减小文档文件大小,欢迎下载最新版组件体验!
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
DevExpress拥有.NET开发需要的所有平台控件,包含600多个UI控件、报表平台、DevExpress Dashboard eXpressApp 框架、适用于 Visual Studio的CodeRush等一系列辅助工具。屡获大奖的软件开发平台DevExpress近期重要版本v24.1已正式发布,该版本拥有众多新产品和数十个具有高影响力的功能,可为桌面、Web和移动应用提供直观的解决方案,全面解决各种使用场景问题。
减小文档文件大小可以改善文档导入/处理相关操作,它还可以帮助最小化数据库和云服务器中的文件存储需求。在这篇文章中,我们将使用DevExpress(WinForms & WPF) Word Processing API来减少Microsoft Word文档文件大小的不同策略。
重要提示:下面列出的策略涉及删除文档内容,删除的内容将无法恢复。
DevExpress技术交流群10:532598169 欢迎一起进群讨论
虽然显而易见,文档简化是减少/优化文件大小的最佳方法,简化策略包括:
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); } } }
更多DevExpress线上公开课、中文教程资讯请上中文网获取
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
文章转载自:慧都网本文将为大家介绍QtitanRibbon在制造业场景中的落地应用探索,欢迎下载最新版体验!
Parasoft SOAtest作为一款企业级API测试工具,通过自动扫描API接口、智能分析变更影响、优化测试,执行以及支持测试用例共享与版本控制等功能,有效解决了这些痛点,显著提升了测试效率和资产可维护性,为测试团队提供了强大的支持。
Parasoft Jtest作为一款人工智能驱动的Java 开发人员生产力解决方案,凭借智能测试影响分析技术,精准识别受影响测试用例,关联测试数据确保全面性和准确性,还能即时反馈问题,有效解决上述痛点,助力企业高效推进软件项目。
嵌入式软件测试工具Parasoft C/C++test 聚焦高可靠性系统开发,提供核心能力三重保障:在编码阶段左移拦截缺陷降低修复成本;自动检查MISRA/CERT/AUTOSAR等安全标准确保合规性;AI智能测试动态优化用例覆盖深度。三者协同构建嵌入式软件质量防护体系,适用于车载、航天等安全关键领域。
优秀的界面控件开发包,帮助企业构建卓越应用!
DevExpress DXperience Subscription高性价比的企业级.NET用户界面套包,助力企业创建卓越应用!
DevExpress WinForms Subscription为Windows Forms平台创建具有影响力的业务解决方案,高性价比WinForms界面控件套包。
DevExpress WPF Subscription高效MVVM开发模式,WPF界面解决方案首选工具,帮助企业实现酷炫动效界面。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号