Aspose.Words for Java ブックマークの設定

①Asposeのメソッド等を使用することで
特定の文字列と完全一致する行に対して、HTML⇒WORD変換時に、WORDのブックマークを設定することは出来ますか。

②Asposeのメソッド等を使用することで
WORD内の特定の文字列と完全一致する行に対して、WORDのブックマークを設定することは出来ますか。
Aspose問合せ_技術TM課題114(5).zip (124.0 KB)

@dirbi,

以下のサンプル文書を参照してください。
Html-Word-Bookmark.zip (4.9 KB)

Aspose.Words for Javaを使用して "in.html"を "18.5.docx"に変換すると、この変換中にブックマーク "bm"が保持されます。

ご回答有難うございます。

①Asposeのメソッド等を使用することで (HTML⇒WORD変換時)
⇒bmは確認しました。どのように設定すれば良いでしょうか。実装方法を教えてください。
⇒<a タグで指定するのでしょうか。

②Asposeのメソッド等を使用することで (WORD内)
⇒再度質問させてください。
⇒WORD内の特定の文字列と完全一致する行に対して、WORDのブックマークを設定することは出来ますか。実装方法を教えてください。

@dirbi,

はい、<a>タグで表されます。 次の簡単なコードを実行してみてください:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml("<a name='bm'><span>This is bookmarked</span></a>");
doc.save("D:\\Temp\\awjava-18.5.docx");

@dirbi,

これらの入力/出力Word文書も参照してください。
Docs.zip (9.8 KB)

次のコードは "World"という単語をブックマークします。

Document doc = new Document("D:\\temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveTo(doc.getFirstSection().getBody().getFirstParagraph().getLastChild());
builder.startBookmark("bm");
builder.moveToDocumentEnd();
builder.endBookmark("bm");
doc.save("D:\\Temp\\awjava-18.5.docx");

有難うございます。

ブックマークが設定されることは確認しましたが、特定の文字列に対してどう設定するのか不明です。
out.zip (10.7 KB)

添付ファイルのように設定したい場合、どう実装するのでしょうか。

>builder.insertHtml(“This is bookmarked”);
新たに追加(insertHtml)ではなく、inputとなるhtmlの中身(特定の文字列と完全一致する行)に対して、ブックマーク(WORDファイル)を設定したいです。

@dirbi,

ドキュメント内の任意のテキストをブックマークすることができます。 次のコードは、 "out.docx"内の "Detail info"というテキストを見つけてブックマークします。 お役に立てれば。

Document doc = new Document("D:\\temp\\out\\out.docx");

FindReplaceOptions opts = new FindReplaceOptions();
opts.setDirection(FindReplaceDirection.BACKWARD);
opts.setReplacingCallback(new ReplacingCallback());

doc.getRange().replace(Pattern.compile("Detail info"), "" , opts);
///////////////////////////////////////
static class ReplacingCallback implements IReplacingCallback {

    public int replacing(ReplacingArgs e) throws Exception {
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.getMatchNode();

        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        if (e.getMatchOffset() > 0)
            currentNode = splitRun((Run) currentNode, e.getMatchOffset());

        ArrayList runs = new ArrayList();

        // Find all runs that contain parts of the match string.
        int remainingLength = e.getMatch().group().length();
        while ((remainingLength > 0) && (currentNode != null) && (currentNode.getText().length() <= remainingLength)) {
            runs.add(currentNode);
            remainingLength = remainingLength - currentNode.getText().length();

            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do {
                currentNode = currentNode.getNextSibling();
            } while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
        }

        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength > 0)) {
            splitRun((Run) currentNode, remainingLength);
            runs.add(currentNode);
        }

        DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
        builder.moveTo((Run) runs.get(0));

        builder.startBookmark("marker");
        BookmarkEnd end = builder.endBookmark("marker");

        Run lastRun = (Run) runs.get(runs.size() - 1);
        lastRun.getParentNode().insertAfter(end, lastRun);

        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.SKIP;
    }

    /**
     * Splits text of the specified run into two runs. Inserts the new run just
     * after the specified run.
     */
    private Run splitRun(Run run, int position) throws Exception {
        Run afterRun = (Run) run.deepClone(true);
        afterRun.setText(run.getText().substring(position));
        run.setText(run.getText().substring((0), (0) + (position)));
        run.getParentNode().insertAfter(afterRun, run);
        return afterRun;
    }
}