heavysixer / node-pptx

Generate PPTX files on the server-side with JavaScript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Slide number shown by default change styling

an-rushikeshlondhe opened this issue · comments

You can directly change the xmlObject.

function setPageNumStyle(slide, style){
    let pageNumElement = slide.powerPointFactory.pptFactory.slideFactory.content[`ppt/slides/${slide.name}.xml`]['p:sld']['p:cSld'][0]['p:spTree'][0]['p:sp'][0];
    let fld = pageNumElement['p:txBody'][0]['a:p'][0]['a:fld'][0];
    let styleData = {$: fld['a:rPr'][0].$};
    if(style.fontSize !== undefined) styleData.$.sz = style.fontSize * 100;
    if(style.fontBold !== undefined) styleData.$.b = style.fontBold ? 1 : 0;
    if(style.fontItalic !== undefined) styleData.$.i = style.fontItalic ? 1 : 0;
    if(style.fontUnderline !== undefined) styleData.$.u = style.fontUnderline ? 'sng' : 0;
    if(style.textColor) styleData['a:solidFill'] = [{'a:srgbClr': [{$: {val: style.textColor}}]}];
    if(style.fontFace){
        let fontFaceAttribute = [{$: {typeface: style.fontFace, pitchFamily: 0, charset: 0}}];
        styleData['a:latin'] = fontFaceAttribute;
        styleData['a:cs'] = fontFaceAttribute;
    }
    fld['a:rPr'][0] = styleData;
}

Here's how the function works.

const PPTX = require('nodejs-pptx');
let pptx = new PPTX.Composer();
await pptx.compose(async pres => {
  await pres.addSlide(slide => {
    setPageNumStyle(slide, {
        fontFace: 'Abadi', 
        fontSize: 15, 
        fontBold: true, 
        fontItalic: false, 
        fontUnderline: true, 
        textColor: '000000', 
    });
  });
});
await pptx.save(`./set-page-num-style.pptx`);