Birch UI v0.1
Pricing

Tables and imagesDocument

Insert and edit tables with merged cells, header rows, shading and borders, and add pictures from files, the clipboard or drag and drop in Birch UI Document Editor.

Open .md

Tables#

The table button opens a grid picker for tables up to 8 × 8, and Insert table… asks for any size up to 100 rows and 30 columns. From code:

TypeScript
editor.insertTable(4, 3); // rows, columns
editor.exec('toggleHeaderRow');
editor.exec('cellBackground', '#e2efda');
editor.exec('tableBorders', 'outer'); // 'all' | 'outer' | 'none'

When the cursor is in a table, the same button lists the table commands. They are also in the context menu:

Command Effect
addRowBefore, addRowAfter Insert a row above or below.
addColumnBefore, addColumnAfter Insert a column to the left or right.
deleteRow, deleteColumn, deleteTable Remove the row, column or table.
mergeCells, splitCell Merge a rectangular cell selection, or split a merged cell.
toggleHeaderRow Turn the first row into a header row. Header rows repeat at the top of every page the table spans and export as Word's "Repeat as header row".
cellBackground Shade the selected cells; null removes shading.
tableBorders All borders, outside borders only, or none.

Drag a column edge to resize the column and the bottom border of a row to set the height of the row. With the cursor in a table a small grip appears at the bottom-right corner of the table: drag it to resize the whole table, or focus it and press the arrow keys (8 px a step, 24 px with Shift). Tab and Shift+Tab move between cells, and Tab in the last cell adds a row. Drag across cells to select a block of them.

Drawing and erasing borders#

Table Design has a border pen (Pen style, Pen weight and a pen colour) with two tools, Draw borders and Eraser. With a tool on, drag along cell edges to paint them with the pen or to remove them; Escape or a second click on the button leaves the tool.

TypeScript
editor.exec('drawBorders', { style: 'dashed', width: 1.5, color: '#2f5496' }); // set the pen
editor.exec('tableTool', 'draw'); // 'draw' | 'erase'; the same value again, or any other value, turns the tool off
editor.exec('paintCellEdges', { edges: [{ pos: 148, side: 'bottom' }] }); // paint edges with the pen
editor.exec('paintCellEdges', { edges: [{ pos: 148, side: 'bottom' }], erase: true });

The pen style is 'single', 'dashed', 'dotted' or 'double', width is 0.25 to 6 points and color is a #rrggbb value; the pen starts as a 0.75 pt black single line. In paintCellEdges, pos is the document position of a cell and side is 'top', 'right', 'bottom' or 'left'. The edge shared with the neighbouring cell is painted on both cells. Both tools are off in a read-only editor.

Cells can hold any block content: paragraphs, lists and nested tables. In the document JSON a table is a table node with table_row children and table_cell or table_header cells. Cell attributes are colspan, rowspan, colwidth (pixels per spanned column), background and verticalAlign ('top' | 'middle' | 'bottom').

In page layout, tables split between rows when they reach the bottom of a page. A single row taller than a page is not split.

Images#

Pictures can be inserted from the toolbar's file picker, pasted from the clipboard or dropped onto the page. By default the editor reads the file into a data: URL, so the picture travels inside the document and the DOCX file without any server. To store pictures elsewhere, provide uploadImage; whatever URL it resolves to is used as the picture source:

TypeScript
const editor = createDocumentEditor(host, {
  uploadImage: async (file) => {
    const body = new FormData();
    body.append('file', file);
    const response = await fetch('/api/attachments', { method: 'POST', body });
    return (await response.json()).url;
  },
});

From code, insertImage(src, { alt, width, height }) inserts a picture at the cursor. Select a picture and drag the handle at its bottom-right corner to resize it; the aspect ratio is kept. With a picture selected:

TypeScript
editor.exec('imageAlt', 'Signed company seal');
editor.exec('imageSize', { width: 160, height: 160 });
editor.exec('imageSize', { width: null, height: null }); // natural size
editor.exec('alignCenter'); // pictures are aligned with their paragraph

