Kode kanggo ngompres Word ing C#

Pustaka .NET kinerja dhuwur kanggo ngoptimalake isi lan ukuran Word njaga kualitas asli lan data dasar

Iki minangka solusi profesional kanggo ngompres Word nggunakake C#, F#, VB.NET. Gunakake API optimasi Word kanggo ngembangake piranti lunak tingkat dhuwur, platform-independen kanggo platform .NET lan .NET Core. Coba online gratis!

Ndeleng potongan kode

Kompres Word file ing C#

Nggawe Word file gedhe luwih cilik tanpa mundhut kualitas. Mbusak data sing ora perlu lan ora digunakake. Nyilikake ukuran Word file ing kode. Pustaka C# nyedhiyakake pangembang API terpadu kanggo ngoptimalake isi Word.

Ngurangi ukuran Word file nggunakake C#

Perpustakaan C# minangka solusi mandiri lan ora mbutuhake piranti lunak pihak katelu sing diinstal.

Kompresi lossless lan optimasi isi jero digawe gampang karo 'Aspose.Words for .NET'. Conto ing ngisor iki nuduhake carane ngoptimalake isi Word file ing C#:

Conto kode ing C# kanggo ngompres DOCX file
Unggah file sing pengin dioptimalake / kompres
Kode roto
Pilih format target saka dhaptar
dotnet add package Aspose.Words
Nyalin
using Aspose.Words;

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

var nodes = doc.GetChildNodes(NodeType.Shape, true);
foreach (Shape shape in nodes)
{
    if (shape.IsImage)
    {
        //  Iku nganti pangembang kanggo milih perpustakaan kanggo komprèsi gambar.
        using var image = Image.FromStream(shape.ImageData.ToStream());

        // ...
        //  Compress gambar lan nyetel maneh menyang wangun.
        shape.ImageData.SetImage("yourCompressedImage");
    }
}

var saveOptions = new OoxmlSaveOptions
{
  CompressionLevel = CompressionLevel.Maximum
};

doc.Save("Output.docx", saveOptions);
using Aspose.Words; var doc = new Document("Input.docx"); doc.Cleanup(); var nodes = doc.GetChildNodes(NodeType.Shape, true); foreach (Shape shape in nodes) { if (shape.IsImage) { // Iku nganti pangembang kanggo milih perpustakaan kanggo komprèsi gambar. using var image = Image.FromStream(shape.ImageData.ToStream()); // ... // Compress gambar lan nyetel maneh menyang wangun. shape.ImageData.SetImage("yourCompressedImage"); } } var saveOptions = new PdfSaveOptions { CacheBackgroundGraphics = true }; doc.Save("Output.docx", saveOptions); using Aspose.Words; var doc = new Document("Input.docx"); doc.Cleanup(); var nodes = doc.GetChildNodes(NodeType.Shape, true); foreach (Shape shape in nodes) { if (shape.IsImage) { // Iku nganti pangembang kanggo milih perpustakaan kanggo komprèsi gambar. using var image = Image.Fromstream(shape.ImageData.ToStream()); // ... // Compress gambar lan nyetel maneh menyang wangun. shape.ImageData.SetImage("yourCompressedImage"); } } doc.Save("Output.docx"); using Aspose.Words; var doc = new Document("Input.docx"); doc.Cleanup(); var nodes = doc.GetChildNodes(NodeType.Shape, true); foreach (Shape shape in nodes) { if (shape.IsImage) { // Iku nganti pangembang kanggo milih perpustakaan kanggo komprèsi gambar. using var image = Image.FromStream(shape.ImageData.ToStream()); // ... // Compress gambar lan nyetel maneh menyang wangun. shape.ImageData.SetImage("yourCompressedImage"); } } var saveOptions = new OoxmlSaveOptions { CompressionLevel = CompressionLevel.Maximum }; doc.Save("Output.docx", saveOptions); using Aspose.Words; var doc = new Document(); var builder = new DocumentBuilder(doc); var shape = builder.InsertImage("Input.docx"); var saveOptions = new ImageSaveOptions(SaveFormat.Docx) { }; shape.GetShapeRenderer().Save("Output.docx", saveOptions); using Aspose.Words; using Aspose.Words.Pdf2Word.FixedFormats; using var pdfStream = File.OpenRead("Input.docx"); 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) { // Setel ukuran kaca maksimal kanggo ngindhari skala gambar kaca saiki. 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.docx", saveOptions); private void SetPageSize(PageSetup pageSetup, double width, double height) { pageSetup.PageWidth = width; pageSetup.PageHeight = height; }
Kode roto

Carane ngompres Word

  1. Pasang Aspose.Words for .NET.
  2. Tambah referensi perpustakaan (ngimpor perpustakaan) menyang proyek C# sampeyan.
  3. Bukak Word berkas sumber ing C#.
  4. Kompres Word tanpa kelangan kualitas.
  5. Telpon metode Document.Cleanup(). Kode asil bisa beda-beda gumantung saka format file output.
  6. Entuk asil sing dikompres minangka Word file.

Pustaka C# kanggo ngoptimalake lan ngompres WORD file

Ana telung pilihan alternatif kanggo nginstal "Aspose.Words kanggo .NET" sistem sampeyan. Pilih salah siji sing cocog karo kabutuhan sampeyan lan tindakake pandhuan langkah demi langkah:

Syarat Sistem

Produk kita kanthi lintas-platform lan ndhukung kabeh implementasi .NET utama:

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

Nganti kode .NET ora gumantung ing hardware utawa sistem operasi sing ndasari, nanging mung ing Mesin Virtual, sampeyan bebas ngembangake piranti lunak apa wae kanggo Windows, macOS, Android, iOS lan Linux. Priksa manawa sampeyan wis nginstal versi .NET Framework, .NET Core, Windows Azure, Mono utawa Xamarin sing cocog.

Disaranake nggunakake Microsoft Visual Studio, Xamarin, lan lingkungan pangembangan terintegrasi MonoDevelop kanggo nggawe aplikasi C#, F#, VB.NET.

Kanggo rincian liyane, waca Dokumentasi Produk.

Optimasi format file liyane sing didhukung

Sampeyan bisa ngoptimalake file ing akeh format file liyane:

5%

Langganan kanggo Aspose Update Product

Entuk buletin saben wulan lan tawaran langsung dikirim menyang kothak layang.

© Aspose Pty Ltd 2001-2024. Kabeh hak dilindhungi undhang-undhang.