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",
|
||||
}
|
||||
|
||||
export enum PageNumberSeparator {
|
||||
COLON = "colon",
|
||||
EM_DASH = "emDash",
|
||||
EN_DASH = "endash",
|
||||
HYPHEN = "hyphen",
|
||||
PERIOD = "period",
|
||||
}
|
||||
|
||||
export interface IPageNumberTypeAttributes {
|
||||
readonly pageNumberStart?: number;
|
||||
readonly pageNumberFormatType?: PageNumberFormat;
|
||||
readonly pageNumberSeparator?: PageNumberSeparator;
|
||||
}
|
||||
|
||||
export class PageNumberTypeAttributes extends XmlAttributeComponent<IPageNumberTypeAttributes> {
|
||||
protected readonly xmlKeys = {
|
||||
pageNumberStart: "w:start",
|
||||
pageNumberFormatType: "w:fmt",
|
||||
pageNumberSeparator: "w:chapSep",
|
||||
};
|
||||
}
|
||||
|
||||
export class PageNumberType extends XmlComponent {
|
||||
constructor(start?: number, numberFormat?: PageNumberFormat) {
|
||||
constructor(start?: number, numberFormat?: PageNumberFormat, separator?: PageNumberSeparator) {
|
||||
super("w:pgNumType");
|
||||
this.root.push(
|
||||
new PageNumberTypeAttributes({
|
||||
pageNumberStart: start,
|
||||
pageNumberFormatType: numberFormat,
|
||||
pageNumberSeparator: separator,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
@ -56,12 +56,8 @@ export type SectionPropertiesOptions = IPageSizeAttributes &
|
||||
// Need to decouple this from the attributes
|
||||
|
||||
export class SectionProperties extends XmlComponent {
|
||||
private readonly options: SectionPropertiesOptions;
|
||||
|
||||
constructor(options: SectionPropertiesOptions = { column: {} }) {
|
||||
super("w:sectPr");
|
||||
|
||||
const {
|
||||
constructor(
|
||||
{
|
||||
width = 11906,
|
||||
height = 16838,
|
||||
top = 1440,
|
||||
@ -79,6 +75,7 @@ export class SectionProperties extends XmlComponent {
|
||||
footers,
|
||||
pageNumberFormatType,
|
||||
pageNumberStart,
|
||||
pageNumberSeparator,
|
||||
lineNumberCountBy,
|
||||
lineNumberStart,
|
||||
lineNumberRestart,
|
||||
@ -90,9 +87,10 @@ export class SectionProperties extends XmlComponent {
|
||||
pageBorderLeft,
|
||||
titlePage = false,
|
||||
verticalAlign,
|
||||
} = options;
|
||||
}: SectionPropertiesOptions = { column: {} },
|
||||
) {
|
||||
super("w:sectPr");
|
||||
|
||||
this.options = options;
|
||||
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 Columns(column.space ? column.space : 708, column.count ? column.count : 1));
|
||||
@ -101,9 +99,7 @@ export class SectionProperties extends XmlComponent {
|
||||
this.addHeaders(headers);
|
||||
this.addFooters(footers);
|
||||
|
||||
if (pageNumberStart || pageNumberFormatType) {
|
||||
this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType));
|
||||
}
|
||||
this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType, pageNumberSeparator));
|
||||
|
||||
if (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