This project has moved and is read-only. For the latest updates, please go here.

Replacing Text with BackSpace character

May 12, 2014 at 10:46 PM
I am trying to replace a line in a template with the backspace character. (Ultimately - I want the entire line deleted). Any one has any ideas on how to do this?
This is the code I tried using:

document.ReplaceText("the text", "\b");

However, I am getting the error:


'', hexadecimal value 0x08, is an invalid character.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: '', hexadecimal value 0x08, is an invalid character.

[ArgumentException: '', hexadecimal value 0x08, is an invalid character.]
System.Xml.XmlEncodedRawTextWriter.InvalidXmlChar(Int32 ch, Char* pDst, Boolean entitize) +2349658
System.Xml.XmlEncodedRawTextWriter.WriteElementTextBlock(Char* pSrc, Char* pSrcEnd) +478
System.Xml.XmlEncodedRawTextWriter.WriteString(String text) +82
System.Xml.XmlEncodedRawTextWriterIndent.WriteString(String text) +11
System.Xml.XmlWellFormedWriter.WriteString(String text) +86
System.Xml.Linq.ElementWriter.WriteElement(XElement e) +131
System.Xml.Linq.XElement.WriteTo(XmlWriter writer) +72
System.Xml.Linq.XContainer.WriteContentTo(XmlWriter writer) +211
System.Xml.Linq.XDocument.WriteTo(XmlWriter writer) +120
System.Xml.Linq.XDocument.Save(TextWriter textWriter, SaveOptions options) +60
Novacode.DocX.Save() ........
May 13, 2014 at 10:27 AM
If you want the entire line removed, I assume the line is placed in its own paragraph?
Perhaps this is a case of using a sledgehammer to crack a nut but if that's the case, this might work for you:
foreach (Paragraph par in document.Paragraphs)
{               
    if (par.Text.Contains("the text"))
    {
        par.Remove(false);
    }
}
Marked as answer by UhdUser on 5/13/2014 at 4:13 AM
May 13, 2014 at 12:14 PM
Thank you Annika89! Your solution did help me achieve what I wanted to.