Free Support Forum - aspose.com

Multiline CDP unlink

The code below is throwing an error when there's a multiline custom document property. What's wrong here? Thank you.

private static void UnlinkFields(Document doc)

  • {
  • ArrayList propertyStarts = new ArrayList();
  • NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
  • foreach (FieldStart start in starts)
  • {
  • if (start.FieldType == FieldType.FieldDocProperty)
  • {
  • propertyStarts.Add(start);
  • }
  • }
  • foreach (FieldStart start in propertyStarts)
  • {
  • Node currentNode = start;
  • Node fieldSeparator = null;
  • while (currentNode.NodeType != NodeType.FieldSeparator && currentNode.NodeType != NodeType.FieldEnd)
  • {
  • currentNode = currentNode.NextSibling;
  • currentNode.PreviousSibling.Remove();
  • }
  • if (currentNode.NodeType == NodeType.FieldSeparator)
  • {
  • fieldSeparator = currentNode;
  • while (currentNode.NodeType != NodeType.FieldEnd)
  • {
  • currentNode = currentNode.NextSibling;
  • }
  • fieldSeparator.Remove();
  • }
  • currentNode.Remove();
  • }
  • }

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Could you please attach your document here for testing? I will check the issue on my side and provide you more information.

Best regards.

Attached is the file.

It's the ~authorsignature property that sais "Peter Tsairis..." that is throwing an error.

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information, The problem occurs because there is field spanned into two paragraphs. You can resolve this issue by checking current node for null. Please try using the following code:

private static void UnlinkFields(Document doc)

{

ArrayList propertyStarts = new ArrayList();

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

foreach (FieldStart start in starts)

{

if (start.FieldType == FieldType.FieldDocProperty)

{

propertyStarts.Add(start);

}

}

foreach (FieldStart start in propertyStarts)

{

Node currentNode = start;

Node fieldSeparator = null;

while (currentNode.NodeType != NodeType.FieldSeparator && currentNode.NodeType != NodeType.FieldEnd)

{

currentNode = currentNode.NextSibling;

currentNode.PreviousSibling.Remove();

}

if (currentNode.NodeType == NodeType.FieldSeparator)

{

fieldSeparator = currentNode;

while (currentNode != null && currentNode.NodeType != NodeType.FieldEnd)

{

currentNode = currentNode.NextSibling;

}

fieldSeparator.Remove();

}

if (currentNode != null)

currentNode.Remove();

}

}

Hope this helps.

Best regards.

This project has been on hold for a while & now we would like to complete it. I’ve changed the code as above, however now the document that it produces is corrupted. See attached documents.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I think, in your case, you can use DocumentVisitor. Please follow the link to learn more

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/howto-extract-content-using-documentvisitor.html

You can find some code example here:

http://www.aspose.com/community/forums/222671/removing-ifs-from-resolved-document/showthread.aspx#222671

Just use FieldType.FieldDocProperty.

Best regards,

Is there any way to sometimes ignore the fields. We sometimes have fields without any field separator(see below) & these are getting deleted completely with the documentvisitor.

[!FieldStart!] MACROBUTTON nomacro F11[!FieldEnd!]

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. In this case, please try using the following code:

public class UnlinkDocPropFields : DocumentVisitor

{

private bool _delete = false;

public override VisitorAction VisitFieldStart(Aspose.Words.Fields.FieldStart fieldStart)

{

if (fieldStart.FieldType == Aspose.Words.Fields.FieldType.FieldDocProperty)

{

_delete = true;

fieldStart.Remove();

}

return VisitorAction.Continue;

}

public override VisitorAction VisitFieldSeparator(Aspose.Words.Fields.FieldSeparator fieldSeparator)

{

if (fieldSeparator.FieldType == Aspose.Words.Fields.FieldType.FieldDocProperty)

{

_delete = false;

fieldSeparator.Remove();

}

return VisitorAction.Continue;

}

public override VisitorAction VisitFieldEnd(Aspose.Words.Fields.FieldEnd fieldEnd)

{

if (fieldEnd.FieldType == Aspose.Words.Fields.FieldType.FieldDocProperty)

{

_delete = false;

fieldEnd.Remove();

}

return VisitorAction.Continue;

}

public override VisitorAction VisitRun(Run run)

{

if (_delete)

run.Remove();

return VisitorAction.Continue;

}

public override VisitorAction VisitTableStart(Table table)

{

if (_delete)

table.Remove();

return VisitorAction.Continue;

}

public override VisitorAction VisitParagraphStart(Paragraph paragraph)

{

if (_delete)

paragraph.Remove();

return VisitorAction.Continue;

}

}

Best regards,

Thank you. This seems to be working nicely.

Is there any way to unlink the following field, so that in the document it should just read "text" without being a macrobutton?

{MACROBUTTON NoMacro text}

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I think in this case you can use the same technique as described in my previous post, just change FieldType to FieldMacroButton.

Also, please follow the link to learn more about DocumentVisitor.

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/howto-extract-content-using-documentvisitor.html

Best regards,