獲取 PowerPoint 主題和顏色信息

使用 Java Cloud SDK 獲取 PowerPoint 主題、字體詳細信息

為了使 PowerPoint 演示文稿更具吸引力和震撼力,我們採用了各種字體和調色板。此外,為了顯示一致的佈局並為最終用戶留下深刻的驚喜體驗,我們應用了 PowerPoint 主題。但是,我們可能從不同來源收到了演示文稿文件,並且有興趣閱讀 PPT 主題詳細信息以及查找調色板,以便進一步利用這些信息。因此在本文中,我們將詳細討論如何以編程方式讀取 Microsoft PowerPoint 主題信息。

PowerPoint 配色方案處理 API

為了創建、閱讀、編輯 PowerPoint 並將其轉換為各種支持的格式,Aspose.Slides Cloud 是一個可靠的解決方案。其基於 REST 的架構使您能夠在任何平台上調用 API。現在,為了在 Java 應用程序中獲得所有這些功能,我們專門創建了 Aspose.Slides Cloud SDK for Java,它是 Cloud API 的包裝器。現在,為了在 Java 應用程序中使用 SDK,第一步是通過在 maven 構建類型項目的 pom.xml 中包含以下信息來在項目中添加其引用。

<repositories> 
    <repository>
        <id>aspose-cloud</id>
        <name>artifact.aspose-cloud-releases</name>
        <url>http://artifact.aspose.cloud/repo</url>
    </repository>   
</repositories>

<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-slides-cloud</artifactId>
        <version>22.9.0</version>
    </dependency>
</dependencies>

如果您還沒有創建帳戶,您可以使用有效的電子郵件地址通過 Aspose Cloud 註冊免費試用。現在使用新創建的帳戶登錄並在 Cloud Dashboard 查找/創建客戶端 ID 和客戶端密碼。在以下部分中,出於身份驗證目的需要這些詳細信息。

獲取 PowerPoint 主題信息

如上所述,我們使用演示文稿模板,以便我們在整個 PowerPoint 演示文稿中擁有一致的主題/佈局。但是,我們可能需要獲取 PowerPoint 主題詳細信息以進行進一步處理。此外,演示設計元素的主要屬性由演示主題決定。每個主題都使用自己獨特的一組顏色、字體和效果來創建幻燈片的整體外觀。以下詳細信息允許您使用 Java 代碼片段從 PowerPoint 演示文稿中讀取有關幻燈片主題的信息。

  • 首先,創建 SlidesApi 對象的對象,同時提供 ClientID 和 Client secret 作為參數
  • 其次,創建一個以輸入的PowerPoint模板文件地址為參數的File實例
  • 第三,使用 readAllBytes(…) 讀取 PowerPoint 文件的內容並將其保存在 byte[] 數組中
  • 現在使用 uploadFile(…) 方法將輸入的 PowerPoint 上傳到雲存儲
  • 最後在提供輸入 PowerPoint PowerPoint 名稱、幻燈片索引的同時調用 getTheme(…) 方法。信息顯示在控制台中。
// 有關詳細信息,請訪問 https://github.com/aspose-slides-cloud/aspose-slides-cloud-java

try
    {	    
        // 從 https://dashboard.aspose.cloud/ 獲取 ClientID 和 ClientSecret
        String clientId = "7ef10407-c1b7-43bd-9603-5ea9c6db83cd";
	String clientSecret = "ba7cc4dc0c0478d7b508dd8ffa029845";

	// 創建 SlidesApi 的實例
	SlidesApi slidesApi = new SlidesApi(clientId,clientSecret);
  
        // 從本地系統加載文件
	File f = new File("tf03431377_win32.potx");

	// 加載第一個 PowerPoint 演示文稿
	byte[] bytes = Files.readAllBytes(f.toPath());
	// 將演示文稿上傳到雲存儲
	slidesApi.uploadFile("source.potx", bytes, null);
	
	// 閱讀第三張幻燈片中的主題。
	var slideTheme = slidesApi.getTheme("source.potx", 1, null, null, null);

	// 印刷資源參考配色方案、字體方案和格式方案。
	System.out.println(slideTheme.getColorScheme().getHref());
	System.out.println(slideTheme.getFontScheme().getHref());
	System.out.println(slideTheme.getFormatScheme().getHref());
    }catch(Exception ex)
    {
        System.out.println(ex);
    }

在 Java 中閱讀 PowerPoint 配色方案

