Document Editing Problems

Insert a Generated QR Code Image with a Word Office.js Add-in

Amos GikundaAmos Gikunda Nov 22, 2026 868 views

Question details

Insert a QR code generated on a task pane canvas into a Word document so it displays correctly.

Product
Microsoft Word Office.js add-in
Device & OS
not provided
Scenario
A custom Word add-in generates a QR code on a canvas and inserts it at the document selection.
Observed behavior
The image does not display correctly when the add-in passes an HTML image containing a data URL to setSelectedDataAsync.
Before you start

Confirm that the QR code is fully rendered on the canvas and that the canvas has nonzero dimensions before exporting it. Check that your Word add-in supports the WordApi 1.1 requirement set.

Solution 1Recommended

Insert the canvas PNG directly with the Word JavaScript API

Use this approach for a custom add-in when inserting an HTML image data URL does not display reliably.

Export the canvas as a PNG, remove the data URL prefix, and pass the remaining Base64 string to Word's insertInlinePictureFromBase64 method. This avoids relying on HTML image data URL handling.

1
Export the QR code canvas

After the QR code finishes rendering, call canvas.toDataURL('image/png') and check that the result starts with 'data:image/png;base64,'. If the canvas uses cross-origin image content without permission, export may fail.

2
Insert the Base64 image

Place the insertion point in the Word document, then run the add-in action with code such as: const dataUrl = canvas.toDataURL('image/png'); const base64 = dataUrl.split(',')[1]; await Word.run(async (context) => { const range = context.document.getSelection(); range.insertInlinePictureFromBase64(base64, 'Replace'); await context.sync(); });

3
Check the inserted QR code

Return to the document and confirm that the image appears and scans correctly. If it does not, inspect the exported PNG and catch errors from Word.run and context.sync().

Selection behavior: The Replace option replaces selected content. Place a cursor without selecting content if you want to insert the image without replacing text.
Free Microsoft Office alternative

Edit Word Documents with WPS Office

WPS Office is a free, lightweight Office alternative with a familiar interface and Microsoft Office format compatibility for easier document migration. It does not fix Microsoft Word Office.js add-in behavior.

Insert existing QR code images into documentsOpen and edit Microsoft Word DOCX filesUse familiar document editing toolsMove between Office documents with compatible file formats
QA img-9

Frequently Asked Questions

Why might an HTML image data URL fail to display in Word?

Support for an image embedded as a data URL in HTML passed to setSelectedDataAsync can vary. Export the canvas as PNG and try Word's insertInlinePictureFromBase64 method instead.

Should I pass the full data URL to insertInlinePictureFromBase64?

No. Remove the 'data:image/png;base64,' prefix and pass only the Base64 image data to insertInlinePictureFromBase64.

Why does canvas.toDataURL fail?

A canvas containing cross-origin image content without the required permission can block export. Check the browser console and ensure any external image used by the canvas has appropriate cross-origin access.

Can Word generate a QR code without an add-in?

Yes. In a supported version of Word, create a DISPLAYBARCODE field with the QR barcode type and update the field to display the code.