CustomDocumentProperties - How to Delete


#1

First wanted to say I that from the short time (trial currently) I have been using Aspose.Word I think its awsome. Currently we use Word automation and that's just not reliable. I am hoping Aspose.Word will eliminate that problem.

Ok so basically I am looking to count the characters in different sections of a document. Depending on our clients needs we sometimes count CustomDocumentProperties as part of the total character count and other times we ignore them. I know I can iterate through the DocumentProperties and count the characters but if I don't what to include the DocumentProperties as characters how can I remove them from the document prior to counting characters. (Reason I need to remove them first is because at the end of the day we are converting characters into lines so the placement of the CustomDocumentProperties plays a roll in line counting)

This is the code I tried to remove the CustomDocumentProperties...

While myDoc.CustomDocumentProperties.count > 0
Dim custDocName as string = myDoc.CustomDocumentProperties(0).Name
myDoc.CustomDocumentProperties.Remove(custDocName)
End While


This seems to remove them correctly (CustomDocumentProperties.count = 0) but when I look at the document sections after as raw text (say sections.GetText() ) I still see the Custom Document Properties. What am I doing wrong ?

Thanks in advance
-Michael


#2

Hi Michael,

Thank you for considering Aspose.

I’m not sure how custom document properties and text contained in the document sections are related. How can you see them there as you state? Could you please post more code and probably the document itself?


#3

The aspose api is very new to me so maybe I am doing something wrong but why doesn't this work. Basically I want to iterate through the document and count characters but I don't want to count the "special" characters Word inserts.


Private Sub Blah()

Dim myDoc As New Aspose.Word.Document("c:\Docs\sample.doc")

Dim myselection As Aspose.Word.Section

'See all text in Document. Correct me if I am wrong but the

'custom document properties are the DOCPROPERTY "XXX" \* MERGEFORMAT Sections

Dim allText As String = myDoc.GetText

While myDoc.CustomDocumentProperties.Count > 0

Dim docName As String = myDoc.CustomDocumentProperties(0).Name

myDoc.CustomDocumentProperties.Remove(docName)

End While

'All Document Properties removed but why does the myDoc.GetText

'still show the DOCPROPERTY "XXX" \* MERGEFORMAT sections

allText = myDoc.GetText
End Sub


#4

Any update on this issue? Am I doing something wrong? Having the ability to strip out the custom Document Properties is part of our requirement and will allow me to determine if Aspose.Word will suite our needs. Thanks in advance


#5

Hi,

Sorry for delay.

The point is that removing custom properties from the document does not affect DOCPROPERTY fields located there. You should remove them explicitly. Use the following code to accomplish this:

NodeCollection fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true);

//Copy from live collection to a static array, otherwise deleting from a live collection is too tricky.
FieldStart[] array = new FieldStart[fieldStarts.Count];

for (int i = 0; i < fieldStarts.Count; i++)
arrayIdea = fieldStartsIdea as FieldStart;

foreach (FieldStart fieldStart in array)
{
if (fieldStart.FieldType == FieldType.FieldDocProperty)
{
Node node, nextNode;
for (node = fieldStart; node.NodeType != NodeType.FieldEnd; node =
nextNode)
{
nextNode = node.NextSibling;
node.Remove();
}
node.Remove();
}
}