[SOLVED] Manage bookmark in text box

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,


Thanks for your inquiry and sorry for the delayed response.
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 (!).
Sure, you can extract contents from inside a particular Section into a separate document, obtain and move to a particular Bookmark and insert that intermediate document at the place where your Bookmark resides. Moreover, you can also try using the code suggested in the following link to control how header/footer appears during merging.
Roseline:
2) If I have to copy/paste many times the first section, what about the bookmark ? Because a bookmark has a unique name…
It is by MS WORD design that a Word document must have unique names for bookmarks. You can not insert two bookmarks with the same names in the document. In your case, the Bookmark you copied will be lost during saving.

Please let me know if I can be of any further assistance.

Best Regards,

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,


Thanks for your inquiry.

1) Please attach your output Word document here for testing? I will investigate as to how you are expecting your output document to be generated like. You can MS WORD to create your target Word document. I will then provide you code to achieve what you’re looking for.

Moreover, may be you can use code like the following to copy/paste the particular section:

// Open document.
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”);


2) I am afraid, there is no such option available in Aspose.Words. Please try renaming Bookmarks in a Section before saving the document. For example:
int i = 0;
Section lastSection = doc.getSections().get(1);
for (Bookmark bm : lastSection.getRange().getBookmarks())
{
String name = bm.getName();
bm.setName(name + “_”+ i);
}
I hope, this will help.

Best Regards,

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,


Thanks for the additional information. We’re working over your most recent queries and will get back to you shortly.

Best Regards,

Hi
Roseline,


Thanks for your patience. Please see the following code; I have imported the first section of ‘doc3.dot’ document into another temporary Document and then cloned this section again. The first section contains a Bookmark named ‘logo’ but when duplicated this Bookmark is lost in ‘out.doc’. Although, the duplicated Bookmark is lost, but please notice the content inside the Bookmark is still preserved.

Document srcDoc = new Document(“c:\test\doc3.dot”);
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”);

Best Regards,

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,


Thanks for your inquiry. It is perfect that you managed to achieve what you were looking for. Please let us know any time you have any further queries.

Best Regards,