Java使用Spire.Presentation for Java將PPT轉(zhuǎn)換為PDF
在日常開(kāi)發(fā)或辦公場(chǎng)景中,經(jīng)常需要將 PowerPoint 演示文稿(PPT/PPTX)轉(zhuǎn)換為 PDF。PDF 文件不僅能保持統(tǒng)一的排版效果,還能方便共享、歸檔和打印。對(duì)于 Java 開(kāi)發(fā)者,可以借助 Spire.Presentation for Java 來(lái)實(shí)現(xiàn)這一功能。本文將介紹從基礎(chǔ)轉(zhuǎn)換到高級(jí)設(shè)置的多種用法。
為什么要將 PowerPoint 轉(zhuǎn)換為 PDF
- 跨平臺(tái)兼容性
PDF 可以在不同操作系統(tǒng)和設(shè)備上保持一致的顯示效果,而 PPT 文件在不同版本的 PowerPoint 或兼容軟件中可能會(huì)出現(xiàn)格式錯(cuò)亂。 - 長(zhǎng)期保存與歸檔
與可編輯的 PPT 相比,PDF 更適合歸檔。特別是 PDF/A 標(biāo)準(zhǔn),專為電子文檔長(zhǎng)期保存而設(shè)計(jì)。 - 文件安全性
PPT 文檔容易被編輯,而 PDF 可以設(shè)置只讀、加密、添加權(quán)限控制,從而防止未經(jīng)授權(quán)的修改。 - 便于分發(fā)與打印
PDF 文件通常更小巧,方便通過(guò)郵件或系統(tǒng)分發(fā),同時(shí)在打印時(shí)不會(huì)因?yàn)樽煮w或布局差異而出現(xiàn)偏差。
安裝 Spire.Presentation for Java
在開(kāi)始編寫(xiě)代碼前,需要先在 Java 項(xiàng)目中引入 Spire.Presentation for Java 庫(kù)。安裝方式主要有以下兩種:
方式一:Maven 倉(cāng)庫(kù)引入
如果使用 Maven 管理項(xiàng)目,可以在 pom.xml 中添加依賴:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation</artifactId>
<version>10.9.3</version>
</dependency>保存后,Maven 會(huì)自動(dòng)下載并引入該庫(kù)。
方式二:手動(dòng)導(dǎo)入 Jar 包
如果項(xiàng)目不是 Maven 管理的,可以:
- 從官網(wǎng)下載對(duì)應(yīng)版本的 Spire.Presentation for Java。
- 將下載的
spire.presentation.jar導(dǎo)入到項(xiàng)目中。
完成安裝后,就可以在 Java 程序中直接 import com.spire.presentation.*; 來(lái)使用 API。
基本轉(zhuǎn)換:將 PPT 轉(zhuǎn)為 PDF
最常見(jiàn)的場(chǎng)景是直接把整個(gè) PPT 文件轉(zhuǎn)換成 PDF,不需要額外設(shè)置。
import com.spire.presentation.*;
public class PPTtoPDF {
public static void main(String[] args) throws Exception {
// 加載 PowerPoint 文檔
Presentation presentation = new Presentation();
presentation.loadFromFile("Input.pptx");
// 轉(zhuǎn)換為 PDF
presentation.saveToFile("ToPdf.pdf", FileFormat.PDF);
// 釋放資源
presentation.dispose();
}
}轉(zhuǎn)換為 PDF/A 格式
如果需要長(zhǎng)期保存或歸檔,可以將文檔轉(zhuǎn)換為符合 PDF/A 標(biāo)準(zhǔn)的文件。
import com.spire.presentation.*;
public class PPTtoPDFA {
public static void main(String[] args) throws Exception {
Presentation presentation = new Presentation();
presentation.loadFromFile("Input.pptx");
SaveToPdfOption options = presentation.getSaveToPdfOption();
options.setPdfConformanceLevel(PdfConformanceLevel.Pdf_A1A);
presentation.saveToFile("ToPdfa.pdf", FileFormat.PDF);
presentation.dispose();
}
}轉(zhuǎn)換為加密 PDF
生成的 PDF 可以設(shè)置密碼,并控制權(quán)限(如允許打印或填寫(xiě)表單)。
import com.spire.presentation.*;
public class PPTtoEncryptedPDF {
public static void main(String[] args) throws Exception {
Presentation presentation = new Presentation();
presentation.loadFromFile("Input.pptx");
SaveToPdfOption option = presentation.getSaveToPdfOption();
option.getPdfSecurity().encrypt("abc-123", PdfPermissionsFlags.Print | PdfPermissionsFlags.FillFields);
presentation.saveToFile("ToEncryptedPdf.pdf", FileFormat.PDF);
presentation.dispose();
}
}包含隱藏幻燈片
默認(rèn)情況下,隱藏的幻燈片不會(huì)出現(xiàn)在導(dǎo)出的 PDF 中。如果需要包含它們,可以這樣設(shè)置:
import com.spire.presentation.*;
public class PPTtoPDFWithHiddenSlides {
public static void main(String[] args) throws Exception {
Presentation presentation = new Presentation();
presentation.loadFromFile("Input.pptx");
SaveToPdfOption option = presentation.getSaveToPdfOption();
option.setContainHiddenSlides(true);
presentation.saveToFile("ToPdfWithHiddenSlides.pdf", FileFormat.PDF);
presentation.dispose();
}
}自定義頁(yè)面大小
有時(shí)需要將幻燈片導(dǎo)出為特定尺寸的 PDF,比如 A4 紙或自定義大小。
import com.spire.presentation.*;
import java.awt.geom.*;
public class PPTtoCustomSizePDF {
public static void main(String[] args) throws Exception {
Presentation presentation = new Presentation();
presentation.loadFromFile("Input.pptx");
// 設(shè)置幻燈片大小為自定義尺寸
presentation.getSlideSize().setType(SlideSizeType.CUSTOM);
presentation.getSlideSize().setSize(new Dimension2D.Float(750, 500));
presentation.setSlideSizeAutoFit(true);
presentation.saveToFile("ToPdfWithCustomSlideSize.pdf", FileFormat.PDF);
presentation.dispose();
}
}導(dǎo)出單個(gè)幻燈片
除了整個(gè)文檔,也可以只將某一頁(yè)幻燈片單獨(dú)保存為 PDF。
import com.spire.presentation.*;
public class SingleSlideToPDF {
public static void main(String[] args) throws Exception {
Presentation presentation = new Presentation();
presentation.loadFromFile("Input.pptx");
// 獲取第二頁(yè)幻燈片(索引從 0 開(kāi)始)
ISlide slide = presentation.getSlides().get(1);
// 單獨(dú)保存該頁(yè)為 PDF
slide.saveToFile("SlideToPdf.pdf", FileFormat.PDF);
presentation.dispose();
}
}總結(jié)
將 PowerPoint 轉(zhuǎn)換為 PDF 的需求在文檔分發(fā)、長(zhǎng)期存檔和安全防護(hù)中都非常常見(jiàn)。通過(guò) Spire.Presentation for Java,開(kāi)發(fā)者不僅能完成基礎(chǔ)的 PPT 轉(zhuǎn) PDF,還能根據(jù)需要靈活設(shè)置輸出格式,比如 PDF/A 合規(guī)、加密、包含隱藏幻燈片、自定義頁(yè)面大小以及單頁(yè)導(dǎo)出等。借助這些功能,可以更高效地滿足不同場(chǎng)景下的文檔處理需求。
到此這篇關(guān)于Java使用Spire.Presentation for Java將PPT轉(zhuǎn)換為PDF的文章就介紹到這了,更多相關(guān)Java實(shí)現(xiàn)PowerPoint轉(zhuǎn)PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
AgentScope Java 集成 Spring AI Alibab
本文提出了一個(gè)基于AgentScope、SpringAI-Alibaba和Java2AI生態(tài)GraphCore的集成方案,涵蓋核心思路、工程配置、代碼實(shí)現(xiàn)和最佳實(shí)踐,感興趣的朋友跟隨小編一起看看吧2026-04-04
JDK1.8源碼下載及idea2021導(dǎo)入jdk1.8源碼的詳細(xì)步驟
這篇文章主要介紹了JDK1.8源碼下載及idea2021導(dǎo)入jdk1.8源碼的詳細(xì)步驟,在文章開(kāi)頭就給大家分享了JDK1.8源碼下載地址和下載步驟,告訴大家idea2021.1.3導(dǎo)入JDK1.8源碼步驟,需要的朋友可以參考下2022-11-11
Spring Cloud Zipkin服務(wù)端追蹤服務(wù)
這篇文章主要介紹了Spring Cloud Zipkin服務(wù)端追蹤服務(wù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Spring通過(guò)配置文件管理Bean對(duì)象的方法
這篇文章主要介紹了Spring通過(guò)配置文件管理Bean對(duì)象的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
Spring MVC接口防數(shù)據(jù)篡改和重復(fù)提交
這篇文章主要為大家詳細(xì)介紹了Spring MVC接口防數(shù)據(jù)篡改和重復(fù)提交,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
使用maven方式創(chuàng)建springboot項(xiàng)目的方式
使用Spring Initializr創(chuàng)建spring boot項(xiàng)目,因?yàn)橥饩W(wǎng)問(wèn)題導(dǎo)致很難成功,所以只能使用maven方式,這里介紹下使用maven方式創(chuàng)建springboot項(xiàng)目的方法,感興趣的朋友一起看看吧2022-09-09
SpringBoot排除不需要的自動(dòng)配置類(lèi)DataSourceAutoConfiguration問(wèn)題
這篇文章主要介紹了SpringBoot排除不需要的自動(dòng)配置類(lèi)DataSourceAutoConfiguration問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Eclipse轉(zhuǎn)Itellij IDEA導(dǎo)入Git/svn本地項(xiàng)目的詳細(xì)步驟
這篇文章主要介紹了Eclipse轉(zhuǎn)Itellij IDEA導(dǎo)入Git/svn本地項(xiàng)目,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
每日六道java新手入門(mén)面試題,通往自由的道路第二天
這篇文章主要為大家分享了最有價(jià)值的6道java面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,對(duì)hashCode方法的設(shè)計(jì)、垃圾收集的堆和代進(jìn)行剖析,感興趣的小伙伴們可以參考一下2021-06-06

