2016-12-19 3 views
1

私の要件は、バージョンで文書を保存できるカスタム文書ライブラリを作成することです。外出先でCKEditorを使用してMS Word文書を変更し、私のサーバーのどこかに保存する方法はありますか?

外出先でCKEditorを使用してMS Word文書(.docまたは.docx)を変更して、自分のサーバーのどこかに保存する方法はありますか?

他のオプションに関する提案も受け入れられます。

答えて

2

あなたはそのためのOpenXML DLLを実装する必要があり

...私は.DOCわからないですが、.docxのために、あなたは次のようにHTMLに変換することができます。あなたが必要になります DLLがSystem.IO.Packaging.dll

1. DocumentFormat.OpenXml.dll

2. OpenXmlPowerTools.dll

3.あります

ここにコードスニペットの作業バージョンがあります。 DocxToHtml.csクラスは

using System.IO; 
    using System.Linq; 
    using OpenXmlPowerTools; 
    using DocumentFormat.OpenXml.Packaging; 
    using System.Drawing.Imaging; 
    using System.Xml.Linq; 

    public class DocxToHtml 
{ 

    public string ConvertToHtml(string fullFilePath) 
    { 
     if (string.IsNullOrEmpty(fullFilePath) || Path.GetExtension(fullFilePath) != ".docx") 
      return "Unsupported format"; 

     FileInfo fileInfo = new FileInfo(fullFilePath); 

     string htmlText = string.Empty; 
     try 
     { 
      htmlText = ParseDOCX(fileInfo); 
     } 
     catch (OpenXmlPackageException e) 
     { 

      if (e.ToString().Contains("Invalid Hyperlink")) 
      { 
       using (FileStream fs = new FileStream(fullFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) 
       { 
        UriFixer.FixInvalidUri(fs, brokenUri => FixUri(brokenUri)); 
       } 
       htmlText = ParseDOCX(fileInfo); 
      } 
     } 


     return htmlText; 


    } 

    private static string FixUri(string brokenUri) 
    { 
     string newURI = string.Empty; 

     if (brokenUri.Contains("mailto:")) 
     { 
      int mailToCount = "mailto:".Length; 
      brokenUri = brokenUri.Remove(0, mailToCount); 
      newURI = brokenUri; 
     } 
     else 
     { 
      newURI = " "; 
     } 
     return newURI; 
    } 


    private string ParseDOCX(FileInfo fileInfo) 
    { 
     byte[] byteArray = File.ReadAllBytes(fileInfo.FullName); 
     using (MemoryStream memoryStream = new MemoryStream()) 
     { 
      memoryStream.Write(byteArray, 0, byteArray.Length); 


      using (WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true)) 
      { 

       int imageCounter = 0; 

       var pageTitle = fileInfo.FullName; 
       var part = wDoc.CoreFilePropertiesPart; 
       if (part != null) 
        pageTitle = (string)part.GetXDocument().Descendants(DC.title).FirstOrDefault() ?? fileInfo.FullName; 

       // TODO: Determine max-width from size of content area. 
       WmlToHtmlConverterSettings settings = new WmlToHtmlConverterSettings() 
       { 
        AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }", 
        PageTitle = pageTitle, 
        FabricateCssClasses = true, 
        CssClassPrefix = "pt-", 
        RestrictToSupportedLanguages = false, 
        RestrictToSupportedNumberingFormats = false, 
        ImageHandler = imageInfo => 
        { 
         ++imageCounter; 
         string extension = imageInfo.ContentType.Split('/')[1].ToLower(); 
         ImageFormat imageFormat = null; 
         if (extension == "png") 
          imageFormat = ImageFormat.Png; 
         else if (extension == "gif") 
          imageFormat = ImageFormat.Gif; 
         else if (extension == "bmp") 
          imageFormat = ImageFormat.Bmp; 
         else if (extension == "jpeg") 
          imageFormat = ImageFormat.Jpeg; 
         else if (extension == "tiff") 
         { 
          // Convert tiff to gif. 
          extension = "gif"; 
          imageFormat = ImageFormat.Gif; 
         } 
         else if (extension == "x-wmf") 
         { 
          extension = "wmf"; 
          imageFormat = ImageFormat.Wmf; 
         } 

         // If the image format isn't one that we expect, ignore it, 
         // and don't return markup for the link. 
         if (imageFormat == null) 
          return null; 

         string base64 = null; 
         try 
         { 
          using (MemoryStream ms = new MemoryStream()) 
          { 
           imageInfo.Bitmap.Save(ms, imageFormat); 
           var ba = ms.ToArray(); 
           base64 = System.Convert.ToBase64String(ba); 
          } 
         } 
         catch (System.Runtime.InteropServices.ExternalException) 
         { return null; } 


         ImageFormat format = imageInfo.Bitmap.RawFormat; 
         ImageCodecInfo codec = ImageCodecInfo.GetImageDecoders().First(c => c.FormatID == format.Guid); 
         string mimeType = codec.MimeType; 

         string imageSource = string.Format("data:{0};base64,{1}", mimeType, base64); 

         XElement img = new XElement(Xhtml.img, 
          new XAttribute(NoNamespace.src, imageSource), 
          imageInfo.ImgStyleAttribute, 
          imageInfo.AltText != null ? 
           new XAttribute(NoNamespace.alt, imageInfo.AltText) : null); 
         return img; 
        } 
       }; 

       XElement htmlElement = WmlToHtmlConverter.ConvertToHtml(wDoc, settings); 

       var html = new XDocument(new XDocumentType("html", null, null, null), htmlElement); 
       var htmlString = html.ToString(SaveOptions.DisableFormatting); 
       return htmlString; 

      } 
     } 
    } 


} 

