I have a process where I make a Document along with a DocumentBuilder and generates a document. The process inserts data into some Word fields and removes some content (bookmarks/nodes) after processing. Finally, does a document.Save( stream, saveFormat ). If I save as word, all my ‘processing’ works (i.e. content I’ve removed, is still gone and all looks good), but if I instead save as Pdf (which is what I need) it’s as though the document puts the content back into the document.
I’ve attached a sample project that reproduces our problem. The attached is simplified code (and hard coded certain sections) as much as I could. In general, this is what ‘real code’ does following:
-
Open document and has a ‘data source’ for different data elements that’ll be inserted into document
-
Loop all sections
-
Loop all section.HeadersFooters.Range and insert ‘data’
-
Loop all section.Body.Range and insert ‘data’
-
Delete all bookmarks (and their content) flagged to delete
-
Save the document
To get around the problem, Aspose support (Awais Hafeez) suggested that before saving to Pdf I call document.UpdatePageLayout() but they said that should only be needed if I ‘saved’ the document once already which I hadn’t.
In creating the sample, I’ve determined that the ‘insert data’ part of my workflow that seems to be causing the problem. You’ll see in code, but essentially to ‘swap’ in the passed in data into Word field I essentially do this to a field:
f.Result = "Some value";
f.IsLocked = true;
I’m not sure why IsLocked needs to be set to true, but if I don’t set that, the bookmark processing works in the Pdf save (without the update layout call), but the data substitution doesn’t. If I leave IsLocked = true, data substitution works in Doc and Pdf, but Save only works in Doc while Pdf doesn’t.
In the sample, address2 is only bookmark I work on removing. So that you can find it easily in your document explorer, it is the paragraph found at doc.Sections[0].Body.ChildNodes[9]. If you open the document in Word, it is found immediately at the top as part of the ‘return address’.
So two questions:
a) Is my code that substitutes data corrent? (need field.IsLocked = true; and range.UpdateFields(); call?
b) Is it expected that setting field.IsLocked some how triggers Pdf export to require a call to UpdatePageLayout()?
Thanks in advance.