2009-12-08 3 views
2

텍스트 및 그래픽 출력을 PDF 문서로 렌더링 할 수있는 라이브러리를 찾고 있습니다. (Cairo은 확실히 옵션입니다.) OpenOffice가 PDF 파일을 작성하여 동일한 라이브러리를 사용할 수 있는지 확인하고 싶습니다. PDF 내보내기를 위해 OpenOffice에서 사용하는 라이브러리는 무엇입니까?OpenOffice PDF 내보내기 라이브러리

편집 : 나는 C 또는 C++ 라이브러리를 찾고 있습니다.

답변

7

OpenOffice를 사용하여 문서를 PDF로 내보내는 방법을 찾아 보았습니다. 드디어 저에게 90 %의 오픈 오피스 포럼에 묻힌 글이 발견되었습니다. 여기 내 100 % 솔루션입니다. OpenOffice 3.1에서 작동합니다. 이 코드를 사용하려면 OpenOffice를 설치해야합니다. cli_basetypes, cli_cppuhelper, cli_oootypes, cli_ure, cli_uretypes에 대한 참조를 포함해야합니다. 이러한 dll 참조는 OpenOffice SDK에 있습니다. 죄송합니다,하지만이 C#에서 .. C/C++ 아닙니다. HTH 누군가.

 
using unoidl.com.sun.star.lang; 
using unoidl.com.sun.star.uno; 
using unoidl.com.sun.star.bridge; 
using unoidl.com.sun.star.frame; 
using unoidl.com.sun.star.beans; 

public static void ConvertToPDF(string inputFile, string outputFile) 
     { 
      if (ConvertExtensionToFilterType(Path.GetExtension(inputFile)) == null) 
       throw new InvalidProgramException("Unknown file type for OpenOffice. File = " + inputFile); 

      StartOpenOffice(); 

      //Get a ComponentContext 
      unoidl.com.sun.star.uno.XComponentContext xLocalContext = 
       uno.util.Bootstrap.bootstrap(); 
      //Get MultiServiceFactory 
      unoidl.com.sun.star.lang.XMultiServiceFactory xRemoteFactory = 
       (unoidl.com.sun.star.lang.XMultiServiceFactory) 
       xLocalContext.getServiceManager(); 
      //Get a CompontLoader 
      XComponentLoader aLoader = 
       (XComponentLoader)xRemoteFactory.createInstance("com.sun.star.frame.Desktop"); 
      //Load the sourcefile 

      XComponent xComponent = null; 
      try 
      { 
       xComponent = initDocument(aLoader, 
        PathConverter(inputFile), "_blank"); 
       //Wait for loading 
       while (xComponent == null) 
       { 
        System.Threading.Thread.Sleep(1000); 
       } 

       // save/export the document 
       saveDocument(xComponent, inputFile, PathConverter(outputFile)); 

      } 
      catch { throw; } 
      finally { xComponent.dispose(); } 

     } 

     private static void StartOpenOffice() 
     { 
      Process[] ps = Process.GetProcessesByName("soffice.exe"); 
      if (ps != null) 
      { 
       if (ps.Length > 0) 
        return; 
       else 
       { 
        Process p = new Process(); 
        p.StartInfo.Arguments = "-headless -nofirststartwizard"; 
        p.StartInfo.FileName = "soffice.exe"; 
        p.StartInfo.CreateNoWindow = true; 
        bool result = p.Start(); 
        if (result == false) 
         throw new InvalidProgramException("OpenOffice failed to start."); 
       } 
      } 
      else 
      { 
       throw new InvalidProgramException("OpenOffice not found. Is OpenOffice installed?"); 
      } 
     } 

     private static XComponent initDocument(XComponentLoader aLoader, string file, string target) 
     { 
      PropertyValue[] openProps = new PropertyValue[1]; 
      openProps[0] = new PropertyValue(); 
      openProps[0].Name = "Hidden"; 
      openProps[0].Value = new uno.Any(true); 


      XComponent xComponent = aLoader.loadComponentFromURL(
       file, target, 0, 
       openProps); 

      return xComponent; 
     } 


     private static void saveDocument(XComponent xComponent, string sourceFile, string destinationFile) 
     { 
      PropertyValue[] propertyValues = new PropertyValue[2]; 
      propertyValues = new PropertyValue[2]; 
      // Setting the flag for overwriting 
      propertyValues[1] = new PropertyValue(); 
      propertyValues[1].Name = "Overwrite"; 
      propertyValues[1].Value = new uno.Any(true); 
      //// Setting the filter name 
      propertyValues[0] = new PropertyValue(); 
      propertyValues[0].Name = "FilterName"; 
      propertyValues[0].Value = new uno.Any(ConvertExtensionToFilterType(Path.GetExtension(sourceFile))); 
      ((XStorable)xComponent).storeToURL(destinationFile, propertyValues); 

     } 


     private static string PathConverter(string file) 
     { 
      if (file == null || file.Length == 0) 
       throw new NullReferenceException("Null or empty path passed to OpenOffice"); 

      return String.Format("file:///{0}", file.Replace(@"\", "/")); 

     } 

     public static string ConvertExtensionToFilterType(string extension) 
     { 
      switch (extension) 
      { 
       case ".doc": 
       case ".docx": 
       case ".txt": 
       case ".rtf": 
       case ".html": 
       case ".htm": 
       case ".xml": 
       case ".odt": 
       case ".wps": 
       case ".wpd": 
        return "writer_pdf_Export"; 
       case ".xls": 
       case ".xlsb": 
       case ".ods": 
        return "calc_pdf_Export"; 
       case ".ppt": 
       case ".pptx": 
       case ".odp": 
        return "impress_pdf_Export"; 

       default: return null; 
      } 
     } 


    } 
+0

유망 해 보인다. 나는 그것을 시도한 후에 이것을 받아 들일 것이다. 대단히 감사합니다. –

+1

'while (xComponent == null)'은 xComponent가 null 인 경우 무한 루프가 발생합니다. –

1

현재 어떤 언어로 작업하고 있습니까? 많은 PDF 라이브러리가 있습니다. 검색 스택 오버플로 "pdf 라이브러리 [프로그래밍 언어]". 이미 많은 추천이 있습니다.

OpenOffice는 Sun PDF library을 확장명으로 사용하여 PDF를 가져 오지만 PDF를 내보내는 데 사용하는 것이 확실하지 않습니다.

+0

나는 몇 가지 옵션 건너했지만 앞으로 이동하기 전에 특별히 OO에 대해 알고 싶습니다. BTW, 나는 그래픽 출력을 PDF로보고있다. –