示例#1
0
        private Workbook(Uri uri)
        {
            if (uri.Scheme.ToLower() == "file")
            {
                if (!File.Exists(uri.LocalPath))
                {
                    throw new FileNotFoundException(uri.LocalPath);
                }
            }

            var aLoader = OfficeServiceManager.loader;

            Peer = (XSpreadsheetDocument)aLoader.loadComponentFromURL(
                uri.AbsoluteUri, "_blank", 0,
                new[] {
                new PropertyValue()
                {
                    Name  = "Hidden",
                    Value = new uno.Any(true)
                },
                new PropertyValue()
                {
                    Name  = "MacroExecutionMode",
                    Value = new uno.Any(MacroExecMode.FROM_LIST_NO_WARN)
                }
            }
                );

            this.storePeer       = (XStorable)Peer;
            this.closePeer       = (XCloseable)Peer;
            this.propSetPeer     = (XPropertySet)Peer;
            this.FormatsSupplier = (XNumberFormatsSupplier)Peer;
        }
示例#2
0
 private void storeDocument(XComponent document, String outputUrl, IDictionary storeProperties)
 {
     try
     {
         XStorable storable = (XStorable)document;
         storable.storeToURL(outputUrl, ToPropertyValues(storeProperties));
     }
     finally
     {
         XCloseable closeable = (XCloseable)document;
         if (closeable != null)
         {
             try
             {
                 closeable.close(true);
             }
             catch (CloseVetoException closeVetoException)
             {
                 Logger.Warn("document.close() vetoed:" + closeVetoException.Message);
             }
         }
         else
         {
             document.dispose();
         }
     }
 }
    private static void ConvertToPDF(string serverHost, int serverPort, Uri inputFile, Uri outputFile)
    {
        // FIX: Workaround for a known bug: XUnoUrlResolver forgets to call WSAStartup. We can use dummy Socket for that:
        using (var dummySocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP))
        {
            // First, initialize local service manager (IoC container of some kind):
            XComponentContext      componentContext    = Bootstrap.bootstrap();
            XMultiComponentFactory localServiceManager = componentContext.getServiceManager();
            XUnoUrlResolver        urlResolver         = (XUnoUrlResolver)localServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", componentContext);

            // Connect to LibreOffice server
            // URL format explained here: https://wiki.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Starting_OpenOffice.org_in_Listening_Mode
            string connectionString = string.Format("uno:socket,host={0},port={1};urp;StarOffice.ComponentContext", serverHost, serverPort);
            object initialObject    = urlResolver.resolve(connectionString);

            // Retrieve remote service manager:
            XComponentContext      remoteContext          = (XComponentContext)initialObject;
            XMultiComponentFactory remoteComponentFactory = remoteContext.getServiceManager();

            // Request our own instance of LibreOffice Writer from the server:
            object           oDesktop = remoteComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", remoteContext);
            XComponentLoader xCLoader = (XComponentLoader)oDesktop;

            var loaderArgs = new PropertyValue[1];
            loaderArgs[0]       = new PropertyValue();
            loaderArgs[0].Name  = "Hidden"; // No GUI
            loaderArgs[0].Value = new Any(true);

            string inputFileUrl = inputFile.AbsoluteUri;

            // WARNING: LibreOffice .NET API uses .NET Remoting and does NOT throw clean and actionable errors,
            //          so, if server crashes or input file is locked or whatever, you will get nulls after casting.
            //          For example, `(XStorable)xCLoader` cast may produce null.
            //          This is counter-intuitive, I know; be more careful in production-grade code and check for nulls after every cast.

            XStorable xStorable = (XStorable)xCLoader.loadComponentFromURL(inputFileUrl, "_blank", 0, loaderArgs);

            var writerArgs = new PropertyValue[2];
            writerArgs[0]       = new PropertyValue();
            writerArgs[0].Name  = "Overwrite"; // Overwrite outputFile if it already exists
            writerArgs[0].Value = new Any(true);
            writerArgs[1]       = new PropertyValue();
            writerArgs[1].Name  = "FilterName";
            writerArgs[1].Value = new Any("writer_pdf_Export");  // Important! This filter is doing all PDF stuff.

            string outputFileUrl = outputFile.AbsoluteUri;

            xStorable.storeToURL(outputFileUrl, writerArgs);

            XCloseable xCloseable = (XCloseable)xStorable;
            if (xCloseable != null)
            {
                xCloseable.close(false);
            }
        }
    }
示例#4
0
 //ファイルを閉じる
 public void CloseFile()
 {
     // Calcファイルを閉じる
     if (doc != null)
     {
         xCloseable = (XCloseable)doc;
         xCloseable.close(true);
         doc = null;
     }
 }
示例#5
0
        /// <summary>
        /// Try to close the frame instead of the document.
        /// It shows the possible interface to do so.
        /// </summary>
        /// <param name="xFrame">frame which should be clcosed</param>
        /// <returns>
        /// <TRUE/>
        ///  in case frame could be closed
        /// <FALSE/>
        ///  otherwise
        /// </returns>
        public static bool CloseFrame(XFrame xFrame)
        {
            bool bClosed = false;

            try
            {
                // first try the new way: use new interface XCloseable
                // It replace the deprecated XTask::close() and should be preferred ...
                // if it can be queried.
                XCloseable xCloseable = (XCloseable)xFrame;
                if (xCloseable != null)
                {
                    // We deliver the owner ship of this frame not to the (possible)
                    // source which throw a CloseVetoException. We whish to have it
                    // under our own control.
                    try
                    {
                        xCloseable.close(false);
                        bClosed = true;
                    }
                    catch (CloseVetoException)
                    {
                        bClosed = false;
                    }
                }
                else
                {
                    // OK: the new way isn't possible. Try the old one.
                    XTask xTask = (XTask)xFrame;
                    if (xTask != null)
                    {
                        // return value doesn't interest here. Because
                        // we forget this task ...
                        bClosed = xTask.close();
                    }
                }
            }
            catch (DisposedException)
            {
                // Of course - this task can be already dead - means disposed.
                // But for us it's not important. Because we tried to close it too.
                // And "already disposed" or "closed" should be the same ...
                bClosed = true;
            }

            return(bClosed);
        }
示例#6
0
 public static void CloseFile(XCloseable XCloseable)
 {
     XCloseable.close(true);
 }