翻譯|使用教程|編輯:吉煒煒|2025-08-11 10:58:33.240|閱讀 91 次
概述:如果您的PDF太大,無法通過電子郵件發送,或者在線加載時間過長,您可以在幾秒鐘內縮小 PDF 大小。本教程介紹了借助Aspose.PDF使用 C#、Java 和 Python 編程快速縮小PDF的方法。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
如果您的PDF太大,無法通過電子郵件發送,或者在線加載時間過長,您可以在幾秒鐘內縮小 PDF 大小。本教程介紹了借助Aspose.PDF使用 C#、Java 和 Python 編程快速縮小PDF的方法。
加入Aspose技術交流QQ群(1041253375),與更多小伙伴一起探討提升開發技能。
如果您需要可重復的自動壓縮功能(例如,用于網站、應用程序或工作流程),您可以使用 Aspose.PDF 以代碼形式實現。 Aspose.PDF 它是一個功能強大的跨平臺庫,可用于處理 .NET、Java 和 Python 中的 PDF 文件。它通過壓縮圖像、刪除未使用的對象、清理元數據和優化內部資源,提供對文件大小的完全控制。無論您需要快速縮減文件大小還是高度自定義的優化流程,Aspose.PDF 都能讓您輕松高效地完成。只需幾行代碼,開發人員即可通過編程方式壓縮 PDF 文件,而無需依賴第三方工具或 Adobe Acrobat。
縮小 PDF 尺寸的目的是在不影響美觀的情況下減小其大小。Aspose.PDF 通過以下方式實現此目的:
您可以使用 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 開發人員,您可以使用 Aspose.PDF for Java 縮小 PDF 尺寸,如下所示:
步驟 1:添加依賴項
將以下內容添加到您的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 并生成更小、更優化的文件以供共享或存儲。
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 是一樣的嗎?
答:是的,這兩個術語都表示減小文件大小,但“縮小”更隨意,“壓縮”更具技術性。
問:縮小 PDF 會影響質量嗎?
答:如果畫質降低太多,確實會這樣。為了保持平衡,建議將畫質控制在 70% 到 80% 之間。
壓縮 PDF 的速度可以快至 10 秒,甚至可以使用代碼實現全自動解決方案。使用 Aspose.PDF,您可以控制文件大小和質量之間的平衡,讓您的 PDF 始終保持快速共享、輕松存儲和愉悅瀏覽。
————————————————————————————————————————
關于慧都科技:
慧都科技是一家行業數字化解決方案公司,長期專注于軟件、油氣與制造行業。公司基于深入的業務理解與管理洞察,以系統化的業務建模驅動技術落地,幫助企業實現智能化運營與長期競爭優勢。在軟件工程領域,我們提供開發控件、研發管理、代碼開發、部署運維等軟件開發全鏈路所需的產品,提供正版授權采購、技術選型、個性化維保等服務,幫助客戶實現技術合規、降本增效與風險可控。慧都科技Aspose在中國的官方授權代理商,提供Aspose系列產品免費試用,咨詢,正版銷售等于一體的專業化服務。Aspose是文檔處理領域的優秀產品,幫助企業高效構建文檔處理的應用程序。
下載|體驗更多Aspose產品,請咨詢,或撥打產品熱線:023-68661681
加入Aspose技術交流QQ群(1041253375),與更多小伙伴一起探討提升開發技能。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網