Wiki

Clone wiki

Aspose Java for Docx4j / Add-Bookmarks

Aspose.Words

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.startBookmark("AsposeBookmark");
builder.writeln("Text inside a bookmark.");
builder.endBookmark("AsposeBookmark");

// By index.
Bookmark bookmark1 = doc.getRange().getBookmarks().get(0);

// By name.
Bookmark bookmark2 = doc.getRange().getBookmarks().get("AsposeBookmark");

Docx4j

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

String outputfilepath = "data/Docx4j_BookmarkAdd.docx";     

wordMLPackage.getMainDocumentPart().addParagraphOfText("x");
wordMLPackage.getMainDocumentPart().addParagraphOfText("x");
wordMLPackage.getMainDocumentPart().addParagraphOfText("hello world");
P p = (P)wordMLPackage.getMainDocumentPart().getContent().get(2);
R r = (R)p.getContent().get(0);

String bookmarkName = "abcd"; 
bookmarkRun(p,r, bookmarkName, 123);

wordMLPackage.getMainDocumentPart().addParagraphOfText("x");
wordMLPackage.getMainDocumentPart().addParagraphOfText("x");

// Now add an internal hyperlink to it
Hyperlink h = MainDocumentPart.hyperlinkToBookmark(bookmarkName, "link to bookmark");
wordMLPackage.getMainDocumentPart().addParagraphOfText("some text").getContent().add(h);

Download Source Code

Updated