Headers and Footers
Text that repeats on every page, such as page numbers, belongs in headers and footers. In documents and templates created in the Document Editor, your code adds and changes them with JavaScript: for the whole document or a single section, for the first page and for even pages.
The examples extend the view of the ASP.NET Core tutorial. A section is a part of a document with its own page settings and headers and footers; Sections explains how to insert them.
Give a Section Its Own Header
When a document has several sections, each new section starts with the headers and footers of the previous one, connected to them. To give a section a different header, get the header from the section's collection and disconnect it with setConnectedToPrevious:
<input type="button" onclick="addSectionHeader()" value="Add Section Header" />
<script>
function addSectionHeader() {
var headerType = TXTextControl.HeaderFooterType.Header;
// Get the section at the current input position
TXTextControl.sections.getItem(function (section) {
var headersAndFooters = section.headersAndFooters;
headersAndFooters.add(headerType, function () {
headersAndFooters.getItem(headerType, function (header) {
if (!header) {
return;
}
// Don't share the content with the previous section's header
header.setConnectedToPrevious(false, function () {
header.setDistance(720, function () { // 720 twips = 0.5 inch
// Move the input position into the header
header.activate(function (activated) {
console.log("Header activated: " + activated);
});
});
});
});
});
});
}
</script>
- TXTextControl.sections with getItem returns the section that contains the input position.
setConnectedToPrevious(false)separates the header from the header of the previous section. A connected header always has the same content as the previous one.- setDistance sets the distance of the header from the top of the page in twips, 1/1440 inch. For a footer, it is the distance from the bottom.
- activate moves the input position into the header, so that the user can type the text. deactivate returns it to the main text.
Place the cursor in a section after the first one and click Add Section Header. The cursor moves into the header of this section, and the text you type there doesn't change the header of the previous section.
Next Steps
- Sections: insert sections with their own page layout.
- Fields: the other fields of a document.
- Headers and Footers in Document Processing: add headers and footers on the server with
ServerTextControl.
