I want to insert a new page generation directory on the second page of word after generating the word from html. How do I write the code?
eg:builder.moveToDocumentStart();this method is to move to the start page of document,how can I move to the scecond page of document
You can simulate moving cursor to the start of any particular Page in Word document by using the following Java code:
int pageIndex = 2;
Document doc = new Document("E:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Node[] runs = doc.getChildNodes(NodeType.RUN, true).toArray();
for (int i = 0; i < runs.length; i++) {
Run run = (Run) runs[i];
int length = run.getText().length();
Run currentNode = run;
for (int x = 1; x < length; x++) {
currentNode = SplitRun(currentNode, 1);
}
}
NodeCollection smallRuns = doc.getChildNodes(NodeType.RUN, true);
LayoutCollector collector = new LayoutCollector(doc);
for (int i = 0; i < smallRuns.getCount(); i++) {
Run run = (Run) smallRuns.get(i);
if (collector.getStartPageIndex(run) == pageIndex) {
builder.moveTo(run);
break;
}
}
builder.getFont().setColor(Color.RED);
builder.write("New text at start of some page");
doc.joinRunsWithSameFormatting();
doc.save("E:\\Temp\\awjava-20.6.docx");
Thank you very much, I will try, if I still have questions, please advise
4 posts were split to a new topic: Change Font Formatting Name Size of Table of Content (TOC) Field Text in Word Document using Java