133 lines
4.6 KiB
TypeScript
133 lines
4.6 KiB
TypeScript
// Track Revisions aka. "Track Changes"
|
|
// Import from 'docx' rather than '../build' if you install from npm
|
|
import * as fs from "fs";
|
|
import { Document, Packer, Paragraph, TextRun, ShadingType, DeletedTextRun, InsertedTextRun, Footer, PageNumber, AlignmentType, FootnoteReferenceRun } from "../build";
|
|
|
|
/*
|
|
For reference, see
|
|
- https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.insertedrun
|
|
- https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.deletedrun
|
|
|
|
The method `addTrackRevisions()` adds an element `<w:trackRevisions />` to the `settings.xml` file. This specifies that the application shall track *new* revisions made to the existing document.
|
|
See also https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.trackrevisions
|
|
|
|
Note that this setting enables to track *new changes* after teh file is generated, so this example will still show inserted and deleted text runs when you remove it.
|
|
*/
|
|
|
|
const doc = new Document({
|
|
footnotes: [
|
|
new Paragraph({
|
|
children:[
|
|
new TextRun("This is a footnote"),
|
|
new DeletedTextRun({
|
|
text: " with some extra text which was deleted",
|
|
id: 0,
|
|
author: "Firstname Lastname",
|
|
date: "2020-10-06T09:05:00Z",
|
|
}),
|
|
new InsertedTextRun({
|
|
text: " and new content",
|
|
id: 1,
|
|
author: "Firstname Lastname",
|
|
date: "2020-10-06T09:05:00Z",
|
|
})
|
|
]
|
|
}),
|
|
],
|
|
});
|
|
|
|
doc.Settings.addTrackRevisions()
|
|
|
|
const paragraph = new Paragraph({
|
|
children: [
|
|
new TextRun("This is a simple demo "),
|
|
new TextRun({
|
|
text: "on how to "
|
|
}),
|
|
new InsertedTextRun({
|
|
text: "mark a text as an insertion ",
|
|
id: 0,
|
|
author: "Firstname Lastname",
|
|
date: "2020-10-06T09:00:00Z",
|
|
}),
|
|
new DeletedTextRun({
|
|
text: "or a deletion.",
|
|
id: 1,
|
|
author: "Firstname Lastname",
|
|
date: "2020-10-06T09:00:00Z",
|
|
})
|
|
],
|
|
});
|
|
|
|
doc.addSection({
|
|
properties: {},
|
|
children: [
|
|
paragraph,
|
|
new Paragraph({
|
|
children: [
|
|
new TextRun("This is a demo "),
|
|
new DeletedTextRun({
|
|
text: "in order",
|
|
color: "red",
|
|
bold: true,
|
|
size: 24,
|
|
font: {
|
|
name: "Garamond",
|
|
},
|
|
shading: {
|
|
type: ShadingType.REVERSE_DIAGONAL_STRIPE,
|
|
color: "00FFFF",
|
|
fill: "FF0000",
|
|
},
|
|
id: 2,
|
|
author: "Firstname Lastname",
|
|
date: "2020-10-06T09:00:00Z",
|
|
}).break(),
|
|
new InsertedTextRun({
|
|
text: "to show how to ",
|
|
bold: false,
|
|
id: 3,
|
|
author: "Firstname Lastname",
|
|
date: "2020-10-06T09:05:00Z",
|
|
}),
|
|
new TextRun({
|
|
bold: true,
|
|
children: [ "\tuse Inserted and Deleted TextRuns.", new FootnoteReferenceRun(1) ],
|
|
}),
|
|
],
|
|
}),
|
|
],
|
|
footers: {
|
|
default: new Footer({
|
|
children: [
|
|
new Paragraph({
|
|
alignment: AlignmentType.CENTER,
|
|
children: [
|
|
new TextRun("Awesome LLC"),
|
|
new TextRun({
|
|
children: ["Page Number: ", PageNumber.CURRENT],
|
|
}),
|
|
new DeletedTextRun({
|
|
children: [" to ", PageNumber.TOTAL_PAGES],
|
|
id: 4,
|
|
author: "Firstname Lastname",
|
|
date: "2020-10-06T09:05:00Z",
|
|
}),
|
|
new InsertedTextRun({
|
|
children: [" from ", PageNumber.TOTAL_PAGES],
|
|
bold: true,
|
|
id: 5,
|
|
author: "Firstname Lastname",
|
|
date: "2020-10-06T09:05:00Z",
|
|
}),
|
|
],
|
|
}),
|
|
],
|
|
}),
|
|
},
|
|
});
|
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
});
|