We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copying sections that contain bookmarks- then adjusting bookmark text?

Hi there,

I seem to be having difficulty copying a section that contains bookmarks, then utilizing the "text" property of the bookmarks (and probably other properties as well):

I have a document with an introductory section, a main section, then a closing section. I would like to use the main section in a loop (effectively copying the main section "X" times), which seems quite straightforward using the section.Clone(true) function.. The problem occurs with bookmarks which I access in each section (usually to delete the contents of a given bookmark) - I receive an exception that reads the "start and end node should have the same grand parent".

If I save out the document without touching the bookmarks I note that the bookmarks seem to stretch from the first section to the final section and only appear once (not once per section), which would seem to correspond with the error message. I saw a prior post which indicated that you could rename the bookmarks, but this doesn't seem to have any effect (and after renaming, and saving, the "renamed" bookmarks have disappeared!).

I do have a temporary workaround:

  1. Create a new temporary document in memory
  2. Import the section to your new document using the "Document.ImportNode" function
  3. Relabel all the bookmarks (I just append a number to the bookmarks' name, relatively to the loop/copy I'm on)
  4. Import the relabelled section from the temporary document back to the main document, again using the "Document.ImportNode" function.

While this works, it seems awfully ugly - am I missing something?

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Word document cannot contain few bookmarks (or formfields) with same name. Therefore, you should rename bookmarks during cloning the Section. This should work in your case.

Could you please provide me your document for testing and code? I will investigate the issue and provide you more information.

Best regards.

Hi Alexy - below is some sample code that causes the issue - I've attached a very basic word document that demonstrates the issue (Word 2000).

string sFileName = @"sample.doc";
Document doc = new Document(sFileName);
Section lastSection = doc.Sections[1]; // the section to clone

for (int i = 1; i < 5; i++)
{
Section newSection = (Section)lastSection.Clone(true); // clone the section
foreach (Bookmark bm in newSection.Range.Bookmarks)
bm.Name += i.ToString(); // relabel the bookmark to get unique bookmarks throughout
doc.InsertAfter(newSection, lastSection); // insert into our document after the last section.
lastSection = newSection; // re-reference the last section for our next insert
}

doc.Save(sFileName + Guid.NewGuid().ToString() + ".doc"); // for testing - will show the "lack" of bookmarks in MSWord

for (int i = 1; i < 6; i++) // loop through again
{
Section mySection = doc.Sections[i]; // grab a section
foreach (Bookmark bm in mySection.Range.Bookmarks)
bm.Text = bm.Text + i.ToString(); // <<<<try to modify a bookmark! Error!
}

doc.Save(sFileName + Guid.NewGuid().ToString() + ".doc"); // for testing

Thanks for the quick reply!

Brendon

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. You should first insert section into the document and afterward rename bookmarks in section. See code below:

Document doc = new Document(@"Test102\in.doc");

Section lastSection = doc.Sections[1]; // the section to clone

for (int i = 1; i < 5; i++)

{

Section newSection = (Section)lastSection.Clone(true); // clone the section

doc.InsertAfter(newSection, lastSection); // insert into our document after the last section.

foreach (Bookmark bm in lastSection.Range.Bookmarks)

{

bm.Name += i.ToString(); // relabel the bookmark to get unique bookmarks throughout

}

lastSection = newSection; // re-reference the last section for our next insert

}

doc.Save(@"Test102\out1.doc"); // for testing - will show the "lack" of bookmarks in MSWord

for (int i = 1; i < 6; i++) // loop through again

{

Section mySection = doc.Sections[i]; // grab a section

foreach (Bookmark bm in mySection.Range.Bookmarks)

bm.Text = bm.Text + i.ToString(); // <<<<try to modify a bookmark! Error!

}

doc.Save(@"Test102\out.doc"); // for testing

Best regards.

Hi Alexey - perhaps I should have mentioned that I've already tried that route (and just tried again now with the sample document), and receive the same error: "start and end node should have the same grand parent". While the error reads the same, I suppose it could be a different issue in each case, merely reporting the same error...

[edit!] I do note that you are changing the "lastSection", not the "new" section that has just been cloned... I'll give his a try as well [/edit!]

Any more thoughts?

Brendon

Ok - just tried replacing "newSection" for "lastSection" like you did above - works perfectly. I also tried inserting the "new" section either before or after relabelling the bookmarks - it doesn't seem to affect the overall execution at all. So, your example works correctly , but due to the the line below your highlighted line!

In any event, I can work with this - I'll just re-arrange my logic a bit to account for which section is actually being relabelled (in the end, it may not matter at all).

Thanks a bunch - should be enough for me to purchase a license!

Brendon

Hi Brendon,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

It is nice that you have found solution. Please let me know in case of any issues, I will be glad to help you.

Best regards.