How are other apps on iOS able to read and write word docs? I see some other questions related to this and accepted answers are along the lines of "it can't be done." I don't want to just display a word doc, I want to read it along with its formatting. How are other apps doing it, are they writing the parsing themselves using the published standard put out by Microsoft? Are they using some kind of bundled utility to convert the file to some other format like XML or HTML before processing it? Is there an open source way of doing this? Looking for ideas.
-
Hello casey. What is the reason behind needing to read the word doc internals? Do you need to make decisions based on what's inside a doc? – Paul Jowett Dec 02 '11 at 13:08
-
Yes, I do need to make decisions based on what the content of the word doc is. This is only in the research stage at this point, I am looking into a possible feature for something I already have. – casey Dec 07 '11 at 21:12
-
@casey Any update on this, were you able to read .doc files in your iOS app? Is there any library to convert .doc to .docx format directly ? – Pavan Aug 11 '15 at 14:01
7 Answers
I don't know if you are still looking for solution or you figured it yourself but I am answering this hoping it will help someone else looking for the same.
I was looking for a solution related to my task that I want to convert word file to text file. I came on this question after some googling and according to the answer from @TJD I gone on the link and from there I found this link.
For my requirement as I was needed to convert word file to text file. I followed second link as my solution.
As the docx file is created with Open XML File format
and it is mentioned in there I understand that I need to unzip the docx file considering it a zip.
For Zip/Unzip google provides code here. After unziping the docx file in our document directory according to the wikipedia link there are three directories and one xml file in root.
For my solution I choose word
directory as mentioned in link that original content of file is placed there (I didn't gone in any other directory or file till now). There is a file under your extract path word/doctment.xml
this is where your docx file content placed in xml format.
There are lots of tags available in that xml file and I don't know the meaning of those text right now but after looking at the xml file I got that the tag which contain my text is w:t
.
After that every thing is like cake. I just used NSXMLParser
and parsed the data from the xml file targeting the w:t
tag and I got my whole string.
Note: I will update my answer as soon as I understand about the other files and tags. As well this solution is not working with doc files as of I know OpenXMLFile format
is introduced in MSOffice 2007 so I will also update my answer for doc file solution.
I know this is not enough that it is not covering creating doc file etc. But I hope this will help lots of us.

- 5,152
- 9
- 65
- 100
-
Thank You so much that helped a lot. I am just getting started with xml with ios. Could you provide the code in the phaser that finds the w:t. That is my only question. Thank You so much. – BDGapps Aug 23 '12 at 19:03
-
Sorry didn't get you. What exactly you mean with Phaser? Do you want to know how to parse the `document.xml` file or something else – Kapil Choubisa Aug 24 '12 at 04:42
-
1
-
Thank you. I was looking for it. I am creating an application(document translator) in flutter. I was searching for it from 3 hours. Your answer made It so easy for me. I unzipped the docx file and there I found a file document.xml and I parsed all the data from```w:t``` tag, and it's done. – Adil Shinwari Oct 28 '22 at 21:40
The "trick" most apps use to read Word files is UIWebView — it can read them. This doesn't allow for writing docs, but that is a much harder problem for which I don't believe an easy solution exists.

- 234,037
- 30
- 302
- 389
-
2They use it for displaying, not reading. Once opened in the UIWebView there is no way to access its content. And the data access methods available on the mac are not available on iOS so I was looking to see if anyone knew "the secret" or if everyone is rolling their own. Maybe this needs to be turned into a Google Code project – casey Dec 07 '11 at 21:14
Modern versions of office use an open standard xml format. http://en.wikipedia.org/wiki/Office_Open_XML

- 11,800
- 1
- 26
- 34
-
Yeah, most of my worries were around the previous doc format, which is documented, but doesn't seem to be openly implemented, at least in C / or compatible with iOS. – casey Nov 22 '11 at 18:18
-
Yes, earlier versions of MS Word used a proprietary .doc and .dot format. Now, (I think 2007 and onward) it uses the open standards of .docx and .dotx. That is why do many earlier questions were answered so negatively. – CodaFi Nov 22 '11 at 18:23
-
2Please elaborate on your answer, as [answers that are mostly just links are not considered "good" answers](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers). – casperOne Nov 26 '11 at 04:20
ISO/IEC 29500 standard conformant, cross-platform, open source, standard C99-based implementation of Part II (OPC) and Part III (MCE) of the ISO/IEC 29500 specification (OOXML). And it works for ios as well http://www.nooxml.com/video/libopc_iphone.wmv

- 117
- 1
- 4
Here's how to read the Open Office XML (OOXML) format in iOS: http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2011/05/09/147049.aspx
The link will lead you to a tutorial that will get you the metadata of an OOXML file, it's not the text, or the formatting, but it's a start.
.doc files are a proprietary zip format containing many files related to text and formatting (if you want to see what's inside, go into the finder and 'Get Info'; then rename the extension to .zip and decompress the file). Those files are filled to the top with very large amounts of random XML symbols that are of no use to you or anyone else.
However, .docx files can be opened and converted easily with the adoption of the OOXML standard. See the link.

- 43,043
- 8
- 107
- 153
-
Please elaborate on your answer, as [answers that are mostly just links are not considered "good" answers](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers). – casperOne Nov 26 '11 at 04:20
-
I've been deleting your comments as they have not been constructive to the question and answer in general. I've suggested that you flesh out your answer (to address concerns over why you are being downvoted, as well as why this is not a good answer). See [my answer](http://stackoverflow.com/q/8276431/50776) for what would more than likely be considered a good answer. – casperOne Nov 26 '11 at 05:03
-
CasperOne, specific edits would be a nice suggestion. Since I don't seem to have the foggiest of what you're getting at here. – CodaFi Nov 26 '11 at 05:05
-
That's up to you, you've been [shown the guideline](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers), and [given an example](http://stackoverflow.com/q/8276431/50776). If you have any *specific* questions about either, please feel free to ask. – casperOne Nov 26 '11 at 05:10
Depending on which version of a Word document that you want to display, you have a few choices.
If your document is indeed a .doc file (meaning, before Word 2007), then you can follow the specification for the .doc Binary File Format (which is an open specification, which allows you to use it freely) to read/write Word documents in that format.
If your document is a .docx file, then as TDJ and CodaFi have pointed out, the docx file format is an open standard.
This means that you can see every detail about how to interpret a .docx file (or any other file in the Office 2007 suite on) and process it to suit your needs.
This is how current iOS applications are able to display a docx file.
Note, this is not an easy task, as there are many, many details to those specifications.
-
I believe the .doc is published. http://msdn.microsoft.com/en-us/library/cc313153%28v=office.12%29.aspx – casey Dec 07 '11 at 20:40
The code below saves a Word .docx file to the app's document directory at launch. It then reads that file into a UIWebView during your viewDidLoad. Finally, it waits for the UIWebView to load the document before fetching the text from the UIWebView. Don't forget to conform to the UIWebViewDelegate protocol in your view controller's header file. And of course, the Word document must be included in your project. Make sure to add the document to Build Phases > Copy Bundle Resources.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/* WRITE WORD FILE TO DOCUMENT DIRECTORY */
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"Text.docx"];
NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Text.docx"]];
[data writeToFile:path atomically:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
/* READ WORD FILE FROM DOCUMENT DIRECTORY TO WEB VIEW */
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *wordFilePath = [documentsDirectory stringByAppendingPathComponent:@"Text.docx"];
UIWebView *theWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
NSURL *wordFileUrl = [NSURL fileURLWithPath:wordFilePath];
NSURLRequest *request = [NSURLRequest requestWithURL:wordFileUrl];
[theWebView loadRequest:request];
theWebView.delegate = self;
[self.view addSubview:theWebView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
/* GET TEXT FROM WEB VIEW */
NSString *text = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerText"];
}

- 15,920
- 23
- 106
- 151