AABBbaby

导航

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 v24.1正式版下载

DevExpress技术交流群10:532598169      欢迎一起进群讨论

简化文档

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

  • 在可能的情况下,使用一组有限的样式来格式化文档内容。
  • 将文档从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);
}
}
}

posted on 2024-07-10 10:01  AABBbaby  阅读(2)  评论(0编辑  收藏  举报