Files
docx-js/demo/49-table-borders.ts
Firat Ciftci 7d1129900f Fix: separate vertical alignment enums for ITableCellOptions and ISectionPropertiesOptions (#3079)
* fix: separate vertical alignment enums for table and section properties

- Introduced `VerticalAlignTable` for table-cell vertical alignment with valid values: `top`, `center`, and `bottom`.
- Added `VerticalAlignSection` for section properties, extending `VerticalAlignTable` with an additional value `both`.
- Marked the original `VerticalAlign` as deprecated for backward compatibility, directing users to the new enums.
- Updated type definitions for better clarity on valid vertical alignments.

* refactor: update vertical alignment imports and types for section and table properties

- Renamed `VerticalAlign` to `VerticalAlignSection` in section properties and `VerticalAlignTable` in table-cell properties for clarity.
- Updated type definitions to reflect the new enum names, ensuring better organization and understanding of vertical alignment options.
- Adjusted related test cases to utilize the new imports and types.

* refactor: update demos to use new enums for table and section properties for vertical alignment
2025-05-02 19:58:10 +01:00

167 lines
5.0 KiB
TypeScript

// Add custom borders and no-borders to the table itself
import * as fs from "fs";
import {
BorderStyle,
Document,
HeadingLevel,
Packer,
Paragraph,
Table,
TableBorders,
TableCell,
TableRow,
TextDirection,
VerticalAlignTable,
} from "docx";
const table = new Table({
rows: [
new TableRow({
children: [
new TableCell({
borders: {
top: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "ff0000",
},
bottom: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "ff0000",
},
left: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "ff0000",
},
right: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "ff0000",
},
},
children: [new Paragraph("Hello")],
}),
new TableCell({
children: [],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [],
}),
new TableCell({
children: [new Paragraph("World")],
}),
],
}),
],
});
// Using the no-border convenience object. It is the same as writing this manually:
// const borders = {
// top: {
// style: BorderStyle.NONE,
// size: 0,
// color: "auto",
// },
// bottom: {
// style: BorderStyle.NONE,
// size: 0,
// color: "auto",
// },
// left: {
// style: BorderStyle.NONE,
// size: 0,
// color: "auto",
// },
// right: {
// style: BorderStyle.NONE,
// size: 0,
// color: "auto",
// },
// insideHorizontal: {
// style: BorderStyle.NONE,
// size: 0,
// color: "auto",
// },
// insideVertical: {
// style: BorderStyle.NONE,
// size: 0,
// color: "auto",
// },
// };
const noBorderTable = new Table({
borders: TableBorders.NONE,
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph({}), new Paragraph({})],
verticalAlign: VerticalAlignTable.CENTER,
}),
new TableCell({
children: [new Paragraph({}), new Paragraph({})],
verticalAlign: VerticalAlignTable.CENTER,
}),
new TableCell({
children: [new Paragraph({ text: "bottom to top" }), new Paragraph({})],
textDirection: TextDirection.BOTTOM_TO_TOP_LEFT_TO_RIGHT,
}),
new TableCell({
children: [new Paragraph({ text: "top to bottom" }), new Paragraph({})],
textDirection: TextDirection.TOP_TO_BOTTOM_RIGHT_TO_LEFT,
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [
new Paragraph({
text: "Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah",
heading: HeadingLevel.HEADING_1,
}),
],
}),
new TableCell({
children: [
new Paragraph({
text: "This text should be in the middle of the cell",
}),
],
verticalAlign: VerticalAlignTable.CENTER,
}),
new TableCell({
children: [
new Paragraph({
text: "Text above should be vertical from bottom to top",
}),
],
verticalAlign: VerticalAlignTable.CENTER,
}),
new TableCell({
children: [
new Paragraph({
text: "Text above should be vertical from top to bottom",
}),
],
verticalAlign: VerticalAlignTable.CENTER,
}),
],
}),
],
});
const doc = new Document({
sections: [{ children: [table, new Paragraph("Hello"), noBorderTable] }],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});