Free Support Forum - aspose.com

Header Footer For documents with landscape and portrait layout pages

Need to add header and footer for documents with landscape and portrait layout pages.

@manuR,

Aspose.Words, Aspose.Cells, Aspose.PDF and Aspose.Slides supports inserting header/footer functionality. Please explain a bit more about your requirement that on which type of document are you trying to insert the header/footer along with landscape and portrait layout setting?

Actually if i have a document with pages includes both landscape and portrait, how can i give headers and footers with respect to the layout.

@manuR,

Sorry for delay. We are looking into it and will update you very soon.

@manuR,

Please note that the header & footer are added on presentation level using Aspose.Slides. The orientation of slide from landscape to portrait or vice versa has no affect.

image.png (59.4 KB)
image.png (56.6 KB)

Hi,

I have a document like this. How can i add header footer for a document like this?

@manuR,

You can insert header/footer into the above mentioned document type by introducing section in the document. You can change page orientation using Section.PageSetup.Orientation property. To change the page orientation as shown in the shared images, you need to insert section break at the desired page. Please visit the following links for details.

How to Create Headers Footers using DocumentBuilder
Working with Sections

We are trying to add header and footer for an existing document and then converting the document to pdf file. How can we manage the header with respect to orientation of the existing page?

@manuR,

You can add header/footer to an existing document using Aspose.Words API. LayoutCollector class can be used to find the page number of a node. The logic will be to loop through each run in the document and insert a section break, at the last node of each page. Please try the sample code given below at your end.

CODE:

        // Load document
        Aspose.Words.Document doc = new Aspose.Words.Document("src");
        Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

        // Get all runs
        Aspose.Words.NodeCollection runs = doc.GetChildNodes(Aspose.Words.NodeType.Run, true);
        foreach (Aspose.Words.Run run in runs)
        {
            // Find the page number
            Aspose.Words.Layout.LayoutCollector collector = new Aspose.Words.Layout.LayoutCollector(doc);
            int pageNumber = collector.GetEndPageIndex(run);

            // If next run is on next page, add a page break
            Aspose.Words.Node nextNode = run.ParentNode.NextSibling;
            if (nextNode == null)
                continue;
            int nextPageNumber = collector.GetStartPageIndex(nextNode);

            if (nextPageNumber > pageNumber)
            {
                Console.WriteLine("Add a break here.");
                builder.MoveTo(run);
                builder.InsertBreak(Aspose.Words.BreakType.SectionBreakNewPage);

                // Modify header footer
                Aspose.Words.Section currentSection = builder.CurrentSection;
                builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.HeaderPrimary);
                builder.Write("Header of page " + nextPageNumber);
                builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.FooterPrimary);
                builder.Write("Footer of page " + nextPageNumber);
            }
        }

I am dynamically building aspose table and inserting it as header , for that how can we insert it properly with respect to the page layout

@manuR,

Thanks for your inquiry. To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your input Word document
  • Aspose.Words generated output DOCX file showing the undesired behavior
  • Your expected DOCX Word document. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document by using Microsoft Word.
  • Please also create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. Please do not include Aspose.Words.dll files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start further investigation into your issue and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.

The link contains application and document file which has issues and the converted pdf file.

@manuR,

Please see these simple input/output Word documents: Docs.zip (20.9 KB)

The input.docx has three Sections. Orientation of first and last Sections are Portrait while the second Section has Landscape Orientation. The following simple code demonstrates how to dynamically add separate Headers to these Sections. Hope, this helps.

Document doc = new Document(MyDir + @"input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// create header for first section
builder.MoveToSection(0);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
BuildTable(builder, "Portrait_sec0");

// create header for second section
builder.MoveToSection(1);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
BuildTable(builder, "Landscape_sec1");

// create header for third section
builder.MoveToSection(2);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
BuildTable(builder, "Portrait_sec2");

for(int i=1; i< doc.Sections.Count; i++)
{
    Section sec = doc.Sections[i];
    sec.HeadersFooters.LinkToPrevious(false);
}

doc.Save(MyDir + @"18.5.docx");
/////////////////////////////////////////////////////
public static void BuildTable(DocumentBuilder builder, string seed)
{
    Table table = builder.StartTable();
    builder.InsertCell();
    builder.Writeln(seed + "_0");
    builder.InsertCell();
    builder.Writeln(seed + "_1");
    builder.EndRow();

    builder.InsertCell();
    builder.Writeln(seed + "_2");
    builder.InsertCell();
    builder.Writeln(seed + "_3");
    builder.EndRow();

    builder.EndTable();
}