1. 程式人生 > 程式設計 >Java 新增、刪除、替換、格式化Word中的文字的步驟詳解(基於Spire.Cloud.SDK for Java)

Java 新增、刪除、替換、格式化Word中的文字的步驟詳解(基於Spire.Cloud.SDK for Java)

Spire.Cloud.SDK for Java提供了TextRangesApi介面可通過addTextRange()新增文字、deleteTextRange()刪除文字、updateTextRangeText()替換文字、updateTextRangeFormat()格式化文字等。本文將從以上方法介紹如何來實現對文字的操作。可參考以下步驟進行準備:

一、匯入jar檔案

建立Maven專案程式,通過maven倉庫下載匯入。以IDEA為例,新建Maven專案,在pom.xml檔案中配置maven倉庫路徑,並指定spire.cloud.sdk的依賴,如下:

<repositories>
  <repository>
    <id>com.e-iceblue</id>
    <name>cloud</name>
    <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
  </repository>
</repositories>

<dependencies>
    <dependency>
      <groupId> cloud </groupId>
      <artifactId>spire.cloud.sdk</artifactId>
      <version>3.5.0</version>
    </dependency>

    <dependency>
    <groupId> com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.1</version>
    </dependency>

    <dependency>
      <groupId> com.squareup.okhttp</groupId>
      <artifactId>logging-interceptor</artifactId>
      <version>2.7.5</version>
    </dependency>

    <dependency>
      <groupId> com.squareup.okhttp </groupId>
      <artifactId>okhttp</artifactId>
      <version>2.7.5</version>
    </dependency>

    <dependency>
      <groupId> com.squareup.okio </groupId>
      <artifactId>okio</artifactId>
      <version>1.6.0</version>
    </dependency>

    <dependency>
      <groupId> io.gsonfire</groupId>
      <artifactId>gson-fire</artifactId>
      <version>1.8.0</version>
    </dependency>

    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-annotations</artifactId>
      <version>1.5.18</version>
    </dependency>

    <dependency>
      <groupId> org.threeten </groupId>
      <artifactId>threetenbp</artifactId>
      <version>1.3.5</version>
    </dependency>
</dependencies>

完成配置後,點選“Import Changes” 即可匯入所有需要的jar檔案。如果使用的是Eclipse,可參考這裡的匯入方法。

匯入結果:

Java 新增、刪除、替換、格式化Word中的文字的步驟詳解(基於Spire.Cloud.SDK for Java)

二、登入冰藍雲賬號,建立資料夾,上傳文件

Java 新增、刪除、替換、格式化Word中的文字的步驟詳解(基於Spire.Cloud.SDK for Java)

三、建立應用程式,獲取App ID及App Key

Java 新增、刪除、替換、格式化Word中的文字的步驟詳解(基於Spire.Cloud.SDK for Java)

完成以上步驟後,可參考以下程式碼,進行文件操作。

用於測試的Word源文件如下:

Java 新增、刪除、替換、格式化Word中的文字的步驟詳解(基於Spire.Cloud.SDK for Java)

1. 新增文字到Word

import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.TextRangesApi;

public class AddTextRange {
  //配置App賬號資訊
  static String appId = "App ID";
  static String appKey = "App Key";
  static String baseUrl = "https://api.e-iceblue.cn";
  static Configuration wordConfiguration = new Configuration(appId,appKey,baseUrl);
  static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration);

  public static void main(String[] args) throws ApiException {
    String name = "testfile.docx";//用於測試的Word源文件
    String paragraphPath = "Section/0/Body/0/Paragraph/0";//獲取文件中的段落
    Integer indexInParagraph = 0;
    String text = "新新增的文字內容!";//指定需要新增的文字內容
    String folder = "input";//源文件所在的雲端資料夾
    String storage = null;//冰藍雲端儲存空間
    String password = null;//源文件密碼
    String destFilePath = "output/AddTextRange.docx";//結果文件路徑

    //呼叫方法新增文字內容到Word段落
    textRangesApi.addTextRange(name,paragraphPath,text,destFilePath,folder,storage,indexInParagraph,password);
  }
}

