Kode kanggo ngompres PDF ing C#

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

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

Ndeleng potongan kode

Kompres PDF ing C#

Pustaka C# iki nyedhiyakake pangembang kanthi kemampuan kanggo ngompres PDF dokumen kanthi cepet lan efisien kanthi program. Gampang entuk output PDF sing dioptimalake siap kanggo dicithak, diarsipake utawa dienggo bareng liwat jaringan. Iki minangka solusi pangolahan PDF dokumen mandiri kanggo C# sing ora mbutuhake piranti lunak pihak katelu kanggo diinstal.

Ngurangi ukuran PDF nggunakake C#

Produk kita nyedhiyakake pangembang C# kanthi API terpadu kanggo nyuda ukuran PDF file. Kompresi PDF ditindakake kanthi rong pass. Sajrone pass pisanan, kabeh kaca PDF diowahi dadi format gambar. Produk kita nggunakake algoritma kompresi gambar sing adaptif lan efisien banget sing ngidini pangembang entuk output PDF sing kompak nalika njaga kualitas dokumen sing optimal. Ing pass sabanjure, urutan gambar dikompres lan digabungake kanggo mbentuk isi PDF file anyar sing dioptimalake.

Kompresi PDF digawe gampang karo 'Aspose.Words for .NET'. Conto ing ngisor iki nuduhake carane nyuda ukuran PDF file ing C#:

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

using var pdfStream = File.OpenRead("Input.pdf");

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.pdf", saveOptions);

private void SetPageSize(PageSetup pageSetup, double width, double height)
{
    pageSetup.PageWidth = width;
    pageSetup.PageHeight = height;
}
using Aspose.Words; var doc = new Document("Input.pdf"); 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.pdf", saveOptions); using Aspose.Words; var doc = new Document("Input.pdf"); 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.pdf"); using Aspose.Words; var doc = new Document("Input.pdf"); 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.pdf", saveOptions); using Aspose.Words; var doc = new Document(); var builder = new DocumentBuilder(doc); var shape = builder.InsertImage("Input.pdf"); var saveOptions = new ImageSaveOptions(SaveFormat.Pdf) { }; shape.GetShapeRenderer().Save("Output.pdf", saveOptions); using Aspose.Words; using Aspose.Words.Pdf2Word.FixedFormats; using var pdfStream = File.OpenRead("Input.pdf"); 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.pdf", saveOptions); private void SetPageSize(PageSetup pageSetup, double width, double height) { pageSetup.PageWidth = width; pageSetup.PageHeight = height; }
Kode roto

Cara ngompres PDF Ing C#

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

Pustaka C# kanggo ngoptimalake lan ngompres PDF 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.