Remove header content from a specific page

Hi Team,

We need to have a logic, consider a document with the first page header different than the other page headers. Here there needs to be a logic to remove the header from page 3 or Page 4 alone. As per my understanding this can’t be done in Word document (correct me if I am wrong). Please let me know if this can be achieved with Aspose word. Attached the sample document which contains 3 page and the header from page 3 alone has to be removed.

Input.docx (45.2 KB)

Thanks,
Karthikeyan

@Karthik_Test_account In MS Word document headers/footers are defined per section. Each section in MS Word document can have 3 types of header and footer - First , Primary and Even pages. In your case it is required to split document by pages, you can achieve this using Document.ExtractPages, then remove headers/footer from the specific pages and finally rejoin the document. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");

// Use empty clone of the original document as a result document.
Document result = (Document)doc.Clone(false);

// Extract the first part of the document 
result.AppendDocument(doc.ExtractPages(0, 1), ImportFormatMode.UseDestinationStyles);

// Extract page where headers/footer must be removed.
Document page = doc.ExtractPages(1, 1);
foreach (Section s in page.Sections)
{
    s.HeadersFooters.Clear();
    s.HeadersFooters.LinkToPrevious(false);
}
result.AppendDocument(page, ImportFormatMode.UseDestinationStyles);

// Append the remaining document content.
result.AppendDocument(doc.ExtractPages(2, doc.PageCount - 2), ImportFormatMode.UseDestinationStyles);

result.Save(@"C:\Temp\out.docx");

@alexey.noskov - This is working. I have two more questions.

  1. In the Realtime project we have an image in the header which is the logo can we customize this code to remove only the text in the header and don’t disturb the image
  2. Currently the method clears both header and footer, is there a way to remove only the header, leaving the footer undisturbed.

@Karthik_Test_account

  1. Images in MS Word documents are represented as Shape nodes and text is represented as Run nodes. So to remove text and leave the image you can remove Run nodes from the header. For example see the following code:
foreach (Section s in page.Sections)
{
    // Remove text only from the headers
    foreach (HeaderFooter hf in s.HeadersFooters)
    {
        if (hf.IsHeader)
            hf.GetChildNodes(NodeType.Run, true).Clear();
    }
    s.HeadersFooters.LinkToPrevious(HeaderFooterType.HeaderPrimary, false);
    s.HeadersFooters.LinkToPrevious(HeaderFooterType.HeaderFirst, false);
    s.HeadersFooters.LinkToPrevious(HeaderFooterType.HeaderEven, false);
}
  1. Sure, you can remove only headers:
foreach (Section s in page.Sections)
{
    // Remove only headers
    foreach (HeaderFooter hf in s.HeadersFooters)
    {
        if (hf.IsHeader)
            hf.Remove();
    }
    s.HeadersFooters.LinkToPrevious(HeaderFooterType.HeaderPrimary, false);
    s.HeadersFooters.LinkToPrevious(HeaderFooterType.HeaderFirst, false);
    s.HeadersFooters.LinkToPrevious(HeaderFooterType.HeaderEven, false);
}

@alexey.noskov - Thanks for the previous reply. One more question is there a way to remove a non blank page from the document with its index ?

If not index is there any other way ? Please share the methodologies

@Karthik_Test_account The similar approach can be used to remove page by index, i.e. split and rejoin document by pages. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
// Clone the document.
Document result = (Document)doc.Clone(false);

// Zero-based index of the page that should be removed
int pageToRemoveIndex = 3;
// Get pages before the page that should be removed.
result.AppendDocument(doc.ExtractPages(0, pageToRemoveIndex), ImportFormatMode.UseDestinationStyles);
// Get pages after the page that should be removed.
result.AppendDocument(doc.ExtractPages(pageToRemoveIndex + 1, doc.PageCount - (pageToRemoveIndex + 1)), ImportFormatMode.UseDestinationStyles);

result.Save(@"C:\temp\out.docx");