The code to compress DOC in C#

High-performance .NET library to optimize DOC content and size retaining the original quality and baseline data

This is a professional solution to compress DOC using C#, F#, VB.NET. Use our DOC optimization API to develop high-level, platform-independent software for .NET and .NET Core platforms. Try it online for free!

View code snippet

Compress DOC file in C#

Make large DOC files smaller without loss of quality. Delete unnecessary and unused data. Shrink the size of a DOC file in code. The C# library provides developers with an integrated API to optimize DOC content.

Reduce the size of DOC files using C#

C# library is a stand-alone solution and doesn't require any third party software installed.

Lossless compression and deep content optimization made easy with 'Aspose.Words for .NET'. The following example demonstrates how to optimize the content of a DOC file in C#:

Code example in C# to compress DOC files
Upload a file you want to optimize/compress
Run code
Select the target format from the list
using Aspose.Words;

var doc = new Document("Input.doc");
doc.Cleanup();

var nodes = doc.GetChildNodes(NodeType.Shape, true);
foreach (Shape shape in nodes)
{
    if (shape.IsImage)
    {
        // It's up to developer to choose the library for image compression.
        using var image = Image.Fromstream(shape.ImageData.ToStream());

        // ...
        // Compress image and set it back to the shape.
        shape.ImageData.SetImage("yourCompressedImage");
    }
}

doc.Save("Output.doc");
using Aspose.Words; var doc = new Document("Input.doc"); doc.Cleanup(); var nodes = doc.GetChildNodes(NodeType.Shape, true); foreach (Shape shape in nodes) { if (shape.IsImage) { // It's up to developer to choose the library for image compression. using var image = Image.FromStream(shape.ImageData.ToStream()); // ... // Compress image and set it back to the shape. shape.ImageData.SetImage("yourCompressedImage"); } } var saveOptions = new PdfSaveOptions { CacheBackgroundGraphics = true }; doc.Save("Output.doc", saveOptions); using Aspose.Words; var doc = new Document("Input.doc"); doc.Cleanup(); var nodes = doc.GetChildNodes(NodeType.Shape, true); foreach (Shape shape in nodes) { if (shape.IsImage) { // It's up to developer to choose the library for image compression. using var image = Image.Fromstream(shape.ImageData.ToStream()); // ... // Compress image and set it back to the shape. shape.ImageData.SetImage("yourCompressedImage"); } } doc.Save("Output.doc"); using Aspose.Words; var doc = new Document("Input.doc"); doc.Cleanup(); var nodes = doc.GetChildNodes(NodeType.Shape, true); foreach (Shape shape in nodes) { if (shape.IsImage) { // It's up to developer to choose the library for image compression. using var image = Image.FromStream(shape.ImageData.ToStream()); // ... // Compress image and set it back to the shape. shape.ImageData.SetImage("yourCompressedImage"); } } var saveOptions = new OoxmlSaveOptions { CompressionLevel = CompressionLevel.Maximum }; doc.Save("Output.doc", saveOptions); using Aspose.Words; var doc = new Document(); var builder = new DocumentBuilder(doc); var shape = builder.InsertImage("Input.doc"); // Update page settings to crop the document to fit the size of the picture. var pageSetup = builder.PageSetup; pageSetup.PageWidth = shape.Width; pageSetup.PageHeight = shape.Height; pageSetup.TopMargin = 0; pageSetup.LeftMargin = 0; pageSetup.BottomMargin = 0; pageSetup.RightMargin = 0; var saveOptions = new ImageSaveOptions(SaveFormat.Doc) { }; doc.Save("Output.doc", saveOptions); using Aspose.Words; using Aspose.Words.Pdf2Word.FixedFormats; using var pdfStream = File.OpenRead("Input.doc"); var renderer = new PdfFixedRenderer(); var pdfReadOptions = new PdfFixedOptions { ImageFormat = FixedImageFormat.Jpeg, JpegQuality = 50 }; const double maxPageDimension = 1584; var pagesStream = renderer.SavePdfAsImages(pdfStream, pdfReadOptions); var builder = new DocumentBuilder(); for (var i = 0; i < pagesStream.Count; ++i) { // Set maximum page size to avoid the current page image scaling. var pageSetup = builder.PageSetup; SetPageSize(pageSetup, maxPageDimension, maxPageDimension); var pageImage = builder.InsertImage(pagesStream[i]); SetPageSize(pageSetup, pageImage.Width, pageImage.Height); pageSetup.TopMargin = 0; pageSetup.LeftMargin = 0; pageSetup.BottomMargin = 0; pageSetup.RightMargin = 0; if (i != pagesStream.Count - 1) builder.InsertBreak(BreakType.SectionBreakNewPage); } var saveOptions = new PdfSaveOptions { CacheBackgroundGraphics = true }; builder.Document.Save("Output.doc", saveOptions); private void SetPageSize(PageSetup pageSetup, double width, double height) { pageSetup.PageWidth = width; pageSetup.PageHeight = height; }
Run code

How to compress DOC C#

  1. Install Aspose.Words for .NET.
  2. Add a library reference (import the library) to your C# project.
  3. Open the source DOC file in C#.
  4. Compress DOC without losing quality.
  5. Call the Document.Cleanup() method. The resulting code may vary depending on the output file format.
  6. Get the compressed result as a DOC file.

C# library to optimize and compress DOC files

There are three alternative options to install "Aspose.Words for .NET" onto your system. Please choose one that resembles your needs and follow the step-by-step instructions:

System Requirements

Our product is fully cross-platform and supports all major .NET implementations:

  • .NET ≥ 5.0
  • .NET Core ≥ 2.0
  • .NET Standard ≥ 2.0
  • .NET Framework ≥ 3.5
  • MonoMac
  • MonoAndroid
  • Xamarin

As far as .NET code doesn't depend on the underlying hardware or operating system, but only on a Virtual Machine, you are free to develop any kind of software for Windows, macOS, Android, iOS and Linux. Just make sure you have installed the corresponding version of .NET Framework, .NET Core, Windows Azure, Mono or Xamarin.

We recommend using Microsoft Visual Studio, Xamarin, and MonoDevelop integrated development environments to create C#, F#, VB.NET applications.

Fore more details please refer to Product Documentation.

Other supported file format optimizations

You can optimize files in many other file formats:

5%

Subscribe to Aspose Product Updates

Get monthly newsletters and offers directly delivered to your mailbox.

© Aspose Pty Ltd 2001-2024. All Rights Reserved.