これは、上記のコードで使用されるウリ定着クラスある

using System; 
using System.IO; 
using System.IO.Compression; 
using System.Linq; 
using System.Xml; 
using System.Xml.Linq; 

public static class UriFixer 
{ 
    public static void FixInvalidUri(Stream fs, Func<string, string> invalidUriHandler) 
    { 
     XNamespace relNs = "http://schemas.openxmlformats.org/package/2006/relationships"; 
     using (ZipArchive za = new ZipArchive(fs, ZipArchiveMode.Update)) 
     { 
      foreach (var entry in za.Entries.ToList()) 
      { 
       if (!entry.Name.EndsWith(".rels")) 
        continue; 
       bool replaceEntry = false; 
       XDocument entryXDoc = null; 
       using (var entryStream = entry.Open()) 
       { 
        try 
        { 
         entryXDoc = XDocument.Load(entryStream); 
         if (entryXDoc.Root != null && entryXDoc.Root.Name.Namespace == relNs) 
         { 
          var urisToCheck = entryXDoc 
           .Descendants(relNs + "Relationship") 
           .Where(r => r.Attribute("TargetMode") != null && (string)r.Attribute("TargetMode") == "External"); 
          foreach (var rel in urisToCheck) 
          { 
           var target = (string)rel.Attribute("Target"); 
           if (target != null) 
           { 
            try 
            { 
             Uri uri = new Uri(target); 
            } 
            catch (UriFormatException) 
            { 
             string newUri = invalidUriHandler(target); 
             rel.Attribute("Target").Value = newUri; 
             replaceEntry = true; 
            } 
           } 
          } 
         } 
        } 
        catch (XmlException) 
        { 
         continue; 
        } 
       } 
       if (replaceEntry) 
       { 
        var fullName = entry.FullName; 
        entry.Delete(); 
        var newEntry = za.CreateEntry(fullName); 
        using (StreamWriter writer = new StreamWriter(newEntry.Open())) 
        using (XmlWriter xmlWriter = XmlWriter.Create(writer)) 
        { 
         entryXDoc.WriteTo(xmlWriter); 
        } 
       } 
      } 
     } 
    } 


} 

あなたがしなければならないのは、あなたのの.docxへのファイル・パスを提供していますファイルをConvertToHtmlメソッドに追加すると、作業が行われ、htmlが表示されます。

注:これはこれは本当に便利だった.docxの

+0

のためにサポートしています。しかし、それはエディタでのみファイルを開くことではありません。また、同じ形式でシステムに戻すこともできます。 – Subhasis

+0

はい、間違いなくそれを行うこともできます。チェックアウトを実行してください http://openxmldeveloper.org/discussions/development_tools/f/16/t/3083.aspxとhttps://html2openxml.codeplex.com/documentation – Ozesh

+0

ここには私が作成したばかりの小さなサンプルプロジェクトがあります:https:/ /github.com/zaagan/Docx-Html-Docx – Ozesh

関連する問題