文字新增效果:

Java 新增、刪除、替換、格式化Word中的文字的步驟詳解(基於Spire.Cloud.SDK for Java)

2. 刪除Word中的文字

import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.TextRangesApi;

public class DeleteTextRange {
  //配置App賬號資訊
  static String appId = "App ID";
  static String appKey = "App Key";
  static String baseUrl = "https://api.e-iceblue.cn";
  static Configuration wordConfiguration = new Configuration(appId,baseUrl);
  static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration);

  public static void main(String[] args) throws ApiException {
    String name = "testfile.docx";//源文件
    String paragraphPath = "Section/0/Body/0/Paragraph/0";//獲取段落
    Integer index = 0;
    String folder = "input";//源文件所在資料夾
    String storage = null;//冰藍雲端儲存空間
    String password = null;//源文件密碼
    String destFilePath = "output/DeleteTextRange.docx";//結果文件路徑

    //呼叫方法刪除Word第一段文字
    textRangesApi.deleteTextRange(name,index,password);
  }
}

文字刪除效果:

Java 新增、刪除、替換、格式化Word中的文字的步驟詳解(基於Spire.Cloud.SDK for Java)

3. 替換Word中的文字

import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.TextRangesApi;

public class UpdateTextRange {
  //配置App賬號資訊
  static String appId = "App ID";
  static String appKey = "App Key";
  static String baseUrl = "https://api.e-iceblue.cn";
  static Configuration wordConfiguration = new Configuration(appId,baseUrl);
  static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration);

  public static void main(String[] args) throws ApiException {
    String name = "testfile.docx";//源文件
    String paragraphPath = "Section/0/Body/0/Paragraph/0";//獲取段落
    Integer index = 0;
    String text = "新替換文字";//指定新文字
    String folder = "input";//源文件所在資料夾
    String storage = null;
    String password = null;
    String destFilePath = "output/UpdateTextRangeText.docx";//結果文件路徑

    //呼叫方法更新(替換)原有的文字
    textRangesApi.updateTextRangeText(name,password);
  }
}

文字替換效果:

Java 新增、刪除、替換、格式化Word中的文字的步驟詳解(基於Spire.Cloud.SDK for Java)

4. 格式化Word中的文字

import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.TextRangesApi;
import spire.cloud.word.sdk.client.model.Color;
import spire.cloud.word.sdk.client.model.Font;
import spire.cloud.word.sdk.client.model.TextRangeFormat;

public class UpdateTextRangeFormat {
  //配置App賬號資訊
  static String appId = "App ID";
  static String appKey = "App Key";
  static String baseUrl = "https://api.e-iceblue.cn";
  static Configuration wordConfiguration = new Configuration(appId,baseUrl);
  static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration);

  public static void main(String[] args) throws ApiException {
    String name = "testfile.docx";//源文件
    String paragraphPath = "Section/0/Body/0/Paragraph/0";//獲取段落
    Integer index = 0;

    //建立文字樣式,指定字型、顏色、字號,並應用到文字
    TextRangeFormat format = new TextRangeFormat();
    Color color = new Color(34,139,34);
    Font font = new Font("宋體",20f,color);
    format.setFont(font);
    TextRangeFormat textRange = format;

    String folder = "input";//源文件所在資料夾
    String storage = null;
    String password = null;
    String destFilePath = "output/UpdateTextRangeFormat.docx";//結果文件路徑

    //呼叫方法更新(應用)文字樣式
    textRangesApi.updateTextRangeFormat(name,textRange,password);
  }
}

文字格式設定效果:

Java 新增、刪除、替換、格式化Word中的文字的步驟詳解(基於Spire.Cloud.SDK for Java)

到此這篇關於Java 新增、刪除、替換、格式化Word中的文字的步驟詳解(基於Spire.Cloud.SDK for Java)的文章就介紹到這了,更多相關Java 新增、刪除、替換、格式化Word中的文字內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!