翻譯|行業(yè)資訊|編輯:龔雪|2024-07-09 10:37:08.557|閱讀 88 次
概述:本文主要介紹使用DevExpress(WinForms & WPF)組件時(shí)如何減小文檔文件大小,歡迎下載最新版組件體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
DevExpress擁有.NET開發(fā)需要的所有平臺(tái)控件,包含600多個(gè)UI控件、報(bào)表平臺(tái)、DevExpress Dashboard eXpressApp 框架、適用于 Visual Studio的CodeRush等一系列輔助工具。屢獲大獎(jiǎng)的軟件開發(fā)平臺(tái)DevExpress近期重要版本v24.1已正式發(fā)布,該版本擁有眾多新產(chǎn)品和數(shù)十個(gè)具有高影響力的功能,可為桌面、Web和移動(dòng)應(yīng)用提供直觀的解決方案,全面解決各種使用場(chǎng)景問題。
減小文檔文件大小可以改善文檔導(dǎo)入/處理相關(guān)操作,它還可以幫助最小化數(shù)據(jù)庫(kù)和云服務(wù)器中的文件存儲(chǔ)需求。在這篇文章中,我們將使用DevExpress(WinForms & WPF) Word Processing API來(lái)減少M(fèi)icrosoft Word文檔文件大小的不同策略。
重要提示:下面列出的策略涉及刪除文檔內(nèi)容,刪除的內(nèi)容將無(wú)法恢復(fù)。
DevExpress技術(shù)交流群10:532598169 歡迎一起進(jìn)群討論
雖然顯而易見,文檔簡(jiǎn)化是減少/優(yōu)化文件大小的最佳方法,簡(jiǎn)化策略包括:
OpenXML格式(DOCX)是現(xiàn)代的、開放的、跨多個(gè)平臺(tái)兼容的,雖然在某些情況下更有效,但遺留格式(如DOC、RTF)是專有的,靈活性較差。OpenXML文件本質(zhì)上是帶有XML文件和附加資源(如圖像和樣式)的ZIP存檔,因此DOCX文件更容易存儲(chǔ)在數(shù)據(jù)庫(kù)中,您可以使用 方法將文檔轉(zhuǎn)換為所需的文件格式。
DevExpress Word Processing Document API允許您在文檔中嵌入字體,雖然具有嵌入式字體的文檔在不同的計(jì)算設(shè)備上保持外觀特征,但這些文檔的大小要大得多。如果您的解決方案在受控/托管環(huán)境中顯示文檔,我們建議使用DevExpress DXFontRepository類。有關(guān)更多信息,請(qǐng)參閱以下幫助主題:
您可以使用第三方應(yīng)用程序來(lái)壓縮文檔圖像,一旦壓縮,只需要調(diào)用方法將原始圖像替換為其壓縮后的等效圖像。
下面的代碼片段將原始圖像替換為壓縮后的等效圖像:
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); }
另一個(gè)技巧是不要裁剪圖像,使用保存的預(yù)裁剪版本。您可以使用屬性在代碼中裁剪圖像,然后保存輸出,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(); } }
如果需要使用大圖像,并且應(yīng)用程序架構(gòu)允許您單獨(dú)存儲(chǔ)圖像,則可以采用以下解決方案。迭代文檔的形狀集合,并將所有圖像保存到具有唯一標(biāo)識(shí)符的數(shù)據(jù)庫(kù)中。完成后,用空?qǐng)D像或DOCVARIABLE字段(用于動(dòng)態(tài)圖像替換)替換原始文檔圖像,或者刪除圖像并用書簽標(biāo)記其在文檔中的位置。通過(guò)使用此策略,您將能夠保存文檔的輕量級(jí)版本,并在必要時(shí)恢復(fù)原始文檔圖像:
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); } } }
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)