Updating bookmarks in included documents

Hi,

I have a problem with updating bookmarks in included documents. When I attached debugger I’ve noticed that they are updating, but when Update() on field is called bookmark value is restored to the original value. I use similar code to this:

var documentPath = "C:\\Aspose\\";
var document = new Document(documentPath + "IncludeText.docx");
foreach (var includeText in document.Range.Fields.Cast().Where(field => field.Type.Equals(FieldType.FieldIncludeText)).Cast())
{
    includeText.Update();
}
document.Range.Bookmarks["TestBookmark"].Text = "0";
document.Range.Bookmarks["InnerBookmark"].Text = "Test";
document.Range.Fields.Cast().Where(f => f.Type != FieldType.FieldIncludeText).ToList().ForEach(f => f.Update());
document.Save(documentPath + "Output.docx");

I’ve attached documents and output. Could you please write me how it should be done properly? I’m not calling UpdateFields() on whole document because I need to change incudeText documents paths and UpdateFields() was causing documents to update incorrectly.

Hi Agnieszka,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you modify the text of InnerBookmark inside IncludeText.docx using MS Word, you will get the same output after updating the fields.

Please let us know if you have any more queries.

Hi Tahir,

I have two documents both with bookmarks, I need to update path in includetext field and then update bookmarks in both documents. How can I accomplish this? It is working fine if includetext field is not inside “if” field. I checked if I can do the same in MS Word, but when I press Shift+Ctrl+F9 I get the document without bookmarks (just the text) which is different than what I can do with Aspose Word. I’m clearly not losing bookmarks after updating include text field by aspose, which is better than what I got with MS word. The problem is that we could do this kind of things with Word Interop which we used before, so we need to support it now.

Many thanks

Hi Agnieszka,

Thanks for your inquiry. Please use FieldIncludeText.SourceFullName property to get or set the location of the document using an IRI.

Please load the included document into separate Aspose.Words.Document and update the bookmark’s text. Please use following code example to get the desired output.

var document = new Document(MyDir + "IncludeText.docx");
document.Range.Bookmarks["TestBookmark"].Text = "0";
foreach (FieldIncludeText includeText in document.Range.Fields.Cast<Field>().Where(field => field.Type.Equals(FieldType.FieldIncludeText)).Cast<FieldIncludeText>())
{
    // update path in includetext field
    includeText.SourceFullName = MyDir + "InnerWithBookmarkTest.docx";
    includeText.Update();
    // Update the text of included text document
    var docInclude = new Document(includeText.SourceFullName);
    docInclude.Range.Bookmarks["InnerBookmark"].Text = "New Test";
    docInclude.Save(includeText.SourceFullName);
}
document.Range.Fields.Cast<Field>().Where(f => f.Type != FieldType.FieldIncludeText).ToList().ForEach(f => f.Update());
document.Save(MyDir + "Output.docx");

Hi Tahir,

thank you for your response. Your solution seems to me a little bit like a workaround. I cannot update included document in its original location because it is where we kept all templates, we don’t want to change them. I could copy it to temp location, change path and update document, but it doesn’t seem to me like a proper way. Is there any other way to insert one document to the other? I don’t necessarily need to update a path and update bookmarks, if I could insert one document to the other and then work on main document as a whole it would be perfectly fine.

Many thanks

Hi Agnieszka,

Thanks for your inquiry. Yes, you can insert one document into another at any location. Please move the cursor to the desired location of input document and insert contents of other document using DocumentBuilder.InsertDocument method. Hope this helps you.

Please check following code example for your kind reference.

var document = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(document);
builder.MoveToDocumentEnd();
var includeDoc = new Document(MyDir + "second document.docx");
builder.InsertDocument(includeDoc, ImportFormatMode.KeepSourceFormatting);
document.Save(MyDir + "Out.docx");