Compare commits

...

1 Commits

Author SHA1 Message Date
c5a2c1c0cb Ability to add PageNumber to paragraph 2023-02-13 23:04:59 +00:00
2 changed files with 51 additions and 4 deletions

View File

@ -10,8 +10,10 @@ import { ColumnBreak, PageBreak } from "./formatting/break";
import { Bookmark, ConcreteHyperlink, ExternalHyperlink, InternalHyperlink } from "./links";
import { Math } from "./math";
import { IParagraphPropertiesOptions, ParagraphProperties } from "./properties";
import { ImageRun, Run, SequentialIdentifier, SimpleField, SimpleMailMergeField, SymbolRun, TextRun } from "./run";
import { ImageRun, PageNumber, Run, SequentialIdentifier, SimpleField, SimpleMailMergeField, SymbolRun, TextRun } from "./run";
import { Comment, CommentRangeEnd, CommentRangeStart, CommentReference, Comments } from "./run/comment-run";
import { Begin, End, Separate } from "./run/field";
import { NumberOfPages, NumberOfPagesSection, Page } from "./run/page-number";
export type ParagraphChild =
| TextRun
@ -33,7 +35,8 @@ export type ParagraphChild =
| Comment
| CommentRangeStart
| CommentRangeEnd
| CommentReference;
| CommentReference
| PageNumber;
export interface IParagraphOptions extends IParagraphPropertiesOptions {
readonly text?: string;
@ -63,6 +66,34 @@ export class Paragraph extends FileChild {
if (options.children) {
for (const child of options.children) {
if (typeof child === "string") {
switch (child) {
case PageNumber.CURRENT:
this.root.push(new TextRun({ children: [new Begin()] }));
this.root.push(new TextRun({ children: [new Page()] }));
this.root.push(new TextRun({ children: [new Separate()] }));
this.root.push(new TextRun({ children: [new End()] }));
break;
case PageNumber.TOTAL_PAGES:
this.root.push(new TextRun({ children: [new Begin()] }));
this.root.push(new TextRun({ children: [new NumberOfPages()] }));
this.root.push(new TextRun({ children: [new Separate()] }));
this.root.push(new TextRun("0"));
this.root.push(new TextRun({ children: [new End()] }));
break;
case PageNumber.TOTAL_PAGES_IN_SECTION:
this.root.push(new TextRun({ children: [new Begin()] }));
this.root.push(new TextRun({ children: [new NumberOfPagesSection()] }));
this.root.push(new TextRun({ children: [new Separate()] }));
this.root.push(new TextRun({ children: [new End()] }));
break;
default:
this.root.push(new TextRun(child));
break;
}
continue;
}
if (child instanceof Bookmark) {
this.root.push(child.start);
for (const textRun of child.children) {

View File

@ -1,13 +1,29 @@
// http://www.datypic.com/sc/ooxml/e-w_fldChar-1.html
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
// <xsd:complexType name="CT_FldChar">
// <xsd:choice>
// <xsd:element name="fldData" type="CT_Text" minOccurs="0" maxOccurs="1" />
// <xsd:element name="ffData" type="CT_FFData" minOccurs="0" maxOccurs="1" />
// <xsd:element name="numberingChange" type="CT_TrackChangeNumbering" minOccurs="0" />
// </xsd:choice>
// <xsd:attribute name="fldCharType" type="ST_FldCharType" use="required" />
// <xsd:attribute name="fldLock" type="s:ST_OnOff" />
// <xsd:attribute name="dirty" type="s:ST_OnOff" />
// </xsd:complexType>
enum FieldCharacterType {
BEGIN = "begin",
END = "end",
SEPARATE = "separate",
}
class FidCharAttrs extends XmlAttributeComponent<{ readonly type: FieldCharacterType; readonly dirty?: boolean }> {
protected readonly xmlKeys = { type: "w:fldCharType", dirty: "w:dirty" };
class FidCharAttrs extends XmlAttributeComponent<{
readonly type: FieldCharacterType;
readonly dirty?: boolean;
readonly fieldLock?: boolean;
}> {
protected readonly xmlKeys = { type: "w:fldCharType", dirty: "w:dirty", fieldLock: "w:fldLock" };
}
export class Begin extends XmlComponent {