Header Insertion

hello,
Can you please suggest that how can we insert text on Header on a particular page.

@RiteshAML10

To insert text into the header of a specific page using Aspose.Words, you can follow these steps. Below is a sample code snippet for adding a header to the first page of a document:

// Create a new document and a DocumentBuilder
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Set up page settings to have a different first page header
Section section = builder.getCurrentSection();
PageSetup pageSetup = section.getPageSetup();
pageSetup.setDifferentFirstPageHeaderFooter(true);

// Move to the header of the first page
builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.getFont().setName("Arial");
builder.getFont().setBold(true);
builder.getFont().setSize(14.0);
builder.write("This is the header text for the first page.");

// If you want to add a header to the primary header (for subsequent pages)
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("This is the header text for subsequent pages.");

// Save the document
doc.save("YourDirectoryPath/DocumentWithHeaders.docx");

Key Points:

  • You can set different headers for the first page and subsequent pages using setDifferentFirstPageHeaderFooter(true).
  • Use moveToHeaderFooter to navigate to the specific header location (first page or primary).
  • Customize the text, font, and alignment as needed.

This approach allows you to manage headers effectively within your document, ensuring that you have the desired text on specific pages.

For further details, you can refer to the Aspose.Words for Java documentation on working with headers and footers.

If you have any more questions or need additional examples, feel free to ask!

@RiteshAML10 Please see our documentation to learn how to work with headers/footers:
https://docs.aspose.com/words/net/working-with-headers-and-footers/

thanks @Professionalize.Discourse ,
As I have checked as you have shared code with me. After using that it is creating header on all pages but I want create header on a particular page. Like it should be generate on second page or third page only. And please suggest solution in c#

@RiteshAML10 As you may know, MS Word documents are flow by their nature and there is no “page” concept. The consumer applications reflows the document content into pages on the fly. So there is no direct way to add header/footer on the particular page only. To achieve this, the page should be represented as a separate section. You can achieve this by splitting the document into pages using Document.ExtractPages method.

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

// Extract the first page.
Document first = doc.ExtractPages(0, 1);
// Extract the second page
Document second = doc.ExtractPages(1, 1);
// Extract the rest pages.
Document rest = doc.ExtractPages(2, doc.PageCount - 2);

// Add header to the second page.
DocumentBuilder builder = new DocumentBuilder(second);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("The second page header.");

// Merge documents back.
doc = Merger.Merge(new Document[] { first, second, rest }, MergeFormatMode.KeepSourceLayout);

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

in.docx (15.8 KB)
out.docx (14.0 KB)