commit
This commit is contained in:
19
.vscode/launch.json
vendored
Normal file
19
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "Launch Program",
|
||||
"program": "${workspaceFolder}/demo/importDemo.js",
|
||||
// "preLaunchTask": "tsc: build - tsconfig.json",
|
||||
"outFiles": [
|
||||
"${workspaceFolder}/build/**/*.js"
|
||||
],
|
||||
"sourceMaps": true,
|
||||
}
|
||||
]
|
||||
}
|
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -5,5 +5,6 @@
|
||||
"editor.formatOnSave": false,
|
||||
"prettier.tabWidth": 4,
|
||||
"prettier.arrowParens": "always",
|
||||
"prettier.bracketSpacing": true
|
||||
"prettier.bracketSpacing": true,
|
||||
"git.ignoreLimitWarning": true
|
||||
}
|
||||
|
24
.vscode/tasks.json
vendored
Normal file
24
.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
// See https://go.microsoft.com/fwlink/?LinkId=733558
|
||||
// for the documentation about the tasks.json format
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"type": "typescript",
|
||||
"tsconfig": "tsconfig.json",
|
||||
"option": "watch",
|
||||
"problemMatcher": [
|
||||
"$tsc-watch"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "npm",
|
||||
"script": "ts-node",
|
||||
"problemMatcher": [],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
BIN
MyDocument.docx
Normal file
BIN
MyDocument.docx
Normal file
Binary file not shown.
13
demo/importDemo.js
Normal file
13
demo/importDemo.js
Normal file
@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
exports.__esModule = true;
|
||||
var build_1 = require("../build");
|
||||
var fs = require("fs");
|
||||
var importDocx = new build_1.ImportDocx();
|
||||
fs.readFile("./src/import/template.dotx", function (err, data) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
else {
|
||||
importDocx.read(data);
|
||||
}
|
||||
});
|
28
demo/importDemo.ts
Normal file
28
demo/importDemo.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { Document, Packer, Paragraph, ImportDocx } from "../build";
|
||||
import * as fs from "fs";
|
||||
|
||||
console.log(process.cwd());
|
||||
|
||||
let importDocx = new ImportDocx();
|
||||
fs.readFile("./src/importDocx/simple.dotx", (err, data) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
else {
|
||||
importDocx.read(data).then(xmlComp => {
|
||||
console.log(xmlComp);
|
||||
const doc = new Document({templateHeader : xmlComp});
|
||||
// const doc = new Document();
|
||||
const paragraph = new Paragraph("Hello World");
|
||||
doc.addParagraph(paragraph);
|
||||
|
||||
// console.log(JSON.stringify(xmlComp, null, 2));
|
||||
const packer = new Packer();
|
||||
|
||||
packer.toBuffer(doc).then((buffer) => {
|
||||
fs.writeFileSync("MyDocument.docx", buffer);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
});
|
@ -48,7 +48,7 @@
|
||||
"types": "./build/index.d.ts",
|
||||
"dependencies": {
|
||||
"@types/image-size": "0.0.29",
|
||||
"@types/jszip": "^3.1.3",
|
||||
"@types/jszip": "^3.1.4",
|
||||
"fast-xml-parser": "^3.3.6",
|
||||
"image-size": "^0.6.2",
|
||||
"jszip": "^3.1.5",
|
||||
|
@ -11,6 +11,8 @@ export interface IPropertiesOptions {
|
||||
lastModifiedBy?: string;
|
||||
revision?: string;
|
||||
externalStyles?: string;
|
||||
|
||||
templateHeader? : XmlComponent;
|
||||
}
|
||||
|
||||
export class CoreProperties extends XmlComponent {
|
||||
|
@ -14,6 +14,7 @@ import { Styles } from "./styles";
|
||||
import { ExternalStylesFactory } from "./styles/external-styles-factory";
|
||||
import { DefaultStylesFactory } from "./styles/factory";
|
||||
import { Table } from "./table";
|
||||
import { XmlComponent } from "./xml-components";
|
||||
|
||||
export class File {
|
||||
private readonly document: Document;
|
||||
@ -71,7 +72,7 @@ export class File {
|
||||
);
|
||||
this.media = new Media();
|
||||
|
||||
const header = this.createHeader();
|
||||
const header = this.createHeader(options.templateHeader);
|
||||
const footer = this.createFooter();
|
||||
|
||||
this.fileRelationships = new Relationships();
|
||||
@ -169,8 +170,10 @@ export class File {
|
||||
this.footNotes.createFootNote(paragraph);
|
||||
}
|
||||
|
||||
public createHeader(): HeaderWrapper {
|
||||
const header = new HeaderWrapper(this.media, this.currentRelationshipId++);
|
||||
public createHeader(templateHeader? : XmlComponent): HeaderWrapper {
|
||||
const header = new HeaderWrapper(this.media, this.currentRelationshipId++, templateHeader);
|
||||
console.log('\n\n-------\n\n');
|
||||
console.log('header', JSON.stringify(header.Header, null, 2));
|
||||
this.headerWrapper.push(header);
|
||||
this.docRelationships.createRelationship(
|
||||
header.Header.ReferenceId,
|
||||
|
@ -5,12 +5,13 @@ import { ImageParagraph, Paragraph } from "./paragraph";
|
||||
import { Relationships } from "./relationships";
|
||||
import { Table } from "./table";
|
||||
|
||||
|
||||
export class HeaderWrapper {
|
||||
private readonly header: Header;
|
||||
private readonly relationships: Relationships;
|
||||
|
||||
constructor(private readonly media: Media, referenceId: number) {
|
||||
this.header = new Header(referenceId);
|
||||
constructor(private readonly media: Media, referenceId: number, initContent? : XmlComponent) {
|
||||
this.header = new Header(referenceId, initContent);
|
||||
this.relationships = new Relationships();
|
||||
}
|
||||
|
||||
|
@ -7,10 +7,11 @@ import { HeaderAttributes } from "./header-attributes";
|
||||
export class Header extends XmlComponent {
|
||||
private readonly refId: number;
|
||||
|
||||
constructor(referenceNumber: number) {
|
||||
super("w:hdr");
|
||||
constructor(referenceNumber: number, initContent? : XmlComponent) {
|
||||
super("w:hdr", initContent);
|
||||
|
||||
this.refId = referenceNumber;
|
||||
|
||||
this.root.push(
|
||||
new HeaderAttributes({
|
||||
wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
|
||||
|
@ -3,11 +3,17 @@ import { IXmlableObject } from "./xmlable-object";
|
||||
export { BaseXmlComponent };
|
||||
|
||||
export abstract class XmlComponent extends BaseXmlComponent {
|
||||
protected root: Array<BaseXmlComponent | string>;
|
||||
public root: Array<BaseXmlComponent | string>;
|
||||
|
||||
constructor(rootKey: string) {
|
||||
constructor(rootKey: string, initContent? : XmlComponent) {
|
||||
super(rootKey);
|
||||
this.root = new Array<BaseXmlComponent>();
|
||||
this.root = initContent ? initContent.root : new Array<BaseXmlComponent>();
|
||||
if (initContent) {
|
||||
console.log('\n\n-------\n\n');
|
||||
console.log('new root', JSON.stringify(initContent, null,2));
|
||||
console.log('\n\n-------\n\n');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public prepForXml(): IXmlableObject {
|
||||
|
27
src/importDocx/importDocx.ts
Normal file
27
src/importDocx/importDocx.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import * as JSZip from "jszip";
|
||||
import * as fastXmlParser from "fast-xml-parser";
|
||||
import { convertToXmlComponent, parseOptions, ImportedXmlComponent } from "file/xml-components";
|
||||
|
||||
export class ImportDocx {
|
||||
|
||||
constructor() {
|
||||
}
|
||||
|
||||
read(data) : Promise<any> {
|
||||
return new Promise((resolve) => {
|
||||
JSZip.loadAsync(data).then((zipContent) => {
|
||||
let headerContent = zipContent['files']['word/header2.xml'];
|
||||
|
||||
headerContent.async('text').then((xmlData : string) => {
|
||||
console.log('\n\n-------\n\n');
|
||||
console.log('headerContent', JSON.stringify(xmlData, null, 2));
|
||||
console.log('\n\n-------\n\n');
|
||||
const jsonObj = fastXmlParser.parse(xmlData, parseOptions);
|
||||
let xmlComp = convertToXmlComponent('w:hdr', jsonObj['w:hdr']) as ImportedXmlComponent;
|
||||
resolve(xmlComp);
|
||||
})
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
|
1
src/importDocx/index.ts
Normal file
1
src/importDocx/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './importDocx';
|
BIN
src/importDocx/simple.dotx
Normal file
BIN
src/importDocx/simple.dotx
Normal file
Binary file not shown.
BIN
src/importDocx/template.dotx
Normal file
BIN
src/importDocx/template.dotx
Normal file
Binary file not shown.
@ -3,3 +3,5 @@
|
||||
export { File as Document } from "./file";
|
||||
export * from "./file";
|
||||
export * from "./export";
|
||||
export * from "./importDocx";
|
||||
|
||||
|
Reference in New Issue
Block a user