Insert a Generated QR Code Image with a Word Office.js Add-in
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.
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.
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.
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.
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(); });
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().
Use a Word DISPLAYBARCODE field for a QR code
Use Word's built-in field when a document field meets your requirements and a custom canvas image is not necessary.
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.

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.




