Is there any way to add persistent metadata to a paragraph?

Is there anything I can do in Aspose that would work essentially the same as adding a CustomNodeId, but will persist in the document?

What I’d like to achieve is the following:

  1. Iterate through the document and add a unique ID to each paragraph (invisible to end users of the document in Word and cannot be deleted normally)
  2. Store the unique ID in an external database
  3. Later, fetch the ID from the database, open the document with Aspose.Words again and use the ID to locate the relevant paragraph in the document

We can’t use just the paragraph / node index, because the document will be edited by end users between steps 2 and 3 above.

@ssmolkin1

Aspose.Words does not provide APIs to preserve unique ID to each paragraph. However, you can bookmark the paragraphs and access them via bookmark.

Could you please share your sample input and expected output Word documents? We will then provide you more information about your query.

Thanks, I think that answers my question – essentially there’s no direct way to do this, but could try a bookmark as a workaround.

Yes, you can achieve this by adding custom XML data to each paragraph using Aspose.Words. This XML data can contain the unique ID, which you can then store in an external database. Later, you can retrieve the ID from the database, open the document with Aspose.Words again, and use the XML data to locate the relevant paragraph.

@drdavidmarco You are right, you can use Aspose.Words to add SmarTag and set custom properties of the node surrounded by this smart tag.

A post was merged into an existing topic: 怎么查询docx表格内容以及合并规则

@alexey.noskov what is the simplest way to surround an existing paragraph node within a SmartTag? Is it to do the following?

  1. Iterate through each Paragraph node
  2. At each Paragraph, insert a SmartTag node before the paragraph using SmartTag.InsertBefore
  3. Remove the Paragraph from its current parent node
  4. Append the removed Paragraph to the SmartNode.AppendChild

Is that correct, or is there a better / faster way?

@ssmolkin1 You can achieve this using code like the following:

Document doc = new Document(@"C:\Temp\in.docx");

int index = 0;
foreach (Paragraph p in doc.GetChildNodes(NodeType.Paragraph, true))
{
    // Create smart tag and add some properties.
    SmartTag tag = new SmartTag(doc);
    tag.Properties.Add(new CustomXmlProperty("id", string.Empty, $"para_{index}"));
    // Set the smart tag's URI to the default value.
    tag.Uri = "urn:schemas-microsoft-com:office:smarttags";
    tag.Element = "text";

    // Move all content from the paragraph into the smart tag
    while (p.HasChildNodes)
        tag.AppendChild(p.FirstChild);

    // Insert smart tag into the paragraph.
    p.AppendChild(tag);

    index++;
}

doc.Save(@"C:\Temp\out.docx");

Very helpful, thank you!

1 Like