Hi,
I want to compress pdf file size before upload,can anyone help to acheive it.
Hi,
I want to compress pdf file size before upload,can anyone help to acheive it.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Selvi Jp35 reputationPosted Sep 2, 2022, 12:51 AM EDT
Vishal YelveRank #178 · 13.6K reputationPosted Sep 2, 2022, 8:29 AM EDT
Selvi Jp35 reputationPosted Sep 5, 2022, 4:01 AM EDT
Uday DodiyaRank #239 · 10.3K reputationPosted Sep 2, 2022, 12:42 AM EDT
Marvin ReidRank #1,963 · 746 reputationPosted May 16, 2023, 5:03 PM EDT
One solution is to compress the file via use of a backend service which would allow you to levearge the PDFOptimizierOptions class. This would provide extensive control for controlling the quality and size of the file, and it would return a compressed PDF tailored to your needs. Here is some sample code that demonstrates some options:
using (RasterCodecs _codecs = new RasterCodecs()) { // Set the path to the PDF utilities dll _codecs.Options.Pdf.InitialPath = @"C:\LEADTOOLS22\Bin\Dotnet4\x64"; string[] _pdfFiles = Directory.GetFiles(_pdfFileDir, "*.pdf"); string _destDir = Path.Combine(_pdfFileDir, "OptimizedPDFs"); // Create the optimizer options PDFOptimizerOptions myOptimizerOptions = new PDFOptimizerOptions(); myOptimizerOptions.AutoOptimizerMode = PDFAutoOptimizerMode.BestSize; myOptimizerOptions.ColorImageDownsamplingMode = PDFDownsamplingMode.Average; myOptimizerOptions.GrayImageDownsamplingMode = PDFDownsamplingMode.Bicubic; myOptimizerOptions.MonoImageDownsamplingMode = PDFDownsamplingMode.Bicubic; myOptimizerOptions.ColorImageDownsampleFactor = 2.0; myOptimizerOptions.GrayImageDownsampleFactor = 2.0; myOptimizerOptions.MonoImageDownsampleFactor = 2.0; myOptimizerOptions.ColorImageDPI = 150; myOptimizerOptions.GrayImageDPI = 150; myOptimizerOptions.MonoImageDPI = 150; myOptimizerOptions.ColorImageCompression = RasterImageFormat.Jpeg; myOptimizerOptions.GrayImageCompression = RasterImageFormat.RasPdfLzw; myOptimizerOptions.MonoImageCompression = RasterImageFormat.Jbig; myOptimizerOptions.EmbedAllFonts = false; myOptimizerOptions.SubsetFonts = true; foreach (string _pdfFile in _pdfFiles) { string _pdfFileName = Path.GetFileNameWithoutExtension(_pdfFile); string _destFile = Path.Combine(_destDir, $"{_pdfFileName}_OPTIMIZED.pdf"); PDFFile _pdf = new PDFFile(_pdfFile); _pdf.OptimizerOptions = myOptimizerOptions; _pdf.Optimize(_destFile); }https://www.leadtools.com/help/sdk/tutorials/dotnet-console-optimize-pdf-files-for-storage.html
https://www.leadtools.com/help/sdk/dh/pdf/pdfoptimizeroptions.html
Leon DRank #851 · 2.6K reputationPosted May 17, 2023, 9:52 PM EDT
Spire.PDF library is capable of compressing PDF files.
Refer to the below article:
https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Document-Operation/How-to-Compress-PDF-Document-in-C-VB.NET.html
Deepak RawatRank #646 · 3.7K reputationPosted May 18, 2023, 12:58 AM EDT
You can utilize third-party libraries that provide PDF compression functionality. One such library is
pdf-lib. Here's an example of how you can compress a PDF file before uploading it in React usingpdf-lib