PowerPoint 處理 API 還使我們能夠使用 Java 代碼片段讀取 powerPoint 配色方案詳細信息。 API 期望源文件在雲存儲中可用。

  • 首先,創建 SlidesApi 對象的對象,同時提供 ClientID 和 Client secret 作為參數
  • 其次,調用 getColorScheme(…) 方法,該方法需要來自云存儲的 PowerPoint 和幻燈片索引作為參數
  • 現在通過調用 getHyperlink(…) 方法在控制台中打印配色方案信息
// 更多詳情,請訪問 https://github.com/aspose-slides-cloud/aspose-slides-cloud-java

try
    {   
        // 從 https://dashboard.aspose.cloud/ 獲取 ClientID 和 ClientSecret
        String clientId = "7ef10407-c1b7-43bd-9603-5ea9c6db83cd";
	String clientSecret = "ba7cc4dc0c0478d7b508dd8ffa029845";

        // 創建 SlidesApi 的實例
	SlidesApi slidesApi = new SlidesApi(clientId,clientSecret);

        // 閱讀應用於第一張幻燈片的配色方案。
	var colorScheme = slidesApi.getColorScheme("source.potx", 1, null, null, null);

	// 打印超鏈接顏色。
	System.out.println("Hyperlink color: " + colorScheme.getHyperlink());
    }catch(Exception ex)
    {
      System.out.println(ex);
    }

上例中使用的示例演示模板可以從 RainbowPresentation.potx 下載。

使用 Java 獲取 PowerPoint 字體

在本節中,我們將討論讀取 PowerPoint 字體信息的步驟。所以我們可以單獨在 PowerPoint 幻燈片之間遍歷並檢索字體信息

  • 第一步是創建 SlidesApi 對象的實例
  • 其次,創建一個 FontScheme 對象,它將保存 getFontScheme(…) 方法返回的數據
  • 現在為了檢索字體信息,請調用 getName(…) 方法並在控制台打印信息
// 更多詳情,請訪問 https://github.com/aspose-slides-cloud/aspose-slides-cloud-java

try
    {   
        // 從 https://dashboard.aspose.cloud/ 獲取 ClientID 和 ClientSecret
        String clientId = "7ef10407-c1b7-43bd-9603-5ea9c6db83cd";
	String clientSecret = "ba7cc4dc0c0478d7b508dd8ffa029845";

        // 創建 SlidesApi 的實例
	SlidesApi slidesApi = new SlidesApi(clientId,clientSecret);

	// 閱讀第一張幻燈片中的字體方案。
        FontScheme fontScheme = slidesApi.getFontScheme("source.potx", 2, null, null, null);

	// 打印字體方案名稱。
	System.out.println(fontScheme.getName());    
    }catch(Exception ex)
    {
      System.out.println(ex);
    }

使用 cURL 命令讀取 PowerPoint 字體

現在是使用 cURL 命令閱讀字體方案詳細信息的時候了。但是,作為先決條件,我們需要在執行以下命令時首先生成 JWT 訪問令牌(基於客戶端憑據)。

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

現在我們有了 JWT 令牌,請執行以下命令。

curl -v -X GET "https://api.aspose.cloud/v3.0/slides/source.potx/slides/2/theme/fontScheme" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>"

下面給出的是執行命令後的響應正文內容

{
  "major": {
    "complexScript": "Arial",
    "eastAsian": "Segoe Print",
    "latin": "Segoe Print"
  },
  "minor": {
    "complexScript": "Arial",
    "eastAsian": "Segoe Print",
    "latin": "Segoe Print"
  },
  "name": "Segoe Print",
  "selfUri": {
    "href": "https://api.aspose.cloud/v3.0/slides/source.potx/slides/2/theme/fontScheme",
    "relation": "self",
    "slideIndex": 2
  }
}

結論

我們剛剛學習瞭如何獲取 PowerPoint 主題信息、如何讀取 PowerPoint 配色方案信息以及如何檢索 PowerPoint 字體詳細信息的詳細信息。除了 Java 代碼片段,您還可以檢索這些 uisng cURL 命令的細節。請注意,我們所有的 Cloud SDK 都是在 MIT 許可下發布的,因此您可以考慮從 GitHub 下載完整的源代碼並根據您的要求進行修改。如有任何問題,您可以考慮通過免費的 產品支持論壇 聯繫我們尋求快速解決方案。

相關文章

請訪問以下鏈接以了解更多信息: