Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Insert bookmark text
The example shows you how to update bookmark's content.
import com.independentsoft.office.ExtendedBoolean; import com.independentsoft.office.word.BookmarkStart; import com.independentsoft.office.word.Paragraph; import com.independentsoft.office.word.Run; import com.independentsoft.office.word.WordDocument; public class Example { public static void main(String[] args) { try { WordDocument doc = new WordDocument("c:\\test\\input.docx"); for (int i = 0; i < doc.getBody().getContent().size(); i++) { if (doc.getBody().getContent().get(i) instanceof Paragraph) { Paragraph paragraph = (Paragraph) doc.getBody().getContent().get(i); for (int j=0; j < paragraph.getContent().size(); j++) { if (paragraph.getContent().get(j) instanceof BookmarkStart) { BookmarkStart bookmarkStart = (BookmarkStart) paragraph.getContent().get(j); if (bookmarkStart.getName().equals("mybookmark")) { Run run1 = new Run(); run1.addText("some text"); run1.setBold(ExtendedBoolean.TRUE); paragraph.getContent().add(j,run1); break; } } } } } doc.save("c:\\test\\output.docx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*