Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/templates/docx-postprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,22 @@ function addWatermarkToHeaderXml(headerXml: string, text: string): string {
let nextXml = ensureWordNamespace(headerXml, 'w:hdr');
nextXml = ensureNamespace(nextXml, 'w:hdr', 'xmlns:v', 'urn:schemas-microsoft-com:vml');
nextXml = ensureNamespace(nextXml, 'w:hdr', 'xmlns:o', 'urn:schemas-microsoft-com:office:office');
nextXml = ensureNamespace(nextXml, 'w:hdr', 'xmlns:w10', 'urn:schemas-microsoft-com:office:word');

return nextXml.replace('</w:hdr>', `${watermarkParagraphXml(text)}</w:hdr>`);
}

function watermarkParagraphXml(text: string): string {
const escapedText = escapeXmlAttribute(text);
return `<w:p><w:r><w:pict><v:shape id="DocgenWatermark" o:spid="_x0000_s1025" type="#_x0000_t136" style="position:absolute;margin-left:0;margin-top:0;width:468pt;height:117pt;rotation:315;z-index:-251654144" fillcolor="silver" stroked="f"><v:fill opacity=".35"/><v:textpath style="font-family:Calibri;font-size:1pt" string="${escapedText}"/></v:shape></w:pict></w:r></w:p>`;
return `<w:p><w:r><w:pict>${watermarkShapeTypeXml()}${watermarkShapeXml(escapedText)}</w:pict></w:r></w:p>`;
}

function watermarkShapeTypeXml(): string {
return '<v:shapetype id="_x0000_t136" coordsize="21600,21600" o:spt="136" adj="10800" path="m@7,l@8,m@5,21600l@6,21600e"><v:formulas><v:f eqn="sum #0 0 10800"/><v:f eqn="prod #0 2 1"/><v:f eqn="sum 21600 0 @1"/><v:f eqn="sum 0 0 @2"/><v:f eqn="sum 21600 0 @3"/><v:f eqn="if @0 @3 0"/><v:f eqn="if @0 21600 @1"/><v:f eqn="if @0 0 @2"/><v:f eqn="if @0 @4 21600"/><v:f eqn="mid @5 @6"/><v:f eqn="mid @8 @5"/><v:f eqn="mid @7 @8"/><v:f eqn="mid @6 @7"/><v:f eqn="sum @6 0 @5"/></v:formulas><v:path textpathok="t" o:connecttype="custom" o:connectlocs="@9,0;@10,10800;@11,21600;@12,10800" o:connectangles="270,180,90,0"/><v:textpath on="t" fitshape="t"/><v:handles><v:h position="#0,bottomRight" xrange="6629,14971"/></v:handles><o:lock v:ext="edit" text="t" shapetype="t"/></v:shapetype>';
}

function watermarkShapeXml(escapedText: string): string {
return `<v:shape id="DocgenWatermark" o:spid="_x0000_s1025" type="#_x0000_t136" style="position:absolute;margin-left:0;margin-top:0;width:527.85pt;height:131.95pt;rotation:315;z-index:251659264;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin;mso-wrap-edited:f" fillcolor="#C0C0C0" stroked="f"><v:fill opacity=".22"/><v:textpath style="font-family:&quot;Calibri&quot;;font-size:1pt" string="${escapedText}"/><w10:wrap anchorx="margin" anchory="margin"/></v:shape>`;
}

async function ensureContentTypeOverride(
Expand Down
4 changes: 4 additions & 0 deletions test/templates/docx-postprocess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ describe('DOCX template post-processing', () => {
expect(documentXml).toContain('<w:headerReference w:type="default" r:id="');
expect(headerXml).toContain('<v:shape');
expect(headerXml).toContain('DocgenWatermark');
expect(headerXml).toContain('<v:shapetype id="_x0000_t136"');
expect(headerXml).toContain('mso-position-horizontal:center');
expect(headerXml).toContain('mso-position-vertical:center');
expect(headerXml).toContain('<w10:wrap anchorx="margin" anchory="margin"/>');
expect(headerXml).toContain('string="DRAFT"');
});

Expand Down
Loading