Cover Page

Hi

is it possible to remove all text and paragraphs from a document except those that are in the header/footer and cover page? I cant seem to find out whether a paragraph is in one of these sections.

I want to set styles in a document, with the option to type a little bit of text in each style so that the user can see what the style looks like before they use this as a “style sheet”. But when this is prepended to other documents, I would like to remove the “test paragraphs”.

Any ideas?

Cheers

Ian

Hi Ian,

Thanks for your inquiry. Please use the following code example to remove the contents of document’s body and keep the header/footer contents.

Document doc = new Document(MyDir + "in.docx");
foreach (Section section in doc.Sections)
{
    section.Body.RemoveAllChildren();
    section.EnsureMinimum();
}
doc.Save(MyDir + "Out.docx");

Could you please attach your input and expected output Word document here for our reference? Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

Hi

Please find attached the Branding.docx and BrandingAfter.docx files. I am just trying to remove the content of the letter. They may put a bit of text in this template to show what it might look like.

Cheers

Ian

Hi

should have said, that the following code seems to do the right thing, but could you confirm that this is the best approach?

Cheers

Ian

foreach (Section section in headerDocument.Sections)
{
    foreach (Node node in section.ChildNodes)
    {
        if (node.NodeType != NodeType.HeaderFooter)
        {
            Body bdy = node as Body;
            foreach (Node bdynode in bdy.ChildNodes)
            {
                if (bdynode.NodeType == NodeType.Paragraph)
                {
                    deleteThese.Add(bdynode);
                }
            }
        }
    }
}

foreach (Node node in deleteThese)
{
    node.Remove();
}

Hi Ian,

Thanks
for sharing the detail. Please use the following code example to achieve your requirements. This code example deletes the contents of document after first page. Hope this helps you.

Document doc = new Document(MyDir + "Branding.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
doc.UpdatePageLayout();
LayoutCollector lc = new LayoutCollector(doc);
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    // Move the cursor to the second page
    if (lc.GetStartPageIndex(para) == 2)
    {
        builder.MoveTo(para);
        para.RemoveAllChildren();
        builder.Writeln();
        // Insert section break at second page
        builder.InsertBreak(BreakType.SectionBreakContinuous);
        // Remove remaining contents of document
        Section currentSection = builder.CurrentSection;
        while (currentSection != doc.LastSection)
        {
            Section nextSection = (Section)currentSection.NextSibling;
            currentSection.Body.RemoveAllChildren();
            currentSection.EnsureMinimum();
            currentSection = nextSection;
        }
        doc.LastSection.Body.RemoveAllChildren();
        doc.LastSection.EnsureMinimum();
        break;
    }
}
doc.Save(MyDir + "Out.docx");