Added in the ability to create borders for paragraphs

This commit is contained in:
Noah Schechter
2018-08-19 11:59:38 -04:00
parent 7b43551148
commit 0411153832
5 changed files with 89 additions and 19 deletions

View File

@ -135,7 +135,7 @@ class DocumentCreator {
for (const education of educations) {
document.addParagraph(
this.createInstitutionHeader(education.schoolName, `${education.startDate.year} - ${education.endDate.year}`),
this.createInstitutionHeader(education.schoolName, `${education.startDate.year} - ${education.endDate.year}`)
);
document.addParagraph(this.createRoleText(`${education.fieldOfStudy} - ${education.degree}`));
@ -151,8 +151,8 @@ class DocumentCreator {
document.addParagraph(
this.createInstitutionHeader(
position.company.name,
this.createPositionDateText(position.startDate, position.endDate, position.isCurrent),
),
this.createPositionDateText(position.startDate, position.endDate, position.isCurrent)
)
);
document.addParagraph(this.createRoleText(position.title));
@ -182,14 +182,14 @@ class DocumentCreator {
document.addParagraph(
new docx.Paragraph(
"Dr. Dean Mohamedally Director of Postgraduate Studies Department of Computer Science, University College London Malet Place, Bloomsbury, London WC1E d.mohamedally@ucl.ac.uk",
),
"Dr. Dean Mohamedally Director of Postgraduate Studies Department of Computer Science, University College London Malet Place, Bloomsbury, London WC1E d.mohamedally@ucl.ac.uk"
)
);
document.addParagraph(new docx.Paragraph("More references upon request"));
document.addParagraph(
new docx.Paragraph(
"This CV was generated in real-time based on my Linked-In profile from my personal website www.dolan.bio.",
).center(),
"This CV was generated in real-time based on my Linked-In profile from my personal website www.dolan.bio."
).center()
);
return document;
}

23
demo/demo26.js Normal file
View File

@ -0,0 +1,23 @@
/*
* Creates two paragraphs, one with a border and one without
*/
const docx = require("../build");
let doc = new docx.Document();
let paragraph = new docx.Paragraph("No border!");
doc.addParagraph(paragraph);
let borderParagraph = new docx.Paragraph("I have a border on all but one side!");
console.log(borderParagraph.Borders);
borderParagraph.Borders.addTopBorder();
borderParagraph.Borders.addBottomBorder();
borderParagraph.Borders.addLeftBorder();
console.log(borderParagraph.Borders);
doc.addParagraph(borderParagraph);
let exporter = new docx.LocalPacker(doc);
exporter.packPdf('My Document');

View File

@ -1,23 +1,61 @@
// http://officeopenxml.com/WPborders.php
import { Attributes, XmlComponent } from "file/xml-components";
class Border extends XmlComponent {
class BorderProperty extends XmlComponent {
public setProperties(space: string, value: string, size: string): XmlComponent {
const attrs = new Attributes({
space: space,
val: value,
sz: size
});
this.root.push(attrs);
return this;
}
}
export class Border extends XmlComponent {
constructor() {
super("w:bottom");
this.root.push(
new Attributes({
color: "auto",
space: "1",
val: "single",
sz: "6",
}),
);
super("w:pBdr");
}
public addTopBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
const top = new BorderProperty("w:top");
top.setProperties(space, value, size);
this.root.push(top);
return this;
}
public addBottomBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
const bottom = new BorderProperty("w:bottom");
bottom.setProperties(space, value, size);
this.root.push(bottom);
return this;
}
public addLeftBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
const left = new BorderProperty("w:left");
left.setProperties(space, value, size);
this.root.push(left);
return this;
}
public addRightBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
const right = new BorderProperty("w:right");
right.setProperties(space, value, size);
this.root.push(right);
return this;
}
}
export class ThematicBreak extends XmlComponent {
constructor() {
super("w:pBdr");
this.root.push(new Border());
const bottom = new BorderProperty("w:bottom");
this.root.push(bottom);
}
}

View File

@ -6,7 +6,7 @@ import { XmlComponent } from "file/xml-components";
import { Alignment } from "./formatting/alignment";
import { Bidirectional } from "./formatting/bidirectional";
import { ThematicBreak } from "./formatting/border";
import { ThematicBreak, Border } from "./formatting/border";
import { Indent } from "./formatting/indent";
import { KeepLines, KeepNext } from "./formatting/keep";
import { PageBreak, PageBreakBefore } from "./formatting/page-break";
@ -30,6 +30,10 @@ export class Paragraph extends XmlComponent {
}
}
public get Borders(): Border {
return this.properties.paragraphBorder;
}
public addRun(run: Run): Paragraph {
this.root.push(run);
return this;

View File

@ -1,9 +1,14 @@
// http://officeopenxml.com/WPparagraphProperties.php
import { XmlComponent } from "file/xml-components";
import { Border } from "./formatting/border";
export class ParagraphProperties extends XmlComponent {
public paragraphBorder: Border;
constructor() {
super("w:pPr");
this.paragraphBorder = new Border();
this.push(this.paragraphBorder);
}
public push(item: XmlComponent): void {