Deleting Text from a Document


#1

I have a template document that has multiple lines that can be replaced with text. On occassion, the text that is being replaced might be blank, in that case, I want to delete the whole line, so I do not have extra space in my document.


In my template I have:

{REPLACE1}
{REPLACE2}
{REPLACE3}

Lets say {REPLACE1} and {REPLACE3} get replaced with valid text, but {REPLACE2} is nothing, how can I delete the line that replace 2 is on?

I can trap the fact that {REPLACE2} is nothing, but what command do I need to issue against the document to tell it to remove the whole line? I have tried replacing {REPLACE2} with chr(8), but I get an error indicating I cannot use control characters.

Is there a way to delete this line if I don’t need it?

Thanks,

John


#2

Hi,

Thank you for considering Aspose.

So is the text to replace located in different paragraphs or just in different lines? Please attach the template for better understanding.


#3

I would be 90% confident saying they are separate paragraphs, as the user who is creating the template probably hit the enter key after entering each replace value.


#4

I realize this is not the best workaround but this is all I’ve come up with so far. Try to remove all empty paragraphs after replacing:

doc.Range.Replace("{TEST3}", string.Empty, false, false);

NodeList paras = doc.SelectNodes("//Paragraph");

foreach (Paragraph para in paras)
{
if (para.GetText() == ControlChar.ParagraphBreak)
para.Remove();
}