Right-click a picture, or press Shift+F10, for Alt text and Reset size. With the cursor in a table the ribbon shows Table Design (style gallery, header/total/banded options, borders, shading) and Table Layout (insert and delete, merge and split, cell size, alignment, sort, formula). The same actions are commands: tableStyle, tableStyleOptions, cellBorders, cellShading, tableProperties, sortTable, insertFormula (=SUM(ABOVE), cell references such as =B2*C2), textToTable and tableToText. Formulas are stored as fields and recalculated by updateFields() or F9.

A selected picture shows the Picture tab. imageWrap sets 'inline' | 'square' | 'tight' | 'through' | 'topBottom' | 'behind' | 'inFront', or takes { wrap, side, wrapSide }: side ('left' | 'right' | 'center') aligns the picture in the column and wrapSide ('bothSides' | 'left' | 'right' | 'largest') says where the text may go. imagePosition takes a preset such as 'topRight', and imageRotate, imageFlip, imageCrop (percentages per side or an aspect ratio such as '16:9'), imageReset, imageReplace and imageCompress change the picture without touching the original file until it is compressed. Arrow keys nudge a floating picture and Shift+arrow keys resize it. Wrap, position, crop, rotation and flip are read from and written to DOCX.

Where pictures of a DOCX file stand#

scripts/docx-image-placements.py builds fifteen documents with flat-coloured pictures in every placement, and test/fixtures/docx/images/lo-positions.json holds where LibreOffice 26.8 draws each of them on a page at 96 dpi. document-images.spec.ts opens the documents and compares every picture.

Document Pictures within 2 px in Chrome at 100%
01 inline: start, middle and end of a line, centred, right-aligned, wider than the column 5 of 6
02 square wrapping, with a paragraph that splits at the foot of the page 3 of 3
03 tight and through wrapping 3 of 3
04 top and bottom wrapping 3 of 3
05 behind and in front of the text 3 of 3
06 text on one side only 3 of 3
07 in table cells 7 of 7
08 header and footer, with a header taller than the margin 9 of 9
09 anchored to the page, the margin and the paragraph 5 of 5
10 cropped 6 of 6
11 rotated and flipped 3 of 3
12 150 pictures on 75 pages (first three pages compared) 6 of 6
13 EMF and WMF, 14 SVG with a PNG fallback, 15 linked and missing 6 of 6
16 two photographs of 5 and 6 MB 2 of 2
All 64 of 65
  • A picture whose width and height have another ratio than its file is stretched to that size, as in Word.
  • An inline picture stands on the baseline of its line. Under a paragraph that holds nothing but pictures there is as much room as the line spacing adds to a line of text: none with single spacing, about 2.5 px at 1.15 and 11 pt.
  • An inline picture with a size wider than the column reaches into the margin, as in Word; in a table cell it is narrowed to the cell.
  • A rotated inline picture takes the room of its turned outline.
  • A paragraph with a picture beside its text splits between its lines at the foot of a page.
  • A header or footer taller than the margin moves the text of the page.
  • In a document in the compatibility mode of Word 2010 or older, tables stand left of the margin by the cell margin.
  • Sizes and positions scale with the zoom.
  • Firefox breaks lines of text at slightly different heights, so a picture further down a page can stand up to 10 px higher than in Chrome; sizes and horizontal positions are the same.

Text on both sides of a picture#

With square, tight or through wrapping, text flows on both sides of a picture or shape that is centred, or that stands at least 72 px from the start of the column without a horizontal alignment. A picture aligned left or right has text on the other side only. The editor lays these lines out itself:

  • wrapSide limits the text to one side: 'left', 'right', or 'largest' for the wider one. 'bothSides' is the default.
  • A side narrower than 72 px is left empty, as in Word.
  • tight and through follow the wrap polygon of a DOCX file (ImageAnchor.wrapPolygon) when it has one, and the rectangle of the object otherwise.
TypeScript
editor.exec('imageWrap', { wrap: 'square', side: 'center' }); // text on both sides
editor.exec('imageWrap', { wrap: 'tight', wrapSide: 'largest' });

Text wraps on both sides in left-aligned, centred, right-aligned, justified and right-to-left paragraphs. Centred text is centred in the room on each side, right-aligned text ends at the object and at the margin, and a right-to-left paragraph starts on the right of the object. An object anchored in a table cell is wrapped by the text of that cell. An object with a negative vertical offset, or with a position measured from the top of the page or margin, can stand above the paragraph that holds its anchor, and the paragraphs above make room for it. When the paragraph beside an object continues on the next page, the lines there run the full width. The lines beside the object are not justified and the web layout uses a plain float with text on one side. In a comparison with LibreOffice on 24 documents with floating objects, 968 of 975 lines beside an object start at the same word in Chrome.

