How to use docx - 10 common examples

To help you get started, we’ve selected a few docx examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github andreafeccomandi / bibisco / bibisco / app / services / DocxExporterService.js View on Github external
currentParagraph.addRun(currentText);
            currentParagraph.spacing(h3counter === 1 ? h3marginspacing1st : h3marginspacing);

          } else if (name === 'question') {
            currentParagraph = new docx.Paragraph();
            currentParagraph.spacing(paragraphspacing);
            boldActive = true;
            italicsActive = true;
        
          } else if (name === 'ul') {
            unorderedListActive = true;
          } else if (name === 'ol') {
            orderedListActive = true;
          } else if (name === 'p' || name === 'li') {

            currentParagraph = new docx.Paragraph();

            // spacing
            currentParagraph.spacing(paragraphspacing);
            
            // alignment
            if (!attribs.style || attribs.style.indexOf('text-align: left') > -1) {
              currentParagraph.left();
            } else if (attribs.style.indexOf('text-align: center') > -1) {
              currentParagraph.center();
            } else if (attribs.style.indexOf('text-align: right') > -1) {
              currentParagraph.right();
            } else if (attribs.style.indexOf('text-align: justify') > -1) {
              currentParagraph.justified();
            }

            // indent
github andreafeccomandi / bibisco / bibisco / app / services / DocxExporterService.js View on Github external
let italicsActive = false;
      let underlineActive = false;
      let strikeActive = false;
      let orderedListActive = false;
      let unorderedListActive = false;
      let h1counter = 0;
      let h2counter = 0;
      let h3counter = 0;
    
      const numbering = new docx.Numbering();
      const numberedAbstract = numbering.createAbstractNumbering();
      numberedAbstract.createLevel(0, 'decimal', '%1. ', 'left');
      const letterNumbering = numbering.createConcreteNumbering(numberedAbstract);

      // Create document
      let doc = new docx.Document();

      var parser = new htmlparser.Parser({

        onopentag: function (name, attribs) {

          if (name === 'exporttitle') {
            currentParagraph = new docx.Paragraph();
            currentParagraph.center();
            currentParagraph.spacing(exportitlespacing);
            boldActive = true;

          } else if (name === 'exportsubtitle') {
            currentParagraph = new docx.Paragraph();
            currentParagraph.center();
            boldActive = true;
github andreafeccomandi / bibisco / bibisco / app / services / DocxExporterService.js View on Github external
export: function (path, html, font, indent, callback) {

      let currentParagraph = null;
      let boldActive = false;
      let italicsActive = false;
      let underlineActive = false;
      let strikeActive = false;
      let orderedListActive = false;
      let unorderedListActive = false;
      let h1counter = 0;
      let h2counter = 0;
      let h3counter = 0;
    
      const numbering = new docx.Numbering();
      const numberedAbstract = numbering.createAbstractNumbering();
      numberedAbstract.createLevel(0, 'decimal', '%1. ', 'left');
      const letterNumbering = numbering.createConcreteNumbering(numberedAbstract);

      // Create document
      let doc = new docx.Document();

      var parser = new htmlparser.Parser({

        onopentag: function (name, attribs) {

          if (name === 'exporttitle') {
            currentParagraph = new docx.Paragraph();
            currentParagraph.center();
            currentParagraph.spacing(exportitlespacing);
            boldActive = true;
github andreafeccomandi / bibisco / bibisco / app / services / DocxExporterService.js View on Github external
onopentag: function (name, attribs) {

          if (name === 'exporttitle') {
            currentParagraph = new docx.Paragraph();
            currentParagraph.center();
            currentParagraph.spacing(exportitlespacing);
            boldActive = true;

          } else if (name === 'exportsubtitle') {
            currentParagraph = new docx.Paragraph();
            currentParagraph.center();
            boldActive = true;

          } else if (name === 'h1') {
            h1counter += 1;
            currentParagraph = new docx.Paragraph();
            currentParagraph.pageBreak();
            boldActive = true;
            let currentText = new docx.TextRun(h1counter + ' ');
            currentText.size(fontSize);
            currentText.font(font);
            currentText.bold();
            currentParagraph.addRun(currentText);
            currentParagraph.spacing(h1marginspacing);

          } else if (name === 'h2') {
            h2counter += 1;
            currentParagraph = new docx.Paragraph();
            italicsActive = true;
            let currentText = new docx.TextRun(h1counter + '.' + h2counter + ' ');
            currentText.size(fontSize);
            currentText.font(font);
github andreafeccomandi / bibisco / bibisco / app / services / DocxExporterService.js View on Github external
} else if (name === 'h1') {
            h1counter += 1;
            currentParagraph = new docx.Paragraph();
            currentParagraph.pageBreak();
            boldActive = true;
            let currentText = new docx.TextRun(h1counter + ' ');
            currentText.size(fontSize);
            currentText.font(font);
            currentText.bold();
            currentParagraph.addRun(currentText);
            currentParagraph.spacing(h1marginspacing);

          } else if (name === 'h2') {
            h2counter += 1;
            currentParagraph = new docx.Paragraph();
            italicsActive = true;
            let currentText = new docx.TextRun(h1counter + '.' + h2counter + ' ');
            currentText.size(fontSize);
            currentText.font(font);
            currentText.italic();
            currentParagraph.addRun(currentText);
            currentParagraph.spacing(h2counter ===1 ? h2marginspacing1st : h2marginspacing);

          } else if (name === 'h3') {
            h3counter += 1;
            currentParagraph = new docx.Paragraph();
            let currentText = new docx.TextRun(h1counter + '.' + h2counter + '.' + h3counter + ' ');
            currentText.size(fontSize);
            currentText.font(font);
            currentParagraph.addRun(currentText);
            currentParagraph.spacing(h3counter === 1 ? h3marginspacing1st : h3marginspacing);
github andreafeccomandi / bibisco / bibisco / app / services / DocxExporterService.js View on Github external
onopentag: function (name, attribs) {

          if (name === 'exporttitle') {
            currentParagraph = new docx.Paragraph();
            currentParagraph.center();
            currentParagraph.spacing(exportitlespacing);
            boldActive = true;

          } else if (name === 'exportsubtitle') {
            currentParagraph = new docx.Paragraph();
            currentParagraph.center();
            boldActive = true;

          } else if (name === 'h1') {
            h1counter += 1;
            currentParagraph = new docx.Paragraph();
            currentParagraph.pageBreak();
            boldActive = true;
            let currentText = new docx.TextRun(h1counter + ' ');
            currentText.size(fontSize);
            currentText.font(font);
            currentText.bold();
            currentParagraph.addRun(currentText);
            currentParagraph.spacing(h1marginspacing);

          } else if (name === 'h2') {
github andreafeccomandi / bibisco / bibisco / app / services / DocxExporterService.js View on Github external
onopentag: function (name, attribs) {

          if (name === 'exporttitle') {
            currentParagraph = new docx.Paragraph();
            currentParagraph.center();
            currentParagraph.spacing(exportitlespacing);
            boldActive = true;

          } else if (name === 'exportsubtitle') {
            currentParagraph = new docx.Paragraph();
            currentParagraph.center();
            boldActive = true;

          } else if (name === 'h1') {
            h1counter += 1;
            currentParagraph = new docx.Paragraph();
            currentParagraph.pageBreak();
            boldActive = true;
            let currentText = new docx.TextRun(h1counter + ' ');
            currentText.size(fontSize);
github andreafeccomandi / bibisco / bibisco / app / services / DocxExporterService.js View on Github external
ontext: function (text) {

          const NOT_SAFE_IN_XML_1_0 = /[^\x09\x0A\x0D\x20-\xFF\x85\xA0-\uD7FF\uE000-\uFDCF\uFDE0-\uFFFD]/gm;
          let sanitizedText = text.replace(NOT_SAFE_IN_XML_1_0, '');
          if (currentParagraph) {
            let currentText = new docx.TextRun(sanitizedText);
            currentText.size(fontSize);
            currentText.font(font);
 
            if (boldActive) {
              currentText.bold();
            }
            if(italicsActive) {
              currentText.italic();
            }
            if (underlineActive) {
              currentText.underline();
            }
            if (strikeActive) {
              currentText.strike();
            }
            currentParagraph.addRun(currentText);
github andreafeccomandi / bibisco / bibisco / app / services / DocxExporterService.js View on Github external
h1counter += 1;
            currentParagraph = new docx.Paragraph();
            currentParagraph.pageBreak();
            boldActive = true;
            let currentText = new docx.TextRun(h1counter + ' ');
            currentText.size(fontSize);
            currentText.font(font);
            currentText.bold();
            currentParagraph.addRun(currentText);
            currentParagraph.spacing(h1marginspacing);

          } else if (name === 'h2') {
            h2counter += 1;
            currentParagraph = new docx.Paragraph();
            italicsActive = true;
            let currentText = new docx.TextRun(h1counter + '.' + h2counter + ' ');
            currentText.size(fontSize);
            currentText.font(font);
            currentText.italic();
            currentParagraph.addRun(currentText);
            currentParagraph.spacing(h2counter ===1 ? h2marginspacing1st : h2marginspacing);

          } else if (name === 'h3') {
            h3counter += 1;
            currentParagraph = new docx.Paragraph();
            let currentText = new docx.TextRun(h1counter + '.' + h2counter + '.' + h3counter + ' ');
            currentText.size(fontSize);
            currentText.font(font);
            currentParagraph.addRun(currentText);
            currentParagraph.spacing(h3counter === 1 ? h3marginspacing1st : h3marginspacing);

          } else if (name === 'question') {
github andreafeccomandi / bibisco / bibisco / app / services / DocxExporterService.js View on Github external
currentParagraph = new docx.Paragraph();
            currentParagraph.center();
            currentParagraph.spacing(exportitlespacing);
            boldActive = true;

          } else if (name === 'exportsubtitle') {
            currentParagraph = new docx.Paragraph();
            currentParagraph.center();
            boldActive = true;

          } else if (name === 'h1') {
            h1counter += 1;
            currentParagraph = new docx.Paragraph();
            currentParagraph.pageBreak();
            boldActive = true;
            let currentText = new docx.TextRun(h1counter + ' ');
            currentText.size(fontSize);
            currentText.font(font);
            currentText.bold();
            currentParagraph.addRun(currentText);
            currentParagraph.spacing(h1marginspacing);

          } else if (name === 'h2') {
            h2counter += 1;
            currentParagraph = new docx.Paragraph();
            italicsActive = true;
            let currentText = new docx.TextRun(h1counter + '.' + h2counter + ' ');
            currentText.size(fontSize);
            currentText.font(font);
            currentText.italic();
            currentParagraph.addRun(currentText);
            currentParagraph.spacing(h2counter ===1 ? h2marginspacing1st : h2marginspacing);

docx

Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.

MIT
Latest version published 14 days ago

Package Health Score

85 / 100
Full package analysis