Files
docx-js/demo/60-track-revisions.ts

165 lines
5.9 KiB
TypeScript
Raw Normal View History

// Track Revisions aka. "Track Changes"
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import {
AlignmentType,
DeletedTextRun,
Document,
Footer,
FootnoteReferenceRun,
InsertedTextRun,
Packer,
PageNumber,
Paragraph,
ShadingType,
2022-10-29 15:10:02 +01:00
Tab,
TextRun,
} 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 setting `features: { trackRevisions: true }` 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.
*/
2021-03-19 20:53:56 +00:00
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",
}),
],
});
const doc = new Document({
2021-03-11 01:06:55 +00:00
footnotes: {
1: {
children: [
2021-03-11 01:06:55 +00:00
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",
}),
],
}),
],
2021-03-11 01:06:55 +00:00
},
},
features: {
trackRevisions: true,
},
2021-03-19 20:53:56 +00:00
sections: [
{
properties: {},
children: [
2021-03-19 20:53:56 +00:00
paragraph,
new Paragraph({
children: [
2021-03-19 20:53:56 +00:00
new TextRun("This is a demo "),
new DeletedTextRun({
2021-03-19 20:53:56 +00:00
break: 1,
text: "in order",
color: "ff0000",
2021-03-19 20:53:56 +00:00
bold: true,
size: 24,
font: {
name: "Garamond",
},
shading: {
2021-05-29 05:51:06 +03:00
type: ShadingType.REVERSE_DIAGONAL_STRIPE,
2021-03-19 20:53:56 +00:00
color: "00FFFF",
fill: "FF0000",
},
id: 2,
author: "Firstname Lastname",
2021-03-19 20:53:56 +00:00
date: "2020-10-06T09:00:00Z",
}),
new InsertedTextRun({
2021-03-19 20:53:56 +00:00
text: "to show how to ",
bold: false,
id: 3,
author: "Firstname Lastname",
date: "2020-10-06T09:05:00Z",
}),
2021-03-19 20:53:56 +00:00
new TextRun({
bold: true,
2022-10-29 15:10:02 +01:00
children: [new Tab(), "use Inserted and Deleted TextRuns.", new FootnoteReferenceRun(1)],
2021-03-19 20:53:56 +00:00
}),
2021-11-26 12:29:11 +01:00
new TextRun({
bold: true,
text: "And some style changes",
revision: {
id: 4,
author: "Firstname Lastname",
date: "2020-10-06T09:05:00Z",
bold: false,
2022-07-06 15:26:17 +01:00
},
2021-11-26 12:29:11 +01:00
}),
],
}),
],
2021-03-19 20:53:56 +00:00
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);
});