Hello all,
We’re using Aspose.Words to generate word document and right now we’re generating a word document with ‘n’ number of pages and now we got a requirement where we need to swap a particular page with another page. My question here is, what is the best way to find a particular section in the Aspose document tree ? And is there a way where we can tag the section node, so that we can easily find and replace that particular node during runtime. Please share your thoughts on this.
@Jagan2021 There is no page concept in MS Word documents, since they are flow documents by their nature. Consumer applications layout the document into pages on the fly. Section on MS Word document can contain any content and can ocuppy multiple pages. Please see our documentation to learn about Document Object model:
https://docs.aspose.com/words/net/aspose-words-document-object-model/
If in your document each page is represented as a separate section, you can simply remove the required section by index and then insert another section using InsertAfter or InsertBefore methods. If the section comes from another document, you should first import the node.
Also, you can use Document.ExtractPages method to extract content before the page that should be replaced, then use this method to extract content after the page and then concatenate the first document, the replacement page document and the second extracted documents using Document.AppenDocument method.
@alexey.noskov Thanks for the reply. Just one more follow-up question, While generating a document with ‘n’ number of sections. Is there a way where we can tag that section (like some attributes/flag), so that we can identify the section later and replace with the new section on the fly.
To give more info, we’re converting html to document object with the help of Aspose, and on the other end we’re using Aspose.words for .NET to generate another document object. And now requirement is to combine and to replace a particular section with the new section.
@Jagan2021 You can put a bookmarks in the sections and identify sections by the bookmark name. For example see the following code:
Document doc = new Document(@"C:\Temp\in.docx");
// Get section by bookmark.
Section sect = (Section)doc.Range.Bookmarks["section_name"].BookmarkStart.GetAncestor(NodeType.Section);
Hello @alexey.noskov. Thanks for your suggestion. I’m trying to add a run text in a paragraph (within section[i].body) and retrieved the section by identifying the tag from run text (firstchild of body). Not sure this is the correct way, But please let me know if we have any other better way to tag and identify the section ?
@Jagan2021 You can use use Node.CustomNodeId to identify nodes in the document. But please note, specified value is not saved to an output file and exists only during the node lifetime.
@alexey.noskov This seems to be interesting. Since we’re using html to document conversion, How do we set the customNodeId in html writer ?
@Jagan2021 Unfortunately, no. Node.CustomNodeId
value is not saved to an output file and exists only during the node lifetime.
Hello @alexey.noskov, Tried using CustomNodeID like you mentioned and I understood its life time. Just need some confirmation, Tried to send the document object as Byte[] through API endpoint and noticed CustomNodeID value is not preserved. Guess it is intentional, like you replied in the previous thread. Please confirm my understanding.
Even noticed CustomDocumentProperties which I’ve added, for that too count became 0. Not sure why it is not getting preserved.
And do you have any other solution to add custom value to identify a section in the Document object ? If so please guide me on this.
@Jagan2021 Yes, the behavior is intentional. When Document
object is saved and then reopened CustomNodeID
is not preserved. Unfortunately, there is no way to set custom tags to nodes, which will be preserved after save/open operation.
Thanks for the reply. Even CustomDocumentProperties array too getting cleared. Is it intentional too ?
And do we have any other way to tackle this requirement ?
@Jagan2021 CustomDocumentProperties
are preserved, but they are assigned to whole document, not to individual nodes.
In the above image, before converting to bytes, I could see CustomDocumentProperties
are preserved
And in the next image, it shows values where it disappeared when I convert the document object to bytes and converting back to new Document()
.
Here value didn’t preserved. Am I missing something here ?
Attaching one more image which will explain how am converting Document to bytes to memory stream and again back to Document.
@Jagan2021 Could you please attach your input document here for testing and specify in which format it is saved and then reloaded? We will check the issue and provide you more information.
Attaching a sample application, Here you can find both CustomNodeID for a section and CustomDocumentProperties both not getting preserved. Please take a look and let me know if you have any suggestions to it. AsposeCustomSample.zip (84.2 KB)
@Jagan2021 CustomNodeID
is not preserved upon document roundtrip, it is expected. Regarding CustomDocumentProperties
, please use some native format for document roundtrip, like DOC, DOCX etc. PDF format is not a best choice for document roundtrip, since Aspose.Words has limited support of PDF format on document import.
@alexey.noskov Thank you. This helps ! Do you have any plan in the Aspose roadmap to preserve CustomNodeID to get saved to the output file in the future ?
@Jagan2021 Unfortunately, Aspose.Words is limited to the features supported by file formats. Currently there is no way to assign custom tags to the nodes in MS word document formats. So there is simply no technical possibility to implement this feature.