Picture border and shadow#

Picture border on the Picture tab offers thin, medium and thick lines, a dashed line, a shadow and No border. From code:

TypeScript
editor.exec('imageBorder', { width: 1.5, color: '#2f5496', style: 'dashed', shadow: true });
editor.exec('imageBorder', { shadow: false }); // change one property, keep the others
editor.exec('imageBorder', null); // remove the border and the shadow

width is in points (up to 12), color is #rrggbb and style is 'solid', 'dashed' or 'dotted' (the ImageBorder type). The border is saved in DOCX as the outline of the picture (a:ln) and the shadow as a:outerShdw.

Picture effects#

The Picture tab has Corrections (brightness and contrast in steps of 20%), Colour (greyscale, sepia, washout, black and white) and Picture styles (rounded corners, oval, soft edges, drop shadow, simple frame, black line). Reset picture removes them.

TypeScript
editor.exec('imageEffects', { brightness: 20, contrast: -20 }); // -100 to 100
editor.exec('imageEffects', { recolor: 'sepia', shape: 'rounded' }); // shape: 'rounded' | 'oval' | 'softEdge'
editor.exec('imageEffects', { recolor: null }); // remove one effect
editor.exec('imageEffects', null); // remove all

The effects are the effects attribute of the picture (ImageEffects). On screen, in print and in HTML export they are drawn with CSS filters; the picture file itself is not changed. In DOCX they are DrawingML: a:lum for brightness and contrast, a:grayscl, a:duotone (sepia) and a:biLevel (black and white) in the picture fill, a:prstGeom with roundRect or ellipse for the shape, and a:softEdge in the effect list. The same elements are read from a Word file.

Charts#

Insert → Chart opens a dialog with the chart type (column, bar, line, area, pie, doughnut or scatter), a title, the legend position and the data as rows of cells, with a preview that follows what you type. The chart is drawn by the editor as SVG and saved in the DOCX file the way Word saves it: a chart part (word/charts/chart1.xml) with the cached values and an embedded workbook that holds the data, so Word opens it as a chart and Edit Data works there. It is not saved as a picture.

TypeScript
import type { ChartModel } from '@birchui/document';

const chart: ChartModel = {
  type: 'column',
  title: 'Pallets moved',
  categories: ['Mon', 'Tue', 'Wed'],
  series: [
    { name: 'Inbound', values: [410, 388, 470] },
    { name: 'Outbound', values: [395, 402, 455], color: '#ed7d31' },
  ],
  stacking: 'stacked', // or 'percent'; leave out for side by side
  legend: 'right', // 'top', 'none'; leave out for below
};

editor.exec('insertChart', chart); // without a value it opens the dialog
editor.exec('editChart', { ...chart, type: 'line' }); // with a chart selected; without a value, or with Enter, the dialog

A chart in a Word file is read from its chart part: type, title, axis titles, legend, categories, series, values and series colours. It is drawn from that data and can be edited in the same dialog. A chart that is not edited is written back byte for byte, with its own workbook and formatting. A chart that is edited is written as a new chart part and workbook from the model, in the frame (size, position, wrapping) it had; formatting that the model does not hold, such as data labels, gap widths, trend lines and number formats, is not carried over. Charts with more than one plot type, and radar, stock, surface, bubble and the newer chart kinds (sunburst, waterfall and so on), are not read: they stay preserved drawings, shown as their fallback picture or a placeholder, and are written back unchanged.

renderChartSvg(model, width, height), normalizeChart(value) and describeChart(model) are exported for use without an editor. HTML export, print and PDF export show the drawn chart; Markdown and plain text write its description.

Charts from your own chart library#

With a chartProvider the Chart button asks your code for a picture of the chart instead of opening the built-in dialog:

TypeScript
import { createDocumentEditor, type ChartProvider, type ChartResult } from '@birchui/document';

declare function openChartDesigner(data?: unknown): Promise<ChartResult | null>;

const chartProvider: ChartProvider = {
  create: () => openChartDesigner(), // resolve to { src, width?, height?, alt?, data? } or null to cancel
  edit: (data) => openChartDesigner(data), // receives the data you returned earlier
};

