簡體   English   中英

Aspose Words-僅刪除書簽的一部分

[英]Aspose Words - remove only part of bookmarks

我在刪除書簽和段落時遇到問題。

書簽: SUB_1SUB_2SUB_3SUB_4SUB_5

循環執行后,僅刪除SUB_1,SUB_3SUB_5

            BookmarkCollection bookmarks = doc.getRange().getBookmarks();
            DocumentBuilder builder = new DocumentBuilder(doc);

            System.out.println(bookmarks.getCount()); 

            for(Bookmark b : bookmarks) {
                System.out.println("bookmark: " + b.getName());
                builder.moveToBookmark(b.getName());
                builder.getCurrentParagraph().remove(); 
            }

輸出:

10:13:10,379 INFO  [stdout] 5

10:13:10,380 INFO  [stdout] (EJB default - 8) bookmark: SUB_1

10:13:10,381 INFO  [stdout] (EJB default - 8) bookmark: SUB_3

10:13:10,382 INFO  [stdout] (EJB default - 8) bookmark: SUB_5

您也可以使用bookmark.remove()方法刪除書簽,而不是將光標移動到段落然后刪除該段落。

我在Aspose擔任開發人員推廣員。

我解決了我的問題。 它不是很好,但是對我有用。

        BookmarkCollection bookmarks = doc.getRange().getBookmarks();
        DocumentBuilder builder;
        try {
            builder = new DocumentBuilder(doc);
            while(bookmarks.getCount() > 0) {
                for(Bookmark b : bookmarks) {
                    System.out.println("bookmark: " + b.getName());
                    b.setText("");
                    builder.moveToBookmark(b.getName());
                    builder.getCurrentParagraph().remove(); 
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Problem with deleting bookmark");
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM