翻譯|使用教程|編輯:吉煒煒|2025-08-11 10:58:33.240|閱讀 91 次
概述:如果您的PDF太大,無法通過電子郵件發(fā)送,或者在線加載時間過長,您可以在幾秒鐘內(nèi)縮小 PDF 大小。本教程介紹了借助Aspose.PDF使用 C#、Java 和 Python 編程快速縮小PDF的方法。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
如果您的PDF太大,無法通過電子郵件發(fā)送,或者在線加載時間過長,您可以在幾秒鐘內(nèi)縮小 PDF 大小。本教程介紹了借助Aspose.PDF使用 C#、Java 和 Python 編程快速縮小PDF的方法。
加入Aspose技術(shù)交流QQ群(1041253375),與更多小伙伴一起探討提升開發(fā)技能。
如果您需要可重復(fù)的自動壓縮功能(例如,用于網(wǎng)站、應(yīng)用程序或工作流程),您可以使用 Aspose.PDF 以代碼形式實現(xiàn)。 Aspose.PDF 它是一個功能強大的跨平臺庫,可用于處理 .NET、Java 和 Python 中的 PDF 文件。它通過壓縮圖像、刪除未使用的對象、清理元數(shù)據(jù)和優(yōu)化內(nèi)部資源,提供對文件大小的完全控制。無論您需要快速縮減文件大小還是高度自定義的優(yōu)化流程,Aspose.PDF 都能讓您輕松高效地完成。只需幾行代碼,開發(fā)人員即可通過編程方式壓縮 PDF 文件,而無需依賴第三方工具或 Adobe Acrobat。
縮小 PDF 尺寸的目的是在不影響美觀的情況下減小其大小。Aspose.PDF 通過以下方式實現(xiàn)此目的:
您可以使用 Aspose.PDF for .NET 在 C# 中縮小 PDF 文檔的大小,如下所示:
步驟1:安裝庫
安裝:
PM> Install-Package Aspose.PDF
步驟2:使用代碼縮小PDF文件
下面是一個減少 PDF 文件大小的 C# 示例:
// Load the PDF document from the specified file path var document = new Document("input.pdf"); // Create an OptimizationOptions object to define compression settings var options = new OptimizationOptions { // Remove unused objects from the PDF to free up space RemoveUnusedObjects = true, // Remove unused streams (extra data not required for display) RemoveUnusedStreams = true, // Link duplicate streams so identical resources (e.g., fonts, images) are stored only once LinkDuplicateStreams = true, }; // Enable image compression options.ImageCompressionOptions.CompressImages = true; // Set image quality to 75% (balances file size and visual clarity) options.ImageCompressionOptions.ImageQuality = 75; // Apply the optimization settings to the PDF document document.OptimizeResources(options); // Save the shrinked PDF to the specified output path document.Save("shrinked.pdf");
作為 Java 開發(fā)人員,您可以使用 Aspose.PDF for Java 縮小 PDF 尺寸,如下所示:
步驟 1:添加依賴項
將以下內(nèi)容添加到您的pom.xml:
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-pdf</artifactId> <version>25.7</version><!-- Use the latest version --> </dependency>
步驟2:用Java縮小PDF大小
這是一個使用 Java 縮小 PDF 文檔大小的簡單 Java 示例:
import com.aspose.pdf.Document; import com.aspose.pdf.optimization.OptimizationOptions; public class ShrinkPDF { public static void main(String[] args) { // Load the PDF document from the specified file path Document document = new Document("input.pdf"); // Create an OptimizationOptions object to define compression settings OptimizationOptions options = new OptimizationOptions(); // Remove unused objects from the PDF to free up space options.setRemoveUnusedObjects(true); // Remove unused streams (extra data not required for display) options.setRemoveUnusedStreams(true); // Link duplicate streams so identical resources (e.g., fonts, images) are stored only once options.setLinkDuplicateStreams(true); // Enable image compression options.getImageCompressionOptions().setCompressImages(true); // Set image quality to 75% (balances file size and visual clarity) options.getImageCompressionOptions().setImageQuality(75); // Apply the optimization settings to the PDF document document.optimizeResources(options); // Save the shrunk (compressed) PDF to the specified output path document.save("shrinked.pdf"); } }
使用 Aspose.PDF for Python,您可以輕松縮小 PDF 尺寸,如下所示:
步驟1:安裝庫
使用 pip 安裝:
pip install aspose-pdf
步驟2:運行腳本以縮小PDF
運行腳本快速縮小您的 PDF 并生成更小、更優(yōu)化的文件以供共享或存儲。
import aspose.pdf as ap # Load the PDF document from the specified file path document = ap.Document("input.pdf") # Create an OptimizationOptions object to define compression settings options = ap.optimization.OptimizationOptions() # Remove unused objects from the PDF to free up space options.remove_unused_objects = True # Remove unused streams (extra data not required for display) options.remove_unused_streams = True # Link duplicate streams so identical resources (e.g., fonts, images) are stored only once options.link_duplicate_streams = True # Enable image compression options.image_compression_options.compress_images = True # Set image quality to 75% (balances file size and visual clarity) options.image_compression_options.image_quality = 75 # Apply the optimization settings to the PDF document document.optimize_resources(options) # Save the shrunk (compressed) PDF to the specified output path document.save("shrinked.pdf")
問:縮小 PDF 與壓縮 PDF 是一樣的嗎?
答:是的,這兩個術(shù)語都表示減小文件大小,但“縮小”更隨意,“壓縮”更具技術(shù)性。
問:縮小 PDF 會影響質(zhì)量嗎?
答:如果畫質(zhì)降低太多,確實會這樣。為了保持平衡,建議將畫質(zhì)控制在 70% 到 80% 之間。
壓縮 PDF 的速度可以快至 10 秒,甚至可以使用代碼實現(xiàn)全自動解決方案。使用 Aspose.PDF,您可以控制文件大小和質(zhì)量之間的平衡,讓您的 PDF 始終保持快速共享、輕松存儲和愉悅瀏覽。
————————————————————————————————————————
關(guān)于慧都科技:
慧都科技是一家行業(yè)數(shù)字化解決方案公司,長期專注于軟件、油氣與制造行業(yè)。公司基于深入的業(yè)務(wù)理解與管理洞察,以系統(tǒng)化的業(yè)務(wù)建模驅(qū)動技術(shù)落地,幫助企業(yè)實現(xiàn)智能化運營與長期競爭優(yōu)勢。在軟件工程領(lǐng)域,我們提供開發(fā)控件、研發(fā)管理、代碼開發(fā)、部署運維等軟件開發(fā)全鏈路所需的產(chǎn)品,提供正版授權(quán)采購、技術(shù)選型、個性化維保等服務(wù),幫助客戶實現(xiàn)技術(shù)合規(guī)、降本增效與風(fēng)險可控。慧都科技Aspose在中國的官方授權(quán)代理商,提供Aspose系列產(chǎn)品免費試用,咨詢,正版銷售等于一體的專業(yè)化服務(wù)。Aspose是文檔處理領(lǐng)域的優(yōu)秀產(chǎn)品,幫助企業(yè)高效構(gòu)建文檔處理的應(yīng)用程序。
下載|體驗更多Aspose產(chǎn)品,請咨詢,或撥打產(chǎn)品熱線:023-68661681
加入Aspose技術(shù)交流QQ群(1041253375),與更多小伙伴一起探討提升開發(fā)技能。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)