2014-07-22 157 views
0

我現在有一個使用Microsoft將docx(以字節[]格式)轉換爲pdf格式(以字節格式)的函數.Office.Interop.Word使用openXML或類似的格式將docx []格式轉換爲pdf格式[pdf格式]

它的效果很好。除了它不能在線工作,因爲它需要將WinOffice安裝在服務器上,我無法做任何事情。

所以我需要去別的東西,我正在考慮openXML(除非你知道更好的方法)。

但是我到底會如何解決這個問題? 我只是想把這個docx文件,轉換並以pdf格式返回它[]格式。

我在以前的Microsoft.Office代碼看起來像這樣

public static byte[] ConvertDocx2PDF(byte[] DocxFile, string FileName) 
{ 
    try 
    { 
     string path = Path.Combine(HttpRuntime.AppDomainAppPath, "MailFiles/DOCX2PDF"); 

     if (!Directory.Exists(path)) 
      Directory.CreateDirectory(path); 

     Guid id = Guid.NewGuid(); 

     FileName = id.ToString() + FileName; 

     path += "/" + FileName; 



     if (File.Exists(path)) 
      File.Delete(path); 

     File.WriteAllBytes(path, DocxFile); 

     Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); 

     object oMissing = System.Reflection.Missing.Value; 

     word.Visible = false; 
     word.ScreenUpdating = false; 

     // Cast as Object for word Open method 
     Object filename = (Object)path; 
     // Use the dummy value as a placeholder for optional arguments 
     Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(ref filename, ref oMissing, 
      ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
      ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
      ref oMissing, ref oMissing, ref oMissing, ref oMissing); 
     doc.Activate(); 
     object outputFileName = (object)path.ToLower().Replace(".docx", ".pdf"); 
     object fileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF; 

     if (File.Exists(outputFileName.ToString())) 
      File.Delete(outputFileName.ToString()); 

     // Save document into PDF Format 
     doc.SaveAs(ref outputFileName, 
      ref fileFormat, ref oMissing, ref oMissing, 
      ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
      ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
      ref oMissing, ref oMissing, ref oMissing, ref oMissing); 

     object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; 
     ((Microsoft.Office.Interop.Word._Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing); 
     doc = null; 

     ((Microsoft.Office.Interop.Word._Application)word).Quit(ref oMissing, ref oMissing, ref oMissing); 
     word = null; 

     try 
     { 
      File.Delete(path); 
     } 
     catch { } 

     return File.ReadAllBytes(path.ToLower().Replace(".docx", ".pdf")); 
    } 
    catch (Exception e) 
    { 

    } 
    byte[] erroByte = new byte[0]; 
    return erroByte; 
} 

至於說。它工作的很好,但不能在我的服務器上工作。

任何想法如何在openXML或任何其他?

謝謝您的時間

+0

http://stackoverflow.com/a/607679/56778 –

回答

0

DOCX是一個文檔描述格式,而你能想到的PDF作爲一個矢量圖形格式。儘管僞裝成文檔格式很難,但它本質上是一種圖形格式。

這是什麼意思?這意味着正確的轉換將需要呈現文檔。基本上,你必須重新實現MS Word的核心部分,以使其可靠。

我想有一些庫存在,但它會比獲得一個服務器更多的花費,你可以只安裝一個Word副本。

但畢竟,OpenOffice的可以渲染word文檔,所以也許有人會嘗試將其嵌入到(龐大)庫...

編輯:其實,我發現this answer,這可能會有幫助,但它說它需要安裝OpenOffice。也許它可以與xcopied OOo一起工作,你可以嘗試一下。

1

您可以使用OpenXmlSdk和OpenXML電動工具將docx轉換爲html,然後您可以將您的html轉換爲pdf。 這裏不需要互操作。 最後,您可以使用WkHtmlToPDF作爲一個DLL來創建Html的PDF。 在Web瀏覽器中的PDF渲染。這對我有效。

鏈接:

OpenXml Docx to Html

Docx to Html using XSLT

希望這有助於!