Reading or Converting word .doc files iOS

筅森魡賤 提交于 2021-02-05 20:29:23

问题


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.


回答1:


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.




回答2:


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.




回答3:


Modern versions of office use an open standard xml format. http://en.wikipedia.org/wiki/Office_Open_XML




回答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.




回答5:


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.




回答6:


libOPC!

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




回答7:


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"];
}


来源:https://stackoverflow.com/questions/8231068/reading-or-converting-word-doc-files-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!