コード例 #1
0
 /// <summary>
 /// 文章转换为PDF格式
 /// </summary>
 /// <param name="OpenOffice_HOME">OpenOffice安装目录</param>
 /// <param name="docPath">文件目录</param>
 /// <param name="pdfPath">pdfPath输入目录</param>
 public static void DocToPdf(string OpenOffice_HOME, string docPath, string pdfPath)
 {
     OpenOfficeConnection connection = null;
     DocumentConverter converter = null;
     Process p = null;
     try
     {
         System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcesses();
         foreach (System.Diagnostics.Process process in processList)
         {
             if (process.ProcessName.Contains("soffice"))
             {
                 p = process;
                 break;
             }
         }
         if (p == null)
         {
             ProcessStartInfo FilestartInfo = new ProcessStartInfo(OpenOffice_HOME);
             FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
             FilestartInfo.Arguments = " -headless -accept=\"socket,host=127.0.0.1,port=8101;urp;\"";
             //转换
             p = Process.Start(FilestartInfo);
         }
         File inputFile = new File(docPath);
         File outputFile = new File(pdfPath);
         connection = new SocketOpenOfficeConnection("127.0.0.1", 8101);
         connection.connect();
         converter = new OpenOfficeDocumentConverter(connection);
         converter.convert(inputFile, outputFile);
     }
     catch (Exception)
     {
         throw;
     }
     finally
     {
         if (connection != null)
         {
             try
             {
                 connection.disconnect();
             }
             catch (Exception)
             {
             }
         }
         try
         {
             p.Kill();
         }
         catch (Exception)
         {
         }
     }
 }
コード例 #2
0
        static void Main(string[] arguments)
        {
            ICommandLineParser commandLineParser = new PosixParser();
            CommandLine        commandLine       = commandLineParser.Parse(Options, arguments);

            int port = SocketOpenOfficeConnection.DefaultPort;

            if (commandLine.HasOption(OptionPort.Opt))
            {
                port = Convert.ToInt32(commandLine.GetOptionValue(OptionPort.Opt));
            }

            String outputFormat = null;

            if (commandLine.HasOption(OptionOutputFormat.Opt))
            {
                outputFormat = commandLine.GetOptionValue(OptionOutputFormat.Opt);
            }

            bool verbose = commandLine.HasOption(OptionVerbose.Opt);

            IDocumentFormatRegistry registry = new DefaultDocumentFormatRegistry();

            String[] fileNames = commandLine.Args;
            if ((outputFormat == null && fileNames.Length != 2) || fileNames.Length < 1)
            {
                String        syntax        = "Convert [options] input-file output-file; or\n" + "[options] -f output-format input-file [input-file...]";
                HelpFormatter helpFormatter = new HelpFormatter();
                helpFormatter.PrintHelp(syntax, Options);
                Environment.Exit(ExitCodeTooFewArgs);
            }

            IOpenOfficeConnection connection = new SocketOpenOfficeConnection(port);
            OfficeInfo            oo         = EnvUtils.Get();

            if (oo.Kind == OfficeKind.Unknown)
            {
                Console.Out.WriteLine("please setup OpenOffice or LibreOffice!");
                return;
            }
            try
            {
                if (verbose)
                {
                    Console.Out.WriteLine("-- connecting to OpenOffice.org on port " + port);
                }
                connection.Connect();
            }
            catch (Exception)
            {
                string CmdArguments = string.Format("-headless -accept=\"socket,host={0},port={1};urp;\" -nofirststartwizard", SocketOpenOfficeConnection.DefaultHost, SocketOpenOfficeConnection.DefaultPort);
                if (!EnvUtils.RunCmd(oo.OfficeUnoPath, "soffice", CmdArguments))
                {
                    Console.Error.WriteLine("ERROR: connection failed. Please make sure OpenOffice.org is running and listening on port " + port + ".");
                    Environment.Exit(ExitCodeConnectionFailed);
                }
            }
            try
            {
                IDocumentConverter converter = new OpenOfficeDocumentConverter(connection, registry);
                if (outputFormat == null)
                {
                    FileInfo inputFile  = new FileInfo(fileNames[0]);
                    FileInfo outputFile = new FileInfo(fileNames[1]);
                    ConvertOne(converter, inputFile, outputFile, verbose);
                }
                else
                {
                    foreach (var t in fileNames)
                    {
                        var inputFile  = new FileInfo(t);
                        var outputFile = new FileInfo(inputFile.FullName.Remove(inputFile.FullName.LastIndexOf(".", StringComparison.Ordinal)) + "." + outputFormat);
                        ConvertOne(converter, inputFile, outputFile, verbose);
                    }
                }
            }
            finally
            {
                if (verbose)
                {
                    Console.Out.WriteLine("-- disconnecting");
                }
                connection.Disconnect();
            }
        }