예제 #1
0
        /// <summary>
        /// Compare two documents with LibreOffice
        /// </summary>
        public void Compare(string filePath, string fileToComparePath, string targetFilePath, int?timeForWaiting = null)
        {
            if (!File.Exists(filePath))
            {
                throw new ArgumentException("File for comparison doesn't exist");
            }
            if (!File.Exists(fileToComparePath))
            {
                throw new ArgumentException("File to compare doesn't exist");
            }
            if (File.Exists(targetFilePath))
            {
                throw new ArgumentException("The target file exists");
            }

            var tempDirPath           = Path.GetTempPath().TrimEnd(Path.DirectorySeparatorChar);
            var tempFilePath          = Path.Combine(tempDirPath, Path.GetRandomFileName());
            var tempFileToComparePath = Path.Combine(tempDirPath, Path.GetRandomFileName());

            File.Copy(filePath, tempFilePath);
            File.Copy(fileToComparePath, tempFileToComparePath);

            var worker = new LibreOfficeWorker();

            worker.DoWork($"macro:///LibreOfficeLibrary.Module1.CompareDocuments(\"{tempFilePath}\",\"{tempFileToComparePath}\")", timeForWaiting);

            File.Move(tempFilePath, targetFilePath);
        }
        /// <summary>
        /// Convert document to PDF format
        /// </summary>
        public void ConvertToPdf(string filePath, string targetPath)
        {
            if (!File.Exists(filePath))
            {
                throw new ArgumentException("The file doesn't exist");
            }
            if (File.Exists(targetPath))
            {
                throw new ArgumentException("The target file exists");
            }

            var tempDirPath        = Path.GetTempPath().TrimEnd(Path.DirectorySeparatorChar);
            var tempFilePath       = Path.Combine(tempDirPath, Path.GetRandomFileName());
            var tempOutputFilePath = Path.Combine(tempDirPath, Path.GetFileNameWithoutExtension(tempFilePath) + ".pdf");

            File.Copy(filePath, tempFilePath);

            var worker = new LibreOfficeWorker();

            worker.DoWork($"/C -headless -writer -convert-to pdf -outdir \"{tempDirPath}\" \"{tempFilePath}\"");

            if (File.Exists(tempOutputFilePath))
            {
                File.Move(tempOutputFilePath, targetPath);
            }
        }
예제 #3
0
        public void AcceptAllRevisions(string filePath, int?timeForWaiting = null)
        {
            if (!File.Exists(filePath))
            {
                throw new ArgumentException("The file doesn't exist");
            }

            var tempDirPath  = Path.GetTempPath().TrimEnd(Path.DirectorySeparatorChar);
            var tempFilePath = Path.Combine(tempDirPath, Path.GetRandomFileName());

            File.Copy(filePath, tempFilePath);

            var worker = new LibreOfficeWorker();

            worker.DoWork($"macro:///LibreOfficeLibrary.Module1.AcceptAllChanges(\"{tempFilePath}\")", timeForWaiting);

            File.Delete(filePath);
            File.Move(tempFilePath, filePath);
        }
        /// <summary>
        /// Convert document to PDF format
        /// </summary>
        public void ConvertToPdf(string filePath, string targetPath, string profileLocation = null)
        {
            if (!File.Exists(filePath))
            {
                throw new ArgumentException("The file doesn't exist");
            }
            if (File.Exists(targetPath))
            {
                throw new ArgumentException("The target file exists");
            }

            if (profileLocation == null)
            {
                var path       = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                var versionDir = Directory.GetDirectories(Path.Combine(path, "libreoffice")).FirstOrDefault();
                if (versionDir == null)
                {
                    return;
                }

                profileLocation = versionDir;
            }

            var profilePath        = Path.GetFullPath(profileLocation).Replace('\\', '/').Replace(" ", "%20");
            var tempDirPath        = Path.GetTempPath().TrimEnd(Path.DirectorySeparatorChar);
            var tempFilePath       = Path.Combine(tempDirPath, Path.GetRandomFileName());
            var tempOutputFilePath = Path.Combine(tempDirPath, Path.GetFileNameWithoutExtension(tempFilePath) + ".pdf");

            File.Copy(filePath, tempFilePath);

            var worker = new LibreOfficeWorker();

            worker.DoWork($"/C -headless -writer -convert-to pdf -outdir \"{tempDirPath}\" \"{tempFilePath}\" \"-env:UserInstallation=file:///{profilePath}/\"");

            if (File.Exists(tempOutputFilePath))
            {
                File.Move(tempOutputFilePath, targetPath);
            }
        }