There are two ways of providing dynamic text data to be printed on your labels. In both cases the text can be “styled”, so different parts of the text will use different fonts, sizes and/or styles.

The first way covers most use cases for text formatting needs, it is simple, and, of cause, has some limitations. The limitation is that it supports line-by-line formatting only. So, each line can have its own formatting, but all characters in the same line will use the same formatting. If this is OK for your application, then here are the steps needed to utilize this method. first, you have to design you label layout/template. The easiest way to design a label is by using DYMO Label software. While designing, type a sample data for a label object, and apply some formatting, on line by line basis, e.g. make the first line bold, save the file, and put it on your server.

Those are manual steps, now we will need some JavaScript. Load the label by using openLabelXml. Now you can set your real data by using setLabelText or, if you need to print multiple labels, by using a LabelSet. You pass a plain string without any formatting to the setLabelText method, and the library will apply line-by-line formatting for you based on the sample data in the label file. For example, if you call setLabelText(‘Will E. CoyotenACME Birdingn2200 Desert Meadows WaynLas Vegas, NV 89122’), the output will be like that:

The second way to format the text is by using so called “Text Markup” feature. It is some what more complex than the first formatting method, but in return you have the full control over the font attributes and can do character-by-character formatting. First, you design your label as described above, but you don’t have to specify any sample data. Next you have to construct a “text markup”. A text markup is an xml string contains tags controlling font attributes, plus plain text data. The Supported tags are ones similar to HTML tags: <font>, <b>, <i>, <u>, <s>, <br>. Full xml-schema definition is available here. After you have the markup text, you can print it by using a LabelSet and setTextMarkup method.

Having trouble with maintaining the formatting (font size) when using method 1 from above. I have designed my label using DYMO Label software. I can print the label with formatting intact, but if use setLabelText(“Will E. CoyotenACME Birdingn2200 Desert Meadows WaynLas Vegas, NV 89122”) Label can be printed with the new text but all formatting is lost.

I’m trying to simplify the example where I can have the added benefit of the tags, without the extra buttons. I seem to break the code if I remove the buttons out of the tags, or If I remove the var out of the javascript. either way, the printer selector immediately stops working.

The best approach to your problem is to use DLS to design the label that you want to use. You can set the font and font size too. When you save the label file, it will contain the label xml that you want to include in your labelXml variable.

Useful post, thank you! I still am unsure of one thing though: how to change text styles.

I looked at the schema and example for Method 2, but I’m unsure how to go about making the input text print out bold without having to manually change it to bold, like in the example. That is, how can I hard code the input text to print out bold every time?