From f3aa6a92036e2d38c0800cbe547b80ccbd54a664 Mon Sep 17 00:00:00 2001 From: amitm02 Date: Tue, 2 Oct 2018 17:52:55 +0300 Subject: [PATCH] detect if page title is defined in tempalte --- demo/demo29.ts | 2 +- src/import-dotx/import-dotx.ts | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/demo/demo29.ts b/demo/demo29.ts index 8949e17af5..94c417ceef 100644 --- a/demo/demo29.ts +++ b/demo/demo29.ts @@ -12,7 +12,7 @@ fs.readFile(filePath, (err, data) => { importDotx.extract(data).then((templateDocument) => { // This any needs fixing const sectionProps = { - titlePage: true, + titlePage: templateDocument.titlePageIsDefined, } as any; const doc = new Document(undefined, sectionProps, { diff --git a/src/import-dotx/import-dotx.ts b/src/import-dotx/import-dotx.ts index 9d541f5c07..4dbe01be56 100644 --- a/src/import-dotx/import-dotx.ts +++ b/src/import-dotx/import-dotx.ts @@ -41,6 +41,7 @@ export interface IDocumentTemplate { headers: IDocumentHeader[]; footers: IDocumentFooter[]; styles: Styles; + titlePageIsDefined: boolean; } export class ImportDotx { @@ -59,6 +60,7 @@ export class ImportDotx { const documentContent = zipContent.files["word/document.xml"]; const documentRefs: IDocumentRefs = this.extractDocumentRefs(await documentContent.async("text")); + const titlePageIsDefined = this.titlePageIsDefined(await documentContent.async("text")); const relationshipContent = zipContent.files["word/_rels/document.xml.rels"]; const documentRelationships: IRelationshipFileInfo[] = this.findReferenceFiles(await relationshipContent.async("text")); @@ -77,7 +79,7 @@ export class ImportDotx { const importedComp = convertToXmlComponent(headerKey, xmlObj[headerKey]) as ImportedXmlComponent; const header = new HeaderWrapper(this.currentRelationshipId++, importedComp); - await this.addImagesToWrapper(relationFileInfo, zipContent, header); + await this.addRelationToWrapper(relationFileInfo, zipContent, header); headers.push({ type: headerRef.type, header }); } @@ -93,15 +95,21 @@ export class ImportDotx { const importedComp = convertToXmlComponent(footerKey, xmlObj[footerKey]) as ImportedXmlComponent; const footer = new FooterWrapper(this.currentRelationshipId++, importedComp); - await this.addImagesToWrapper(relationFileInfo, zipContent, footer); + await this.addRelationToWrapper(relationFileInfo, zipContent, footer); footers.push({ type: footerRef.type, footer }); } - const templateDocument: IDocumentTemplate = { headers, footers, currentRelationshipId: this.currentRelationshipId, styles }; + const templateDocument: IDocumentTemplate = { + headers, + footers, + currentRelationshipId: this.currentRelationshipId, + styles, + titlePageIsDefined, + }; return templateDocument; } - public async addImagesToWrapper( + public async addRelationToWrapper( relationFile: IRelationshipFileInfo, zipContent: JSZip, wrapper: HeaderWrapper | FooterWrapper, @@ -183,6 +191,12 @@ export class ImportDotx { return { headers, footers }; } + public titlePageIsDefined(xmlData: string): boolean { + const xmlObj = fastXmlParser.parse(xmlData, importParseOptions); + const sectionProp = xmlObj["w:document"]["w:body"]["w:sectPr"]; + return sectionProp["w:titlePg"] !== undefined; + } + public parseRefId(str: string): number { const match = /^rId(\d+)$/.exec(str); if (match === null) {