Hi,
This topic follows another one (Insert section break generates incorrect output using Java)
The context:
I have a .dot file containing a first page with a landscape layout and text box (simulated header when printing the page)
In this document, I have also two bookmarks: Change and Back.
When I found the ‘Change’ bookmark, I have to copy/paste the first section in landscape.
When I found the ‘Back’ bookmark, I have to change the layout as portrait (with all its header/footer of course).
My problem is:
1) If I have a bookmark in the landscape ‘text box’, when I copy/paste the landscape section and I print the content of each bookmark, the bookmark has the ‘content’ of all the page (!).
2) If I have to copy/paste many times the first section, what about the bookmark ? Because a bookmark has a unique name…
Please find the code (and in attachment the .dot file):
Document doc = new Document(new File(“D:\Doc3.dot”)getAbsolutePath());
try {
DocumentBuilder builder = new DocumentBuilder(doc);
Bookmark changeOrientation = doc.getRange().getBookmarks().get(“Change”);
// Go to the bookmark Change
builder.moveTo(changeOrientation.getBookmarkStart());
// Get HeaderFooter for Portrait layout
HeaderFooterCollection hfCollection = builder.getCurrentSection()
.getHeadersFooters();
Section landscape = builder.getCurrentSection();
builder.writeln(“Before Section break new page”);
// Get the current section index to insert the landscape section after
int index = doc.getSections().indexOf(landscape);
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
// Get the first section which is the rendering of the landscape
Section firstSection = doc.getFirstSection();
// Duplicate the first section
Section newSection = (Section) doc.importNode(firstSection, true);
// Insert the landscape section after the current section
builder.moveToSection(index + 1);
doc.getSections().insert(index + 1, newSection);
// The landscape header/footer is not link to previous
newSection.getHeadersFooters().linkToPrevious(false);
doc.getSections().get(index + 1).getPageSetup().setOrientation(
Orientation.LANDSCAPE);
builder.moveTo(doc.getSections().get(index + 1).getBody()
.getFirstParagraph());
builder.writeln(“After Section break new page”);
builder.moveToBookmark(“back”);
Section portrait = builder.getCurrentSection();
for (HeaderFooter headerFooter : hfCollection){
portrait.getHeadersFooters().add(headerFooter.deepClone(true));
}
for (Bookmark bookmark : doc.getRange().getBookmarks()) {
String bookmarkName = bookmark.getName();
String bookmarkValue = bookmark.getText();
System.out.println(bookmarkName + " - ‘“+bookmarkValue.trim()+”’ -");
bookmark.setText(“Replace Logo”);
}
builder.getDocument().save(“D:\Result.doc”);
Thanks in advance for your help.
Hi
Roseline,
Roseline:1) If I have a bookmark in the landscape ‘text box’, when I copy/paste the landscape section and I print the content of each bookmark, the bookmark has the ‘content’ of all the page (!).
Roseline:2) If I have to copy/paste many times the first section, what about the bookmark ? Because a bookmark has a unique name…
Hi Awais,
Thanks for your answer.
But for the question 1, I don’t understand.
I have just printed the content of a bookmark and I don’t understand why the content is the content of all the page and not only the content of the bookmark.
Have you test with the code I gave in the first message of the topic ? because the execution of the code will be more explicit than my message in english…
I’m sorry if I explain very badly my problem.
For the question 2, is there an option (like for copy two sections 'ImportFormatMode.KeepSourceFormatting) to rename the copied bookmark and concat the bookmark name with _0,_1… ?
Thanks in advance.
Hi Roseline,
Document doc = new Document(“C:\Temp\in.doc”);// Get section, which should be copied.
Section sect = doc.getSections().get(0);// Copy section few times
for (int i = 0; i < 5; i++)
{
// Clone section
Section clone = (Section)sect.deepClone(true);
// Insert cloned section after the original section.
sect.getParentNode().insertAfter(clone, sect);
}// Save ouytput document
doc.save(“C:\Temp\out.doc”);
int i = 0;
Section lastSection = doc.getSections().get(1);
for (Bookmark bm : lastSection.getRange().getBookmarks())
{
String name = bm.getName();
bm.setName(name + “_”+ i);
}
Hi,
Thanks for your answer.
For the first point, the matter is not that the section is not copied, but that the content of a bookmark in a section is not correct.
In the provided files, when I print the content of bookmark I have (The first bookmark is in the first page, and the second bookmark in the fourth page) :
For the first bookmark : Logo the content is 'logo’
For the second bookmark (the section that I have cloned), the bookmark name is Logo and the content of the bookmark is
'logo This is the Header in Landscape
After Section break new page
Landscape
Portrait’
which corresponding to the content of the page.
So, when I have to get the content of the bookmark to make some process, it is not work because the content is wrong.
Please find in attachment the result.doc , the expected result.doc and the .dot file. (The code is in the first message of the topic).
Thanks in advance.
Hi
Roseline,
Hi
Roseline,
Document dstDoc = new Document();
dstDoc.removeAllChildren();NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
Node node = importer.importNode(srcDoc.getFirstSection(), true);
dstDoc.appendChild(node);Section clone = (Section)node.deepClone(true);
dstDoc.appendChild(clone);dstDoc.save(“c:\test\outJava.doc”);
Hi,
Thanks for your answer.
My problem was the conflict of bookmark name.
If I rename the bookmark then there is no problem
Regards.
Hi Roseline,