想在Java中把PPT轉化為PDF嗎?教你用Aspose.Slides輕鬆搞定

慧都科技 發佈 2020-01-02T09:43:19+00:00

//Instantiate a Presentation object that represents a presentation file


PDF已成為最廣泛和最常用的數字文檔格式。由於PDF格式具有固定的布局,因此大多數文檔在共享之前都已轉換為PDF。

在將各種文檔轉換為PDF格式的過程中,PPT到PDF的轉換是一種流行的用例,且非常的方便省時,特別是當必須將大量PowerPoint演示文稿轉換為PDF時。如果您希望在Java中通過編程的方式將PPT或PPTX轉換為PDF,那麼你可以認真閱讀本文的教程,將演示如何使用 Aspose.Slides for Java API提供的各種選項。

如果您還未使用過Java版Aspose.Slides,可點擊文末「了解更多」下載最新版體驗。


在本文中,我們將介紹使用Aspose.Slides for Java的以下轉換方案:

  • 使用Java將PowerPoint PPT / PPTX轉換為PDF
  • 使用自定義選項將PPT / PPTX轉換為PDF
  • 將PPT / PPTX轉換為PDF,包括隱藏的幻燈片
  • 將PPT / PPTX轉換為受密碼保護的PDF
  • 將PPT / PPTX的特定幻燈片轉換為PDF
  • 將PPT / PPTX轉換為具有訪問權限的PDF

①將Java中的PPT / PPTX轉換為PDF

以下是使用Aspose.Slides for Java提供的默認選項將PowerPoint演示文稿轉換為PDF的簡單步驟。

  • 使用Presentation對象加載PowerPoint PPT / PPTX 。
  • 調用save()方法傳遞輸出的PDF文件名和輸出格式。

以下代碼示例顯示如何使用默認選項將Java中的PPTX轉換為PDF。

// Instantiate a Presentation object that represents a presentation file
Presentation pres = new Presentation("presentation.pptx");

// Save the presentation to PDF with default options
pres.save("output.pdf", SaveFormat.Pdf);

②使用自定義選項將PPT / PPTX轉換為PDF

Aspose.Slides for Java提供了PdfOptions類,可自定義PowerPoint到PDF的轉換。PdfOptions類使您可以指定JPEG質量,定義圖元文件的行為,設置文本壓縮級別,PDF遵從級別以及其他選項。以下是使用自定義選項將PPT / PPTX轉換為PDF的步驟。

  • 使用Presentation對象加載PowerPoint PPT / PPTX 。
  • 創建PdfOptions類的對象。
  • 設置/指定PdfOptions類公開的選項。
  • 調用save()方法。

下面的代碼示例演示如何使用自定義選項將PowerPoint PPTX轉換為Java中的PDF。

// Instantiate a Presentation object that represents a presentation file
Presentation pres = new Presentation("presentation.pptx");

// Instantiate the PdfOptions class
PdfOptions opts = new PdfOptions();
               
// Set JPEG Quality
opts.setJpegQuality((byte) 90);

// Define behavior for Metafiles
opts.setSaveMetafilesAsPng(true);

// Set Text Compression level
opts.setTextCompression(PdfTextCompression.Flate);

// Define the PDF standard
opts.setCompliance(PdfCompliance.Pdf15);
              
INotesCommentsLayoutingOptions options = opts.getNotesCommentsLayouting();
options.setNotesPosition(NotesPositions.BottomFull);

// Save the presentation to PDF with specified options
pres.save("output.pdf", SaveFormat.Pdf, opts);

③將PPT / PPTX轉換為PDF(包括隱藏的幻燈片)

PowerPoint演示文稿包含隱藏的幻燈片時,可能會出現這種情況。在默認的PowerPoint到PDF轉換中,Aspose.Slides for Java將忽略隱藏的幻燈片。但是,如果要在轉換後的PDF中包含隱藏的幻燈片,則可以使用PdfOptions.setShowHiddenSlides(true)選項。

下面的代碼示例演示如何將PowerPoint PPTX轉換為PDF,包括Java中的隱藏幻燈片。

Presentation pres = new Presentation("presentation.pptx");
try {
        // Instantiate the PdfOptions class
        PdfOptions pdfOptions = new PdfOptions();

        // Specify that the generated document should include hidden slides
        pdfOptions.setShowHiddenSlides(true);

        // Save the presentation to PDF with specified options
        pres.save("output.pdf", SaveFormat.Pdf, pdfOptions);
} finally {
        if (pres != null)
                pres.dispose();
}

④將PPT / PPTX的特定幻燈片轉換為PDF

Aspose.Slides for Java還允許選擇要包含在生成的PDF文檔中的幻燈片。可以創建一個數組以指定要包含在PPTX到PDF轉換中的幻燈片編號,並將其傳遞給save()方法。

下面的代碼示例演示如何在Java中將PowerPoint PPTX的特定幻燈片轉換為PDF。

// Instantiate a Presentation object that represents a presentation file
Presentation pres = new Presentation("presentation.pptx");
// Setting array of slides positions
int[] slides = new int[] { 2, 3, 5 };

// Save the presentation to PDF
pres.save("output.pdf", slides, SaveFormat.Pdf);

⑤將PPT / PPTX轉換為受密碼保護的PDF

將PowerPoint演示文稿轉換為受密碼保護的PDF,以保護文檔。可以使用 PdfOptions.setPassword(「 password」)設置密碼 ,並將PdfOptions對象傳遞給save()方法。

下面的代碼示例演示如何將PowerPoint PPTX轉換為Java中受密碼保護的PDF。

// Instantiate a Presentation object that represents a presentation file
Presentation pres = new Presentation("demo.pptx");

// Instantiate the PdfOptions class
PdfOptions opts = new PdfOptions();

// Setting PDF password
opts.setPassword("password");

// Save the presentation to password protected PDF
pres.save("output.pdf", SaveFormat.Pdf, opts);

⑥使用訪問權限將PPT / PPTX轉換為PDF

PDF格式允許您指定不同的訪問權限,例如列印權限,添加或修改文本注釋或表單欄位的權限等。根據此功能,Aspose.Slides for Java提供了為從PowerPoint演示文稿轉換而來的PDF文檔設置權限的功能。該PdfAccessPermissions類包含你可以在PowerPoint演示文稿應用到PDF轉換不同的權限類型的標誌集合。

下面的Java代碼示例演示如何將具有訪問權限的PowerPoint演示文稿轉換為PDF。

// Create PDF options
PdfOptions pdfOptions = new PdfOptions();
// Set password
pdfOptions.setPassword("my_password");
// Set access permissions
pdfOptions.setAccessPermissions(PdfAccessPermissions.PrintDocument| PdfAccessPermissions.HighQualityPrint);

// Load PowerPoint presentation
Presentation presentation = new Presentation("Presentation.pptx");
try {
   presentation.save("output.pdf", SaveFormat.Pdf, pdfOptions);
} finally {
   if (presentation != null) presentation.dispose();
}

還想要更多嗎?如果您有任何需求,請隨時加入Aspose技術交流群(642018183),我們很高興為您提供查詢和諮詢。

關鍵字: