Documents Letterheads
Letterheads
A letterhead is a picture the size of the page, behind the text of every page: the logo at the top, the address at the foot, a pattern across the sheet. In a DOCX it lives in the header, placed from the page and drawn under the text, which is how Word does it and how Rebodocs reads and writes it.
Documents that come with one keep it: it is drawn, printed, put in the PDF, and written back to the DOCX. Given one, the margins grow to clear what it prints at the top and the foot, the dashed lines of the sheet.
Give it a letterhead
A real editor with a letter. Give it one of three letterheads drawn on this page, as your application would give its own, and watch the margins; or open the menu and pick a picture of yours.
Opening the editor…
In the editor
The document menu (☰), under Page. Fit, extract and remove show only when there is a letterhead.
- Letterhead… · Change the letterhead…
- A PNG or JPG from the disk. The top and bottom margins grow to clear what it prints; the one there was goes. One step to undo.
- Fit the margins to the letterhead
- Does that again, after the margins were changed by hand.
- Extract the letterhead
- Saves its picture as a file, named after the document, to use on another one.
- Remove the letterhead
- Takes the picture away. What else the header has stays.
How margins are fitted
The picture is read pixel by pixel, at 320 across: where its printing ends at the top, and where it starts at the foot. Make the band and the address taller, or take them away.
Drawing the letterhead…
The tone there is most of. A row is printed when enough of it is far from that tone; what is see-through counts as white paper.
Printing that starts in the top fifth and ends before 35% of the page asks for the top margin; the same from the foot. What is in the middle asks for nothing.
The margins grow, never shrink, and never past 40% of the page. Side margins are left alone, and so is any text already in the header.
From your application
To give every document your letterhead, whatever it came with:
<DocsEditor docx={file} letterhead="/brand/letterhead.png" />letterhead is an address to fetch, a File or Blob, or { bytes, type }; null for none. A server that does not say what it sends is no trouble: the picture is told by how it starts. Set, it is yours: the menu does not offer it to the person. Left out, the document keeps its own, and the person may change it.
It is put once the document is open, and is how the document starts: not a step to undo, and the document is not left unsaved by it. A document that has the very same picture is left alone, so in a shared room the second person to come in does not put it again. Changing the prop later gives the new one as a change; the handle's setLetterhead(source | null) does the same.
Headless
const { letterhead, setLetterhead, fitLetterheadMargins, removeLetterhead, letterheadFile } = useDocsPage();
<input type="file" accept="image/png,image/jpeg" onChange={(e) => e.target.files?.[0] && setLetterhead(e.target.files[0])} />- letterhead: boolean
- Whether the document has one; also state.letterhead of useDocs.
- setLetterhead(file, fit?) → Promise<boolean>
- The editor’s setLetterheadFile.
- fitLetterheadMargins() → Promise<boolean>
- As on the editor.
- removeLetterhead()
- As on the editor.
- letterheadFile() → Blob | undefined
- Its picture as a file, for a button that saves it.
On the editor
- hasLetterhead: boolean
- Whether the document has one.
- letterhead: { bytes, type } | undefined
- Its picture, as the document keeps it: to save apart, or to put on another document.
- setLetterheadFile(file, fit = true) → Promise<boolean>
- Gives it the letterhead a PNG or JPG file is, and, unless fit is false, grows the margins to clear it: one step to undo. A file that does not say its kind is told by how it starts. False when it is not a picture that can be one.
- setLetterhead(bytes, type)
- The same from bytes, without touching the margins.
- fitLetterheadMargins() → Promise<boolean>
- Grows the top and bottom margins to clear what it prints; one that already clears it stays. False when nothing changed.
- removeLetterhead()
- Takes it away.
In code
On a server, to stamp a template before it is filled or sent.
import { exportDocx, importDocx, letterheadOf, setLetterhead } from '@rebodocs/core';
let doc = importDocx(template);
doc = setLetterhead(doc, { bytes: png, type: 'image/png' });
letterheadOf(doc); // { part: 'header', para: 0, offset: 0, xml }
const docx = exportDocx(doc);- setLetterhead(doc, { bytes, type }) → Doc
- The picture stretched over the whole page, in the header of every page, the first page’s own and that of each section with its own, each the size of its page; the one there was goes. A kind of picture a document cannot hold leaves it as it was. The margins are not touched.
- removeLetterhead(doc) → Doc
- Without it; the rest of the header stays.
- letterheadOf(doc) → { part, para, offset, xml } | undefined
- Where it is: the header and the place in it.
- letterheadImage(doc) → { bytes, type } | undefined
- Its picture.
- letterheadMargins(pixels, page) → { top?, bottom? }
- The margins a picture asks for, read from its pixels (RGBA, as a canvas gives them). A side with nothing printed asks for nothing.
- pngLetterheadMargins(bytes, page) → { top?, bottom? } | undefined
- The same from the bytes of a PNG, without a browser, as on a server. Undefined for a JPEG, whose pixels are not read here.
- withLetterheadMargins(doc, asked) → Doc
- The top and bottom margins grown to what was asked: never shrunk, never past 40% of the page. What the editor does after reading a picture.
- resizeLetterhead(doc, width, height, section?) → Doc
- The letterhead made the size of another page. setSectionPage does it when the paper or its orientation changes, so it is called by hand only for a page set some other way.
- imageTypeOf(bytes) → string | undefined
- image/png, image/jpeg or image/gif, by how the bytes start, for a file that did not say.
For an assistant
The MCP server has set_letterhead, and get_overview says when the document has one.
set_letterhead { "path": "/brand/norte-studio.png" } // the band and address above
→ Letterhead set, behind the text of every page. Top margin 3.9 cm, bottom 2.5 cm, to clear what it prints.The server reads the file, which is why it is not among the tools of an assistant in a page. For a PNG it reads the pixels and grows the margins as the editor does; for a JPEG it leaves them, and says so. "remove": true takes the letterhead away.
Checked against Word
A document of two sections, the first turned to landscape with a header of its own, was given a letterhead here and opened in Word for Mac. Word drew it under the text of both pages, each the size of its page, and put it in the PDF so. Saved by Word and read back here, it was still the letterhead of both headers.
What counts as a letterhead
A picture is the document’s letterhead when all four are true. Word has nothing called a letterhead; this is what a letterhead is in a file Word made.
In a header
The header of every page, the first page’s own, or that of a section with a header of its own.
Behind the text
Drawn under it, with the text not wrapping around it.
Measured from the page
Placed from the edges of the sheet, not from the margins or a paragraph.
The size of the page
At least 85% of the width and of the height of the page it is on. A smaller picture behind the text is a picture in the header, not the letterhead.
What to know
- The picture is stretched to the page: one of other proportions is distorted. Make it the shape of the paper.
- When a letterhead is replaced or removed, its picture leaves the file on export: it is not left inside the ZIP for whoever unpacks it.
- One letterhead for the whole document: every page, the first ones and the sections with headers of their own show the same picture, each stretched to its page. Turning the paper of a section turns its own.