Issue Summary
- You're inserting Word docs (as base64) into a Word context (new or existing).
Styles like font size and positioning are not correctly applied, especially in headers and footers.
Text boxes in headers/footers appear as shapes, not as content controls.
Using contentControls
and cannotEdit
didn’t help because text boxes inside headers/footers are being returned as shapes (not content controls).
You managed to adjust font styles by accessing Shapes
in headers/footers and editing their TextFrame
or body
, but this relies on a preview API, which is not stable for production.
Suggested Direction
- Use
context.document.sections.getHeader("Primary")
withshapes
Since you're already doing this, you’re on the right path. You may want to fully rely on shapes
within headers/footers and not expect them to be wrapped as contentControls
.
await Word.run(async (context) => {
const sections = context.document.sections;
context.load(sections, "items");
await context.sync();
const header = sections.items[0].getHeader("Primary");
const shapes = header.shapes;
shapes.load("items");
await context.sync();
shapes.items.forEach((shape) => {
const textBody = shape.textFrame.textRange;
textBody.font.size = 12;
textBody.font.name = "Calibri";
textBody.font.color = "black";
});
await context.sync();
});
- Alternative Option: Use Open XML on Backend
If you have full control over the backend (since you're generating the base64), you could manipulate the .docx
before sending it — using Open XML SDK to set exact fonts/styles, including for text boxes in headers/footers.
This way, you don’t rely on the client-side Office.js API to fix styles later.
- Use Office Scripts (if Excel, Word for Web)
If your frontend supports Office Scripts (Word for Web), they provide similar access to headers/footers/shapes — and may behave more predictably than preview APIs.
Final Thoughts
Text boxes in headers/footers are shapes — not content controls — so treating them as such is correct.
Styling shapes via Office.js preview is fragile, but your current workaround is smart.
Consider handling formatting at the backend (Open XML) if API stability is crucial.
Let me know if you want help with an Open XML snippet to pre-format the file, or if you want to structure your current logic better for shape traversal. Happy to help!Hi there! Thanks for the detailed explanation — it sounds like you’ve already done a lot of great work. Based on what you shared, here’s a summary and some suggestions to move forward:
Issue Summary
You're inserting Word docs (as base64) into a Word context (new or existing).
Styles like font size and positioning are not correctly applied, especially in headers and footers.
Text boxes in headers/footers appear as shapes, not as content controls.
Using contentControls
and cannotEdit
didn’t help because text boxes inside headers/footers are being returned as shapes (not content controls).
You managed to adjust font styles by accessing Shapes
in headers/footers and editing their TextFrame
or body
, but this relies on a preview API, which is not stable for production.
Suggested Direction
- Use
context.document.sections.getHeader("Primary")
withshapes
Since you're already doing this, you’re on the right path. You may want to fully rely on shapes
within headers/footers and not expect them to be wrapped as contentControls
.
await Word.run(async (context) => {
const sections = context.document.sections;
context.load(sections, "items");
await context.sync();
const header = sections.items[0].getHeader("Primary");
const shapes = header.shapes;
shapes.load("items");
await context.sync();
shapes.items.forEach((shape) => {
const textBody = shape.textFrame.textRange;
textBody.font.size = 12;
textBody.font.name = "Calibri";
textBody.font.color = "black";
});
await context.sync();
});
- Alternative Option: Use Open XML on Backend
If you have full control over the backend (since you're generating the base64), you could manipulate the .docx
before sending it — using Open XML SDK to set exact fonts/styles, including for text boxes in headers/footers.
This way, you don’t rely on the client-side Office.js API to fix styles later.
- Use Office Scripts (if Excel, Word for Web)
If your frontend supports Office Scripts (Word for Web), they provide similar access to headers/footers/shapes — and may behave more predictably than preview APIs.
Final Thoughts
Text boxes in headers/footers are shapes — not content controls — so treating them as such is correct.
Styling shapes via Office.js preview is fragile, but your current workaround is smart.
Consider handling formatting at the backend (Open XML) if API stability is crucial.
Let me know if you want help with an Open XML snippet to pre-format the file, or if you want to structure your current logic better for shape traversal. Happy to help!