Restart page numbering
Add separator option
This commit is contained in:
87
demo/52-restart-page-numbers.ts
Normal file
87
demo/52-restart-page-numbers.ts
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
// Page numbers - Start from 0 on a new section
|
||||||
|
// Import from 'docx' rather than '../build' if you install from npm
|
||||||
|
import * as fs from "fs";
|
||||||
|
import { AlignmentType, Document, Header, Packer, PageBreak, PageNumber, Paragraph, TextRun } from "../build";
|
||||||
|
|
||||||
|
const doc = new Document();
|
||||||
|
|
||||||
|
doc.addSection({
|
||||||
|
headers: {
|
||||||
|
default: new Header({
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
alignment: AlignmentType.RIGHT,
|
||||||
|
children: [
|
||||||
|
new TextRun("My Title "),
|
||||||
|
new TextRun({
|
||||||
|
children: ["Page ", PageNumber.CURRENT],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
first: new Header({
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
alignment: AlignmentType.RIGHT,
|
||||||
|
children: [
|
||||||
|
new TextRun("First Page Header "),
|
||||||
|
new TextRun({
|
||||||
|
children: ["Page ", PageNumber.CURRENT],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
children: [new TextRun("First Page"), new PageBreak()],
|
||||||
|
}),
|
||||||
|
new Paragraph("Second Page"),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
doc.addSection({
|
||||||
|
properties: {
|
||||||
|
pageNumberStart: 1,
|
||||||
|
},
|
||||||
|
headers: {
|
||||||
|
default: new Header({
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
alignment: AlignmentType.RIGHT,
|
||||||
|
children: [
|
||||||
|
new TextRun("My Title "),
|
||||||
|
new TextRun({
|
||||||
|
children: ["Page ", PageNumber.CURRENT],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
first: new Header({
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
alignment: AlignmentType.RIGHT,
|
||||||
|
children: [
|
||||||
|
new TextRun("First Page Header of Second section"),
|
||||||
|
new TextRun({
|
||||||
|
children: ["Page ", PageNumber.CURRENT],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
children: [new TextRun("Third Page"), new PageBreak()],
|
||||||
|
}),
|
||||||
|
new Paragraph("Fourth Page"),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
Packer.toBuffer(doc).then((buffer) => {
|
||||||
|
fs.writeFileSync("My Document.docx", buffer);
|
||||||
|
});
|
@ -16,25 +16,36 @@ export enum PageNumberFormat {
|
|||||||
UPPER_ROMAN = "upperRoman",
|
UPPER_ROMAN = "upperRoman",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum PageNumberSeparator {
|
||||||
|
COLON = "colon",
|
||||||
|
EM_DASH = "emDash",
|
||||||
|
EN_DASH = "endash",
|
||||||
|
HYPHEN = "hyphen",
|
||||||
|
PERIOD = "period",
|
||||||
|
}
|
||||||
|
|
||||||
export interface IPageNumberTypeAttributes {
|
export interface IPageNumberTypeAttributes {
|
||||||
readonly pageNumberStart?: number;
|
readonly pageNumberStart?: number;
|
||||||
readonly pageNumberFormatType?: PageNumberFormat;
|
readonly pageNumberFormatType?: PageNumberFormat;
|
||||||
|
readonly pageNumberSeparator?: PageNumberSeparator;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class PageNumberTypeAttributes extends XmlAttributeComponent<IPageNumberTypeAttributes> {
|
export class PageNumberTypeAttributes extends XmlAttributeComponent<IPageNumberTypeAttributes> {
|
||||||
protected readonly xmlKeys = {
|
protected readonly xmlKeys = {
|
||||||
pageNumberStart: "w:start",
|
pageNumberStart: "w:start",
|
||||||
pageNumberFormatType: "w:fmt",
|
pageNumberFormatType: "w:fmt",
|
||||||
|
pageNumberSeparator: "w:chapSep",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export class PageNumberType extends XmlComponent {
|
export class PageNumberType extends XmlComponent {
|
||||||
constructor(start?: number, numberFormat?: PageNumberFormat) {
|
constructor(start?: number, numberFormat?: PageNumberFormat, separator?: PageNumberSeparator) {
|
||||||
super("w:pgNumType");
|
super("w:pgNumType");
|
||||||
this.root.push(
|
this.root.push(
|
||||||
new PageNumberTypeAttributes({
|
new PageNumberTypeAttributes({
|
||||||
pageNumberStart: start,
|
pageNumberStart: start,
|
||||||
pageNumberFormatType: numberFormat,
|
pageNumberFormatType: numberFormat,
|
||||||
|
pageNumberSeparator: separator,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -56,12 +56,8 @@ export type SectionPropertiesOptions = IPageSizeAttributes &
|
|||||||
// Need to decouple this from the attributes
|
// Need to decouple this from the attributes
|
||||||
|
|
||||||
export class SectionProperties extends XmlComponent {
|
export class SectionProperties extends XmlComponent {
|
||||||
private readonly options: SectionPropertiesOptions;
|
constructor(
|
||||||
|
{
|
||||||
constructor(options: SectionPropertiesOptions = { column: {} }) {
|
|
||||||
super("w:sectPr");
|
|
||||||
|
|
||||||
const {
|
|
||||||
width = 11906,
|
width = 11906,
|
||||||
height = 16838,
|
height = 16838,
|
||||||
top = 1440,
|
top = 1440,
|
||||||
@ -79,6 +75,7 @@ export class SectionProperties extends XmlComponent {
|
|||||||
footers,
|
footers,
|
||||||
pageNumberFormatType,
|
pageNumberFormatType,
|
||||||
pageNumberStart,
|
pageNumberStart,
|
||||||
|
pageNumberSeparator,
|
||||||
lineNumberCountBy,
|
lineNumberCountBy,
|
||||||
lineNumberStart,
|
lineNumberStart,
|
||||||
lineNumberRestart,
|
lineNumberRestart,
|
||||||
@ -90,9 +87,10 @@ export class SectionProperties extends XmlComponent {
|
|||||||
pageBorderLeft,
|
pageBorderLeft,
|
||||||
titlePage = false,
|
titlePage = false,
|
||||||
verticalAlign,
|
verticalAlign,
|
||||||
} = options;
|
}: SectionPropertiesOptions = { column: {} },
|
||||||
|
) {
|
||||||
|
super("w:sectPr");
|
||||||
|
|
||||||
this.options = options;
|
|
||||||
this.root.push(new PageSize(width, height, orientation));
|
this.root.push(new PageSize(width, height, orientation));
|
||||||
this.root.push(new PageMargin(top, right, bottom, left, header, footer, gutter, mirror));
|
this.root.push(new PageMargin(top, right, bottom, left, header, footer, gutter, mirror));
|
||||||
this.root.push(new Columns(column.space ? column.space : 708, column.count ? column.count : 1));
|
this.root.push(new Columns(column.space ? column.space : 708, column.count ? column.count : 1));
|
||||||
@ -101,9 +99,7 @@ export class SectionProperties extends XmlComponent {
|
|||||||
this.addHeaders(headers);
|
this.addHeaders(headers);
|
||||||
this.addFooters(footers);
|
this.addFooters(footers);
|
||||||
|
|
||||||
if (pageNumberStart || pageNumberFormatType) {
|
this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType, pageNumberSeparator));
|
||||||
this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lineNumberCountBy || lineNumberStart || lineNumberRestart || lineNumberDistance) {
|
if (lineNumberCountBy || lineNumberStart || lineNumberRestart || lineNumberDistance) {
|
||||||
this.root.push(new LineNumberType(lineNumberCountBy, lineNumberStart, lineNumberRestart, lineNumberDistance));
|
this.root.push(new LineNumberType(lineNumberCountBy, lineNumberStart, lineNumberRestart, lineNumberDistance));
|
||||||
@ -191,8 +187,4 @@ export class SectionProperties extends XmlComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public get Options(): SectionPropertiesOptions {
|
|
||||||
return this.options;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user