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

Free Support Forum - aspose.com

Aspose Word Document.Save removes bookmarks

Attached Word document is loaded into Aspose Document object.

Add a bookmark to surround/encapsulate the document.

Prior to the .Save, the bookmark count is correct at 1. After the .Save, the bookmark count is 0 and the inserted bookmark is no longer in the document or in the datastream output from the .Save.

We use the bookmarks when a user saves the document back into our system to identify the one to many pieces of content being returned. So, without the bookmark we cannot identify what is being returned to our system. This breaks our entire content editing process/model.

This does not happen all the time. So, there must be something unique about the attached document that the Aspose Word engine is detecting or failing on and inadvertently removing the bookmark during the .Save operation.

This is very time-critical since we are announcing GA release of this release of our product this coming week.

Please advise ASAP.

Following is the code.....

Dim BLOBDoc As Document
BLOBDoc = New Document("C:\\Tmp\\Share\\BookMarksDeleted.doc")

Dim blobBuilder As New DocumentBuilder(BLOBDoc)
blobBuilder.MoveToDocumentStart()
blobBuilder.StartBookmark("ContentExport_" & contentItem.ContentID)
blobBuilder.MoveToDocumentEnd()
blobBuilder.EndBookmark("ContentExport_" & contentItem.ContentID)

Dim preSaveBookmarks As Integer = BLOBDoc.Range.Bookmarks.Count

Using dataStream As New System.IO.MemoryStream
BLOBDoc.Save(dataStream, SaveFormat.Doc)
End Using

Dim postSaveBookmarks As Integer = BLOBDoc.Range.Bookmarks.Count

Hi William,

Thanks for your inquiry.

I managed to reproduce the issue on my side. Your request has been linked to the appropriate issue and you will be informed as soon as a fix is available.

In the mean time you can work around this by using the code below. You need to use this line in place of MoveToDocumentEnd().

blobBuilder.MoveTo(BLOBdoc.LastSection.Body.LastParagraph.LastChild);

The reason this is occuring is because the end portion of the bookmark is being placed inside a Footnote.

Thanks,

Thanks for the quick turnaround. Using the code you provided does solve the issue for the original document I attached. If I then use this same code for another similar document, none of the content or the bookmark comes through.

I am attaching another document that is very similar but when your line of code is used instead of the MoveToDocumentEnd, I end up with no content at all in the output document. In my actual production code, we are using the BLOBDoc once the bookmarks are added in an AppendDocument call as follows:

DestinationDoc.AppendDocument(BLOBDoc, ImportFormatMode.KeepSourceFormatting)

The resulting contents of DestinationDoc is basically nothing/empty. If I restore my code to use the MoveToDocumentEnd, it works for this attached doc but not for the original that your line of code fixes.

So, is there code we can use to know when to use your MoveTo versus the MoveToDocumentEnd based on the BLOBDoc's property differences between the attached document in the original post and the document attached to this post?

Hello,

Thank you for your request.
I have reviewed your file. At the end of your document in the last section is SectionBreake. I think all you need is to check whether there are child nodes in the last section.

I used the following code. and document test.doc have all content of the original document with the embedded bookmarks.

License license = new License();<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

license.SetLicense("Aspose.Words.lic");

Document doc = new Document("E:\\Firm+Description.doc");

Document doc2 = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToDocumentStart();

builder.StartBookmark("bookmark1");

if (doc.LastSection.Body.LastParagraph.HasChildNodes)

builder.MoveTo(doc.LastSection.Body.LastParagraph.LastChild);

else

builder.MoveTo(doc.LastSection.Body.LastParagraph);

builder.EndBookmark("bookmark1");

Console.WriteLine(doc.Range.Bookmarks.Count);

doc2.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);

doc2.Save("E:\\test.doc", SaveFormat.Doc);

Console.WriteLine(doc.Range.Bookmarks.Count);

Console.Read();

Could you please try it and write the result. Or attach your out document here for testing.

Hope this helps.

Hi William,

Thanks for this additional information.

I think you can remove the .LastChild part of the above line and it should work as expected.

Instructing the builder to move to the LastParagraph will move the cursor to the last inline node anyway (if there is one) and a body in a document should have at least one paragraph so this code should work in all situations while avoiding the original issue.

Thanks,

So far, the code you suggested is working for various content files. Continuing to test but it seems to have fixed the issue. Thanks!

Continuing to test now after removing the ".LastChild" as suggested and so far, so good.

Quick question though, why doesn't the MoveToDocumentEnd effectively do what we are forcing with the MoveTo method? Is that a bug with MoveToDocumentEnd? Do you plan to "fix" MoveToDocumentEnd to handle these data scenarios?

Hi William,

Thanks for your inquiry.

Yes this is a bug with MoveToDocumentEnd, the issue linked to this thread is to fix this. You will be informed as soon as the issue is resolved.

As I described above the behaviour is occuring because MoveToDocumentEnd is incorrectly moving inside a Footnote node which happens to be at the end of the document. A Footnote can contain inline children as well which is why the bookmark end is able to be inserted in there. However a bookmark whose range spans from the start of the body to inside a footnote invalid and is removed during save.

Using MoveTo method will instruct the builder to move to the last direct inline child of the paragraph and will not move within the Footnote node thus avoiding this issue while still covering the document with a bookmark.

The same functionality will be implemented with MoveToDocumentEnd to fix this issue.

Thanks,

The issues you have found earlier (filed as WORDSNET-4487) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.