tags:

views:

49

answers:

1

I'm trying to write a MSword file in document directory by the following code:

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString* path;
NSString* gradeDoc = [self fetchCommentsDesc];
NSString* str = [self.objStudent.strName stringByAppendingFormat:@".doc"];
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:str];
[gradeDoc writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

[self fetchCommentsDesc] returns NSString.
self.student.strName is a String

Issue: When i Open the doc file created in document directory of iphone, all the special characters in the doc appears as boxes or some arabic chars.

Please help!

A: 

You're writing some UTF-8 bytes, which is not a ".doc". Try writing it to a ".txt" file.

Also, you may have to write a "byte order mark" for some text editors to notice that it's UTF-8 or UTF-16 (in NS/CF-land, this is known as an "external representation"):

NSData * d = [(id)CFStringCreateExternalRepresentation(NULL, (CFStringRef)gradeDoc, kCFStringEncodingUTF8, 0) autorelease];
[d writeToFile:path atomically:YES];

Also try kCFStringEncodingUnicode and kCFStringEncodingUTF16.

tc.
Hi, thanks for the reply but that didn't work.Writing into a .txt file is not an issue but client want .doc file.i tried the above code, when we try to open the .doc file msword opens the file conversion dialog box which has a list of encodings from which i have to select 1. This same dialog box used to appear when i tried this:[gradeDoc writeToFile:path atomically:YES encoding:NSUnicodeStringEncoding error:nil];
Saurabh Verma
Try outputting HTML as a ".doc". It's a nasty hack (and you might need to use <meta> or XML entities), but I think most versions of Word support it.
tc.
I tried this, .doc is now getting opened perfectly in ms word but not getting opened in "pages" in mac and "good reader" in ipad.
Saurabh Verma