2013-06-04 105 views
4

我想用iOS-App創建以下文件類型之一:RTF,DOC或DOCX。 用戶應該能夠寫文字還有添加圖像它。 UI的構建不是問題,只是創建文件。在iOS中創建RTF,DOC或DOCX

有沒有最佳做法來做到這一點?

第三方框架是一個選項,但我想自己做。

感謝

回答

10

我可以幫你的docx文件(RTF文件更容易和DOC文件是完全一樣的docx但不太良好的組織)

我認爲你能做的最好的是開始打開一個docx文件到文本編輯器。您首先要解壓縮該文件。

您將有以下文件夾

>_rels 
>customXML 
>docProps 
>word: 
    >_rels: 
     document.xml.rels //this document tells word where the images are situated 
    >media //Here are the images 
    >theme 
    >document.xml //Here is the actual content of the file 
    >header1.xml //Here is the content of your header 
>[Content_Types].xml 

要插入一些文字,看看在document.xml中的文件。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<w:document mc:Ignorable="w14 wp14" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"> 
<w:body> 
.... 
</w:body> 
</w:document> 

Docx使用<w:p>標籤爲段落。 它使用<w:r>表示具有一種格式的文本塊。 它使用<w:t>的實際文本

這將產生一個簡單的Hello World

<w:p w:rsidP="00CA7135" w:rsidR="00137C91" w:rsidRDefault="00137C91"> 
    <w:r> 
     <w:t>Hello world</w:t> 
    </w:r> 
</w:p> 

對於圖像,你將不得不把更多的信息:

  <w:p w:rsidP="00CA7135" w:rsidR="00B12C70" w:rsidRDefault="00B12C70"> 
       <w:r> 
        <w:rPr> 
         <w:noProof/> 
         <w:lang w:eastAsia="fr-FR"/> 
        </w:rPr> 
        <w:drawing> 
         <wp:inline distB="0" distL="0" distR="0" distT="0" wp14:anchorId="7CC0BC42" wp14:editId="09031C8D"> 
          <wp:extent cx="866775" cy="1323975"/> 
          <wp:effectExtent b="9525" l="0" r="9525" t="0"/> 
          <wp:docPr id="13" name="Image 13"/> 
          <wp:cNvGraphicFramePr> 
           <a:graphicFrameLocks noChangeAspect="1" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/> 
          </wp:cNvGraphicFramePr> 
          <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"> 
           <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"> 
            <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"> 
             <pic:nvPicPr> 
              <pic:cNvPr id="0" name=""/> 
              <pic:cNvPicPr/> 
             </pic:nvPicPr> 
             <pic:blipFill> 
              <a:blip r:embed="rId15"/> 
              <a:stretch> 
               <a:fillRect/> 
              </a:stretch> 
             </pic:blipFill> 
             <pic:spPr> 
              <a:xfrm> 
               <a:off x="0" y="0"/> 
               <a:ext cx="866775" cy="1323975"/> 
              </a:xfrm> 
              <a:prstGeom prst="rect"> 
               <a:avLst/> 
              </a:prstGeom> 
             </pic:spPr> 
            </pic:pic> 
           </a:graphicData> 
          </a:graphic> 
         </wp:inline> 
        </w:drawing> 
       </w:r> 
      </w:p> 

的ID(13)內這個標籤:<wp:docPr id="13" name="Image 13"/>必須是唯一的。 Word將會查看document.xml.rels以查看圖像的存儲位置。

Document.xml.rels:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> 
    <Relationship Id="rId8" Target="endnotes.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes"/> 
    <Relationship Id="rId13" Target="media/image5.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
    <Relationship Id="rId18" Target="media/image10.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
    <Relationship Id="rId26" Target="header3.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"/> 
    <Relationship Id="rId3" Target="styles.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"/> 
    <Relationship Id="rId21" Target="media/image13.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
    <Relationship Id="rId7" Target="footnotes.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes"/> 
    <Relationship Id="rId12" Target="media/image4.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
    <Relationship Id="rId17" Target="media/image9.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
    <Relationship Id="rId25" Target="footer1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"/> 
    <Relationship Id="rId2" Target="numbering.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering"/> 
    <Relationship Id="rId16" Target="media/image8.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
    <Relationship Id="rId20" Target="media/image12.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
    <Relationship Id="rId1" Target="../customXml/item1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml"/> 
    <Relationship Id="rId6" Target="webSettings.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings"/> 
    <Relationship Id="rId11" Target="media/image3.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
    <Relationship Id="rId24" Target="header2.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"/> 
    <Relationship Id="rId5" Target="settings.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings"/> 
    <Relationship Id="rId15" Target="media/image7.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
    <Relationship Id="rId23" Target="header1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"/> 
    <Relationship Id="rId28" Target="theme/theme1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"/> 
    <Relationship Id="rId10" Target="media/image2.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
    <Relationship Id="rId19" Target="media/image11.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
    <Relationship Id="rId4" Target="stylesWithEffects.xml" Type="http://schemas.microsoft.com/office/2007/relationships/stylesWithEffects"/> 
    <Relationship Id="rId9" Target="media/image1.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
    <Relationship Id="rId14" Target="media/image6.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
    <Relationship Id="rId22" Target="media/image14.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
    <Relationship Id="rId27" Target="fontTable.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable"/> 
</Relationships> 

,你可以看到,這個標籤,id爲13的圖像在目標屬性locafted。

<Relationship Id="rId13" Target="media/image5.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/> 
+0

Word是否可以在base64中渲染圖像? – Peter

+0

是的,這是可能的。我創建了一個可以替換圖片的庫:下面是一個演示:http://javascript-ninja.fr/docxgenjs/examples/imageTagging.html# Repo在這裏:https://github.com/edi9999/docxgenjs – edi9999

+0

其實你需要一個可以壓縮Base 64文件的庫(我認爲它存在於ios上) – edi9999