2

We have a custom object from which we are generating a word document using a visual force page and the corresponding controller. The custom object has several long text area fields and the reps are copying information from word documents and pasting them into Salesforce fields. While doing this copy/paste operations, all characters are showing up visually correctly in Salesforce. However, when we generate the word document report from the custom object, I see that some of the characters are looking weird. I did some research and found that one such character is the long dash character. When we type a - b in word document, word changes the - (short dash) character to (a -- b) long dash and this is messing up the report.

Below is the example of what is showing in Salesforce.

Continue lending growth – expand new markets – AZ is a focus

and, below is what I am seeing in the word document generated by Visual force page and the corresponding controller.

Continue lending growth – expand new markets – AZ is a focus

How do I handle such weird characters while generating the word document using the visual force page and the corresponding controller? Some other characters that seem to be messing up the formatting are single quotes in word, back ticks, some bullet points etc.

2

Your data in Salesforce is encoded in UTF-8 format. The en dash is represented in UTF-8 as three bytes, 0xE2 0x80 0x93. Microsoft Word is interpreting the characters as Windows Codepage 1252 encoding, which treats each of these bytes as a separate character. 0xE2 = â. 0x80 = €. 0x93 = “.

Salesforce does not provide robust support for manipulating text encoding, or text as byte strings, in Apex. The code you are using to generate the Microsoft Word document will need to appropriately declare the encoding of your text in whatever way is required by the format, or call out to an external service to convert the text into Codepage 1252 encoding.

The only solution I am aware of in pure Apex would be to use String.getChars() to interpret the string as a byte array and filter to remove all values above 128, before rebuilding the string using String.fromCharArray(). This would, of course, be a lossy and rather slow operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.