We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose Word 查詢特定字串和確認Table數量

您好,

請問要如何使用 Aspose Word 去尋找Word內是否有特定字串呢?
例如: 檢查 Word 內是否有 “Hello World” 這個字串
以及如何可以知道Word內有幾個Table呢?

有查看Document,看到比較多是replace,不過這裡並沒有取代的需求,只是想要單純的返回文檔中是否有該字串。

謝謝

@Hankch, 请参阅以下 Aspose.Words for Java 示例:

public static void main(String[] args) throws Exception {
    License lic = new License();
    lic.setLicense("Aspose.Words.Java.lic");

    Document doc = new Document("test.docx");

    // Check whether the string is present in the document.
    String str = "Hello World";

    FindReplaceOptions opt = new FindReplaceOptions();
    SearchOnlyCallback searchOnlyCallback = new SearchOnlyCallback();
    opt.setReplacingCallback(searchOnlyCallback);
    doc.getRange().replace(str, "", opt);
    System.out.println(str + " is present: " + searchOnlyCallback.getIsPresent());

    // Get the number of tables in the document.
    NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true);
    System.out.println("Number of tables: " + tables.getCount());
}

private static class SearchOnlyCallback implements IReplacingCallback {
    private boolean mIsPresent = false;

    public boolean getIsPresent() {
        return mIsPresent;
    }

    public int replacing(ReplacingArgs args) throws Exception {
        mIsPresent = true;
        return ReplaceAction.STOP;
    }
}

test.docx (12.5 KB)

以下是 API 参考:

很抱歉這麼晚才看到,十分謝謝您提供的協助。
我會用上述方法來試驗看看的,謝謝您。