mirror of
https://github.com/dolanmiu/docx.git
synced 2026-12-01 16:55:59 +01:00
* docs: point text frame users to ShapeRun text boxes Text frames are positioned paragraphs (w:framePr), not the text box Word makes when you insert one, which is a shape with text in it. The text box page still recommended frames for most uses. Explain the difference on the text frames page, with a floating ShapeRun example, and recommend ShapeRun on the text box page. Closes #866 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0121RHRfmgN8jDksNAdAWKnK * docs: say how Google Docs shows text frames Checked by converting a test document in Google Docs: the frame stays in line, full width, and a floating ShapeRun text box is placed where it should be. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0121RHRfmgN8jDksNAdAWKnK --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
208 lines
8.7 KiB
Markdown
208 lines
8.7 KiB
Markdown
# Text Frames
|
|
|
|
> Similar to `Text Boxes`!
|
|
|
|
!> Text Frames requires an understanding of [Paragraphs](usage/paragraph.md).
|
|
|
|
> Text frames are paragraphs of text in a document which are positioned in a separate region or frame in the document, and can be positioned with a specific size and position relative to non-frame paragraphs in the current document.
|
|
|
|
## Text Frames or Text Boxes
|
|
|
|
A text frame is a paragraph with a position, written as `w:framePr`. It is not the text box Word makes when you insert one: Word draws that as a shape with text in it. Not every application positions text frames: Google Docs leaves them in line with the paragraphs around them, as wide as the column.
|
|
|
|
For a text box like Word's, use a [`ShapeRun`](usage/shapes.md#text-in-shapes) from `docx/shapes`, with paragraphs in `children` and [`floating`](usage/shapes.md#inline-and-floating) to position it:
|
|
|
|
```ts live
|
|
import { Document, HorizontalPositionRelativeFrom, Paragraph, VerticalPositionRelativeFrom } from "docx";
|
|
import { ShapeRun } from "docx/shapes";
|
|
|
|
const doc = new Document({
|
|
sections: [
|
|
{
|
|
children: [
|
|
new Paragraph({
|
|
children: [
|
|
new ShapeRun({
|
|
type: "rectangle",
|
|
transformation: { width: 200, height: 80 },
|
|
children: [new Paragraph("Hello from a text box!")],
|
|
floating: {
|
|
horizontalPosition: { relative: HorizontalPositionRelativeFrom.MARGIN, offset: 914400 },
|
|
verticalPosition: { relative: VerticalPositionRelativeFrom.MARGIN, offset: 1828800 },
|
|
},
|
|
}),
|
|
],
|
|
}),
|
|
],
|
|
},
|
|
],
|
|
});
|
|
```
|
|
|
|
Use a text frame for a drop cap, or to position paragraphs without a shape around them.
|
|
|
|
## Overview
|
|
|
|
To make a `Text Frame`, add the `frame` property on a paragraph. Frames support two positioning modes:
|
|
|
|
- **Absolute** (`type: "absolute"`) — position using exact X/Y coordinates in twips
|
|
- **Alignment** (`type: "alignment"`) — position using alignment values (left, center, right, top, bottom)
|
|
|
|
## Frame Options
|
|
|
|
| Property | Type | Notes | Description |
|
|
| ---------- | ----------------------------- | -------- | ------------------------------------------------------- |
|
|
| type | `"absolute"` \| `"alignment"` | Required | Positioning mode |
|
|
| width | `number` | Optional | Frame width in twips (sizes to content when omitted) |
|
|
| height | `number` | Optional | Frame height in twips (sizes to content when omitted) |
|
|
| anchor | `{ horizontal, vertical }` | Required | What the frame is anchored to (see Anchor Types below) |
|
|
| position | `{ x, y }` | Required | X/Y coordinates in twips (only when `type: "absolute"`) |
|
|
| alignment | `{ x, y }` | Required | Alignment values (only when `type: "alignment"`) |
|
|
| wrap | `FrameWrap` | Optional | Text wrapping behavior |
|
|
| space | `{ horizontal, vertical }` | Optional | Spacing between frame and surrounding text in twips |
|
|
| rule | `HeightRule` | Optional | How frame height is calculated |
|
|
| dropCap | `DropCapType` | Optional | Drop cap effect |
|
|
| lines | `number` | Optional | Number of lines for drop cap |
|
|
| anchorLock | `boolean` | Optional | Lock the anchor position |
|
|
|
|
### Anchor Types (`FrameAnchorType`)
|
|
|
|
| Value | Description |
|
|
| -------- | ---------------------------------- |
|
|
| `MARGIN` | Anchor relative to the page margin |
|
|
| `PAGE` | Anchor relative to the page edge |
|
|
| `TEXT` | Anchor relative to the text column |
|
|
|
|
### Wrap Types (`FrameWrap`)
|
|
|
|
| Value | Description |
|
|
| ------------ | --------------------------------------- |
|
|
| `AROUND` | Wrap text around the frame on all sides |
|
|
| `AUTO` | Automatic wrapping based on space |
|
|
| `NONE` | No text wrapping |
|
|
| `NOT_BESIDE` | Do not allow text beside the frame |
|
|
| `THROUGH` | Allow text to flow through the frame |
|
|
| `TIGHT` | Wrap text tightly around the frame |
|
|
|
|
### Drop Cap Types (`DropCapType`)
|
|
|
|
| Value | Description |
|
|
| -------- | ------------------------------------------- |
|
|
| `NONE` | No drop cap effect |
|
|
| `DROP` | Drop cap that drops into the paragraph text |
|
|
| `MARGIN` | Drop cap that extends into the margin |
|
|
|
|
## Absolute Positioning
|
|
|
|
Position a frame using exact coordinates relative to the anchor:
|
|
|
|
```ts live
|
|
import { BorderStyle, Document, FrameAnchorType, Paragraph, TextRun } from "docx";
|
|
|
|
const doc = new Document({
|
|
sections: [
|
|
{
|
|
children: [
|
|
new Paragraph({
|
|
frame: {
|
|
type: "absolute",
|
|
position: {
|
|
x: 1000,
|
|
y: 3000,
|
|
},
|
|
width: 4000,
|
|
height: 1000,
|
|
anchor: {
|
|
horizontal: FrameAnchorType.MARGIN,
|
|
vertical: FrameAnchorType.MARGIN,
|
|
},
|
|
},
|
|
border: {
|
|
top: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6 },
|
|
bottom: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6 },
|
|
left: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6 },
|
|
right: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6 },
|
|
},
|
|
children: [new TextRun("Hello World"), new TextRun({ text: "Foo Bar", bold: true })],
|
|
}),
|
|
],
|
|
},
|
|
],
|
|
});
|
|
```
|
|
|
|
## Alignment Positioning
|
|
|
|
Position a frame using alignment values instead of exact coordinates:
|
|
|
|
```ts live
|
|
import { Document, FrameAnchorType, FrameWrap, HorizontalPositionAlign, Paragraph, TextRun, VerticalPositionAlign } from "docx";
|
|
|
|
const doc = new Document({
|
|
sections: [
|
|
{
|
|
children: [
|
|
new Paragraph({
|
|
frame: {
|
|
type: "alignment",
|
|
alignment: {
|
|
x: HorizontalPositionAlign.CENTER,
|
|
y: VerticalPositionAlign.TOP,
|
|
},
|
|
width: 4000,
|
|
height: 1000,
|
|
anchor: {
|
|
horizontal: FrameAnchorType.PAGE,
|
|
vertical: FrameAnchorType.PAGE,
|
|
},
|
|
wrap: FrameWrap.AROUND,
|
|
},
|
|
children: [new TextRun("Centered at top of page")],
|
|
}),
|
|
],
|
|
},
|
|
],
|
|
});
|
|
```
|
|
|
|
## Drop Cap
|
|
|
|
Create a decorative large initial letter using the `dropCap` and `lines` properties:
|
|
|
|
```ts live
|
|
import { Document, DropCapType, FrameAnchorType, Paragraph, TextRun } from "docx";
|
|
|
|
const doc = new Document({
|
|
sections: [
|
|
{
|
|
children: [
|
|
new Paragraph({
|
|
frame: {
|
|
type: "absolute",
|
|
position: { x: 0, y: 0 },
|
|
width: 1440,
|
|
height: 1440,
|
|
anchor: {
|
|
horizontal: FrameAnchorType.TEXT,
|
|
vertical: FrameAnchorType.TEXT,
|
|
},
|
|
dropCap: DropCapType.DROP,
|
|
lines: 3,
|
|
},
|
|
children: [new TextRun({ text: "O", size: 72 })],
|
|
}),
|
|
new Paragraph(
|
|
"nce upon a time, there was a paragraph that began with a large letter. The letter dropped down three lines into the text, and the rest of the paragraph flowed around it, the way the first page of a chapter in a book often begins.",
|
|
),
|
|
],
|
|
},
|
|
],
|
|
});
|
|
```
|
|
|
|
## Demo
|
|
|
|
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/text-boxes/text-frame.ts ":include")
|
|
|
|
_Source: https://github.com/dolanmiu/docx/blob/master/demo/text-boxes/text-frame.ts_
|