Free Support Forum - aspose.com

Need help removing specific Paragraphs

Hi.
I need som code help for removing some specific paragraphs.

I attatched a Document for you to help clarify what i mean.
At page four and five i got some unwanted paragraphs at the top of the page(just below the header).
Is there any way to get rid of theese because they are destroying my layout?

I have tried :

foreach (Section srcSection in doc.Sections)
{
// loop through all block level nodes (paragraphs and tables) in the body of the section.
foreach (Node srcNode in srcSection.Body)
{
// remove node if it is the last empty paragarph in the section.
Paragraph para = srcNode as Paragraph;
if ((para != null) && para.IsEndOfSection && !para.HasChildNodes)
{
para.Remove();
brepeat = true;
}
}
}



This doesnt work for me. it seems theese paragraph still got som childnodes or something.
Regaards
/Joel

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

Thanks for your inquiry. There is no section breaks between page 3 and 4, and between 4 and 5. There are just PageBreaks. I think that you should replace PageBreaks with SectionBreaks and then remove empty paragraphs at the beginning of each section. For example you can try using the following code:

Document doc = new Document(@"Test048\in.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

//Replace PageBreaks with Section breaks

foreach (Paragraph par in doc.GetChildNodes(NodeType.Paragraph, true, false))

{

foreach (Run run in par.Runs)

{

//If run contains PageBreak then remove it and insert section break

if (run.Text.Contains("\f"))

{

builder.MoveTo(run);

builder.InsertBreak(BreakType.SectionBreakNewPage);

run.Remove();

break;

}

}

}

//Remove empty paragraphs from the begining of each section

foreach (Section srcSection in doc.Sections)

{

while (srcSection.Body.FirstParagraph != null)

{

if (!srcSection.Body.FirstParagraph.HasChildNodes)

srcSection.Body.FirstParagraph.Remove();

else

break;

}

}

doc.Save(@"Test048\out.doc");

I hope this could help you.

You can also open your document using DocumentBuilder (Aspose.Words demo application) to view structure of the document.

Best regards.

Thank you so much.
The demo sure helps alot.
As you already known im quite new to both aspose and the structure of word documents.

The code was very helpful to.

Surely some more questions will pop up later but ill return if so.

Regaards
/Joel