const editor = createDocumentEditor(host, { chartProvider });
editor.exec('insertChart');
editor.exec('editChart'); // with a chart picture selected; Enter does the same

src must be a data:image/… or http(s) URL. data is whatever your designer needs to reopen the chart; it is kept with the picture in the document JSON only. A DOCX file keeps the picture, so after export and import the chart is an ordinary picture and editChart returns false. Errors thrown by the provider are logged with console.error.

Drawing with a pen#

The Draw tab has a Pen, a Highlighter and an Eraser, a colour list and a thickness list. With a tool on, drag on the page to draw; each stroke is its own object, anchored to the paragraph where it starts and placed in front of the text, so it moves with that paragraph. The eraser removes every stroke it is dragged over, Erase all ink removes them all, and Esc leaves drawing. Each stroke and each erasure is one undo step. A selected stroke can be moved with the mouse or the arrow keys and deleted with Delete.

TypeScript
editor.setDrawTool('pen'); // 'highlighter', 'eraser' or null
editor.exec('inkColor', '#0b57d0');
editor.exec('inkWidth', 4);

// Without a pointer: points in pixels from the top left of the paragraph that holds the cursor.
editor.insertInk({ tool: 'pen', color: '#c00000', width: 3, points: [[20, 30], [80, 5], [140, 40]] });
editor.getInk(); // [{ pos, stroke: { tool, color, width, points }, width, height }]
editor.eraseInk(); // every stroke, or eraseInk(pos) for one

A stroke is saved in DOCX as a freeform shape (a:custGeom) with a round line, and a highlighter stroke with a line at half opacity, so Word and LibreOffice show it and can move or delete it, and the editor reads it back as ink with its tool, colour, width and points. The points are thinned when the stroke is finished: a point that lies within 0.6 px of the line between its neighbours is dropped.

Shapes, notes, references and forms#

TypeScript
editor.exec('insertShape', { shape: 'rect', width: 120, height: 60, fill: '#eef3fb', stroke: '#2f5496', text: 'Approved' });
editor.exec('insertTextBox');
editor.exec('shapeStyle', { fill: '#fff4ce', strokeWidth: 1.5, wrap: 'square' }); // the selected shape
editor.exec('insertEquation', 'x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}'); // LaTeX; without a value the equation dialog opens

insertShape (rect, roundRect, ellipse, line, arrow, callout) and insertTextBox add drawings that are saved as Word shapes. A selected shape shows resize and rotate handles, arrow keys move a floating shape, Shift+arrow keys resize it and Enter edits its text. Equations are typed as LaTeX and saved as Office Math. An equation from a Word file is converted to LaTeX, rendered and editable when its Office Math uses only structures the converter knows: fractions, superscripts and subscripts, radicals, n-ary operators such as sums and integrals, delimiters, functions, accents, bars, limits, matrices and equation arrays. An equation with any other structure (for example a border box or an underbrace) is drawn from its parts and opens in the dialog as Office Math XML: you edit the XML itself, the preview follows, and the structure stays in the file. editor.exec('insertEquation', { omml }) does the same from code; it accepts one m:oMath or m:oMathPara element and nothing else.

The equation dialog has a Structures gallery above the samples: fraction, superscript, subscript, square root, root of a degree, integral, sum and matrix. A structure is inserted at the cursor in the LaTeX box with its first placeholder selected, or wrapped around the selected text. Floating shapes and pictures are placed by the editor; see Text on both sides of a picture and Limitations. insertFootnote, insertEndnote, insertBookmark, insertCrossReference, insertCaption, insertTableOfFigures and insertEquation (LaTeX, saved as Office Math) cover references; getFootnotes(), getBookmarks() and goToBookmark(name) read them.

insertContentControl adds plain text, rich text, drop-down, combo box, date, check box, picture and repeating section controls. formMode: true (or setFormMode(true)) lets users edit only inside controls and move between them with Tab; getFormData(), setFormData(data) and validateForm() work with the control tags and formChange reports edits. A document's protection (readOnly, comments, trackedChanges, forms) is enforced when it is opened and can be changed with exec('restrictEditing', mode).

↑ ↓ to move, Enter to open, Esc to close