中文教程 - 如何减小文档文件大小?)
DevExpress拥有.NET开发需要的所有平台控件包含600多个UI控件、报表平台、DevExpress Dashboard eXpressApp 框架、适用于 Visual Studio的CodeRush等一系列辅助工具。屡获大奖的软件开发平台DevExpress近期重要版本v24.1已正式发布该版本拥有众多新产品和数十个具有高影响力的功能可为桌面、Web和移动应用提供直观的解决方案全面解决各种使用场景问题。减小文档文件大小可以改善文档导入/处理相关操作它还可以帮助最小化数据库和云服务器中的文件存储需求。在这篇文章中我们将使用DevExpressWinForms 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); } } }