2014-07-22 1 views
0

Microsoft를 사용하여 docx (바이트 [] 형식)를 pdf (바이트 [] 형식)로 변환하는 함수가 있습니다. .Office.Interop.Worddocx를 bytes [] 형식으로 변환하여 pdf 형식으로 openXML 또는 그 유사 형식으로 변환

그리고 잘 작동합니다. WinOffice를 서버에 설치해야하므로 온라인으로 작동하지 않는다는 점을 제외하고는 아무 것도 할 수 없습니다.

그래서 저는 다른 뭔가를 생각해보고 openXML에 대해 생각하고 있습니다 (더 좋은 방법을 알지 못한다면).

하지만 정확히이 문제를 어떻게 해결할 수 있습니까? 이 docx 파일을 가져 와서 바이트 수 [] 형식의 pdf로 변환하고 싶습니다. 이

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

으로 말한 것처럼 Microsoft.Office에서

내 앞의 코드 보인다. 그것은 잘 작동하지만 내 서버에서 작동하지 않습니다.

openXML 또는 다른 어떤 방법으로 어떻게 할 수 있습니까?

당신은 벡터 그래픽 포맷으로 PDF 생각할 수있는 반면

+0

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

답변

0

DOCX이 문서의 설명 형식입니다 시간 내 주셔서 감사합니다. 문서 형식으로 가장하기는 매우 어렵지만, 본질적으로 그래픽 형식입니다.

이것은 무엇을 의미합니까? 이는 적절한 변환을 통해 문서를 렌더링해야 함을 의미합니다. 기본적으로 신뢰할 수 있도록 MS Word의 핵심 부분을 재 구현해야합니다.

존재하는 라이브러리가 있지만 Word 사본을 설치할 수있는 서버를 얻는 것보다 훨씬 많은 비용이들 것입니다. , 사실, 내가 도움이 될 수 있습니다 this answer을 발견

편집

이 ...

그러나 결국

는 오픈 오피스 워드 문서를 렌더링 할 수있는, 그래서 아마도 누군가가 (거대한) 라이브러리에 그것을 포함을 시도 할 수 있습니다 오픈 오피스를 설치해야한다고 말하고있다. 아마도 xcopied OOo와 함께 작동 할 수도 있습니다. 시도해 볼 수 있습니다.

1

OpenXmlSdk 및 OpenXML 전동 공구를 사용하여 docx를 html로 변환 한 다음 html을 pdf로 변환 할 수 있습니다. 여기에 interop이 필요하지 않습니다. 그리고 마지막으로 WkHtmlToPDF을 dll로 사용하여 HTML에서 pdf를 만들 수 있습니다. 웹 브라우저에서의 PDF 렌더링. 이것은 나를 위해 일했다.

링크 :이 도움이

OpenXml Docx to Html

Docx to Html using XSLT

희망!