I am trying to create a Word document in my Swift app; just some plain text in a certain font as a .docx
file. I haven't found a way to create a word document, but my best guess is that I could import a word document into my Xcode project with just the text "Hello World" in it, then in my code replace every occurrence of "Hello World" with the desired text.
How might I go about altering the text of a .docx
file in my swift project?
Edit (here's why I need this): I want to create a .docx
file so that the user can export whatever text the type in into the Google Docs app or Pages app. I understand the usage of UIDocumentInteractionController
to export preexisting documents, but I need to figure out how I can write and save text to a Word document.
3 Answers
Answers 1
You can create the document in HTML then add the proper header and file extension.
Example:
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"> <head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><title></title> <style> v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <style> @page { mso-page-orientation: landscape; size:29.7cm 21cm; margin:1cm 1cm 1cm 1cm; } @page Section1 { mso-header-margin:.5in; mso-footer-margin:.5in; mso-header: h1; mso-footer: f1; } div.Section1 { page:Section1; } table#hrdftrtbl { margin:0in 0in 0in 900in; width:1px; height:1px; overflow:hidden; } p.MsoFooter, li.MsoFooter, div.MsoFooter { margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; font-size:12.0pt; } </style> <xml> <w:WordDocument> <w:View>Print</w:View> <w:Zoom>100</w:Zoom> <w:DoNotOptimizeForBrowser/> </w:WordDocument> </xml> </head> <body> <div class="Section1"> <p> </p> <br/> <table id='hrdftrtbl' border='0' cellspacing='0' cellpadding='0'> <tr><td> <div style='mso-element:header' id=h1 > <!-- HEADER-tags --> <p class=MsoHeader >HEADER</p> <!-- end HEADER-tags --> </div> </td> <td> <div style='mso-element:footer' id=f1><span style='position:relative;z-index:-1'> <!-- FOOTER-tags --> FOOTER <span style='mso-no-proof:yes'><!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0"/> <v:f eqn="sum @0 1 0"/> <v:f eqn="sum 0 0 @1"/> <v:f eqn="prod @2 1 2"/> <v:f eqn="prod @3 21600 pixelWidth"/> <v:f eqn="prod @3 21600 pixelHeight"/> <v:f eqn="sum @0 0 1"/> <v:f eqn="prod @6 1 2"/> <v:f eqn="prod @7 21600 pixelWidth"/> <v:f eqn="sum @8 21600 0"/> <v:f eqn="prod @7 21600 pixelHeight"/> <v:f eqn="sum @10 21600 0"/> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/> <o:lock v:ext="edit" aspectratio="t"/> </v:shapetype><v:shape id="Picture_x0020_1" o:spid="_x0000_s3073" type="#_x0000_t75" alt="VHB" style='position:absolute; margin-right:0pt;margin-top:-400pt; z-index:-1; visibility:visible;mso-wrap-style:square;mso-wrap-distance-left:9pt; mso-wrap-distance-top:0;mso-wrap-distance-right:9pt; mso-wrap-distance-bottom:0;mso-position-horizontal:absolute; mso-position-horizontal-relative:text;mso-position-vertical:absolute; mso-position-vertical-relative:text'> <v:imagedata src="https://www.google.bg/logos/2012/Rodin-2012-homepage.png"/> </v:shape><![endif]--></span> <p class=MsoFooter> <span style=mso-tab-count:2'></span> Page <span style='mso-field-code: PAGE '><span style='mso-no-proof:yes'></span> from <span style='mso-field-code: NUMPAGES '></span> <!-- end FOOTER-tags --> </span> </p> </div> <div style='mso-element:header' id=fh1> <p class=MsoHeader><span lang=EN-US style='mso-ansi-language:EN-US'> <o:p></o:p></span></p> </div> <div style='mso-element:footer' id=ff1> <p class=MsoFooter><span lang=EN-US style='mso-ansi-language:EN-US'> <o:p></o:p></span></p> </div> </td></tr> </table> </div> </body></html>
Source: HTML generated Microsoft Word document with header, footer and watermark
Answers 2
You can't directly "edit the text" in the Word document. A .docx
file is actually a ZIP file with a certain file/folder structure inside (try to rename it to .zip
& extract it and you'll see), which conforms to the "Office Open XML" format (https://en.wikipedia.org/wiki/Office_Open_XML).
If there are no libraries to modify the Office OpenXML format in Swift, you may just have to resort to writing the code yourself. However, if you have simple requirements (just some plain text, as you state), this shouldn't be too complicated.
Microsoft has an OpenXML library in GitHub at https://github.com/OfficeDev/Open-XML-SDK which may be of help to you.
Answers 3
Given that you mention a user wanting to transfer documents to Google Docs, you could consider working with the RTF format instead. You can also export a docx file to RTF format, work on that and when you are done, import the modified RTF file directly into Google docs.
I suggest this since RTF is also a workable open format: See https://www.safaribooksonline.com/library/view/rtf-pocket-guide/9781449302047/ch01.html. Based on that reference, you can generate this simple (ascii) output:
{\rtf1\ansi\deff0 {\fonttbl {\f0 Verdana;}} \f0\fs16 Hello World! }
and save it as a file with the .rtf extension.
0 comments:
Post a Comment