2009-12-08 79 views
2

我正在尋找一個庫,它允許我將文本和圖形輸出呈現到PDF文檔上。 (Cairo肯定是一個選項。)我想知道OpenOffice如何編寫PDF文件以查看是否可以使用相同的庫。 OpenOffice用於PDF導出的庫有哪些?OpenOffice PDF導出庫

編輯:我正在尋找一個C或C++庫。

回答

7

我到處查找了解如何使用OpenOffice將任何文檔導出爲PDF。我終於在OpenOffice論壇上發現了一篇埋葬帖子,讓我有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

看起來很有希望。我嘗試一下後會接受這個。太感謝了。 – 2010-01-19 16:17:18

+1

'while(xComponent == null)'將導致無限循環,如果xComponent永遠爲null。 – 2016-11-14 19:27:06

1

你在用什麼語言工作?那裏有很多PDF庫。搜索堆棧溢出「pdf庫[編程語言]」。已經有很多建議。

OpenOffice使用Sun PDF library作爲擴展來導入PDF,但我不確定它用於導出它們的用途。

+0

我所遇到的一些選項,但想繼續之前具體地說約OO知道。順便說一句,我正在看圖形輸出爲PDF。 – 2009-12-08 18:49:13