示例#1
0
        protected override IEnumerable <DestinationFile> ConvertApplicableImageThenSave(
            SourceFile source,
            ImageFileKind destinationKind,
            ArgumentOptionCollection optionCollection,
            ILogger logger)
        {
            foreach (var savedEmf in this.powerpoint2Emf.ConvertThenSave(source, ImageFileKind.Emf, optionCollection, logger))
            {
                var sourceEmf = savedEmf.AsSourceFile();
                foreach (var savedEps in this.emf2Eps.ConvertThenSave(sourceEmf, destinationKind, optionCollection, logger))
                {
                    yield return(savedEps);
                }

                // Delete tempolary EMF image
                try
                {
                    File.Delete(sourceEmf.Path);
                }
                catch (Exception e)
                {
                    logger.WriteLog($"Failed to delete tempolary emf file: {e.Message}", LogLevel.Error);
                    continue;
                }
            }
        }
        public void TestLoadArgumentOptionCollection_Combination()
        {
            var logger         = new LoggerImpl();
            var argumentTokens = new[] {
                new ArgumentToken("-w"),
                new ArgumentToken("-r"),
                new ArgumentToken("-t"),
                new ArgumentToken("eps"),
                new ArgumentToken("-p"),
                new ArgumentToken("5"),
                new ArgumentToken("10"),
                new ArgumentToken("-L"),
                new ArgumentToken("debug"),
                new ArgumentToken("slide.pptx")
            };

            var(optionCollection, remainningTokens) = ArgumentOptionCollection.LoadArgumentOptionCollection(argumentTokens, logger);

            Assert.IsTrue(optionCollection.AllowDestinationOverwrite);
            Assert.IsTrue(optionCollection.EnableRecursiveDirectorySearch);
            Assert.AreEqual(ImageFileKind.Eps, optionCollection.DestinationImageFileKind);
            Assert.AreEqual(new Range <uint>(5, 10), optionCollection.PowerpointPageRange);
            Assert.IsFalse(optionCollection.EnableVersionInfoDisplay);

            Assert.AreEqual(1, remainningTokens.Count);
            Assert.AreEqual(new ArgumentToken("slide.pptx"), remainningTokens[0]);
        }
        public void TestLoadArgumentOptionCollection_LogLevel_InvalidLevel()
        {
            var logger         = new LoggerImpl();
            var argumentTokens = new[] { new ArgumentToken("--log-level"), new ArgumentToken("carropino"), new ArgumentToken("file1.png") };

            Assert.ThrowsException <ArgumentException>(delegate { ArgumentOptionCollection.LoadArgumentOptionCollection(argumentTokens, logger); });

            Assert.AreEqual(LogLevel.Error, logger.LogLevelOfLastLog);
        }
        public void TestLoadArgumentOptionCollection_PowerpointPageRange_ParseFail()
        {
            var logger         = new LoggerImpl();
            var argumentTokens = new[] { new ArgumentToken("-p"), new ArgumentToken("5"), new ArgumentToken("aiueo") };

            Assert.ThrowsException <ArgumentException>(delegate { ArgumentOptionCollection.LoadArgumentOptionCollection(argumentTokens, logger); });

            Assert.AreEqual(LogLevel.Error, logger.LogLevelOfLastLog);
        }
        public void TestLoadArgumentOptionCollection_UnexpectedOption()
        {
            var logger         = new LoggerImpl();
            var argumentTokens = new[] { new ArgumentToken("--carro-pino"), new ArgumentToken("file1.png") };

            Assert.ThrowsException <ArgumentException>(delegate { ArgumentOptionCollection.LoadArgumentOptionCollection(argumentTokens, logger); });

            Assert.AreEqual(LogLevel.Error, logger.LogLevelOfLastLog);
            Assert.AreEqual("Unexpected option \"--carro-pino\"", logger.LastLog);
        }
        public void TestLoadArgumentOptionCollection_DestinationImageFileKind_NoTypeFound()
        {
            var logger = new LoggerImpl();
            // 'txt' file is invalid for output image type!
            var argumentTokens = new[] { new ArgumentToken("-t") };

            Assert.ThrowsException <ArgumentException>(delegate { ArgumentOptionCollection.LoadArgumentOptionCollection(argumentTokens, logger); });

            Assert.AreEqual(LogLevel.Error, logger.LogLevelOfLastLog);
            Assert.AreEqual(logger.LastLog, "Output type must be specified after \"-t\" or \"--output-type\" option.");
        }
        public void TestLoadArgumentOptionCollection_DestinationImageFileKind_InvalidType()
        {
            var logger = new LoggerImpl();
            // 'txt' file is invalid for output image type!
            var argumentTokens = new[] { new ArgumentToken("-t"), new ArgumentToken("txt"), new ArgumentToken("file1.bmp") };

            Assert.ThrowsException <ArgumentException>(delegate { ArgumentOptionCollection.LoadArgumentOptionCollection(argumentTokens, logger); });

            Assert.AreEqual(LogLevel.Error, logger.LogLevelOfLastLog);
            Assert.AreEqual(logger.LastLog, "Invalid output type: txt");
        }
示例#8
0
        protected override IEnumerable <DestinationFile> ConvertApplicableImageThenSave(
            SourceFile source,
            ImageFileKind destinationKind,
            ArgumentOptionCollection optionCollection,
            ILogger logger)
        {
            var savedImageFiles = new List <DestinationFile>();

            try
            {
                using (var powerPointApplication = new PowerPointApplication(new Application()))
                {
                    var application  = powerPointApplication.Application;
                    var presentation = application.Presentations;
                    using (var presenationFile = new PowerPointPresentation(presentation.Open(
                                                                                FileName: source.Path,
                                                                                ReadOnly: MsoTriState.msoTrue,
                                                                                Untitled: MsoTriState.msoTrue,
                                                                                WithWindow: MsoTriState.msoTrue)))
                    {
                        var file = presenationFile.Presentation;
                        foreach (var slide in EnumeratePowerPointSlides(file))
                        {
                            if (!optionCollection.PowerpointPageRange.Contains((uint)slide.SlideNumber))
                            {
                                continue;
                            }
                            slide.Select();
                            slide.Shapes.SelectAll();
                            var selection = application.ActiveWindow.Selection;
                            var shapes    = selection.ShapeRange;

                            var destinationPathForSlide = $"{Path.GetDirectoryName(source.Path)}\\{Path.GetFileNameWithoutExtension(source.Path)}{slide.SlideNumber}{destinationKind.GetExtensionWithDot()}";
                            var destinationForSlide     = new DestinationFile(destinationPathForSlide);
                            if (!DeleteExistingFileIfNecessary(destinationForSlide, optionCollection, logger))
                            {
                                continue;
                            }

                            //create emf
                            shapes.Export(destinationForSlide.Path, PpShapeFormat.ppShapeFormatEMF);
                            savedImageFiles.Add(destinationForSlide);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                logger.WriteLog($"Failed to convert powerpoint slide to emf images: {e.Message}\n{e.StackTrace}", LogLevel.Error);
            }

            return(savedImageFiles);
        }
        public void TestLoadArgumentOptionCollection_NoToken()
        {
            var logger         = new LoggerImpl();
            var argumentTokens = new ArgumentToken[0];

            var(optionCollection, remainningTokens) = ArgumentOptionCollection.LoadArgumentOptionCollection(argumentTokens, logger);

            Assert.IsFalse(optionCollection.AllowDestinationOverwrite);
            Assert.IsFalse(optionCollection.EnableRecursiveDirectorySearch);
            Assert.AreEqual(ImageFileKind.Pdf, optionCollection.DestinationImageFileKind);
            Assert.AreEqual(new Range <uint>(uint.MinValue, uint.MaxValue), optionCollection.PowerpointPageRange);
            Assert.IsFalse(optionCollection.EnableVersionInfoDisplay);

            Assert.AreEqual(0, remainningTokens.Count);
        }
        public void TestLoadArgumentOptionCollection_PowerpointPageRange_LongOption()
        {
            var logger         = new LoggerImpl();
            var argumentTokens = new[] { new ArgumentToken("--powerpoint-page-range"), new ArgumentToken("3"), new ArgumentToken("5"), new ArgumentToken("slide.pptx") };

            var(optionCollection, remainningTokens) = ArgumentOptionCollection.LoadArgumentOptionCollection(argumentTokens, logger);

            Assert.IsFalse(optionCollection.AllowDestinationOverwrite);
            Assert.IsFalse(optionCollection.EnableRecursiveDirectorySearch);
            Assert.AreEqual(ImageFileKind.Pdf, optionCollection.DestinationImageFileKind);
            Assert.AreEqual(new Range <uint>(3, 5), optionCollection.PowerpointPageRange);
            Assert.IsFalse(optionCollection.EnableVersionInfoDisplay);

            Assert.AreEqual(1, remainningTokens.Count);
            Assert.AreEqual(new ArgumentToken("slide.pptx"), remainningTokens[0]);
        }
        public void TestLoadArgumentOptionCollection_DestinationImageFileKind_LongOption()
        {
            var logger         = new LoggerImpl();
            var argumentTokens = new[] { new ArgumentToken("--output-type"), new ArgumentToken("eps"), new ArgumentToken("file1.bmp") };

            var(optionCollection, remainningTokens) = ArgumentOptionCollection.LoadArgumentOptionCollection(argumentTokens, logger);

            Assert.IsFalse(optionCollection.AllowDestinationOverwrite);
            Assert.IsFalse(optionCollection.EnableRecursiveDirectorySearch);
            Assert.AreEqual(ImageFileKind.Eps, optionCollection.DestinationImageFileKind);
            Assert.AreEqual(new Range <uint>(uint.MinValue, uint.MaxValue), optionCollection.PowerpointPageRange);
            Assert.IsFalse(optionCollection.EnableVersionInfoDisplay);

            Assert.AreEqual(1, remainningTokens.Count);
            Assert.AreEqual(new ArgumentToken("file1.bmp"), remainningTokens[0]);
        }
示例#12
0
        protected override IEnumerable <DestinationFile> ConvertApplicableImageThenSave(
            SourceFile source,
            ImageFileKind destinationKind,
            ArgumentOptionCollection optionCollection,
            ILogger logger)
        {
            foreach (var tempEps in this.emf2Eps.ConvertThenSave(source, ImageFileKind.Eps, optionCollection, logger))
            {
                var tempEpsSource = tempEps.AsSourceFile();

                foreach (var savedImage in this.eps2Pdf.ConvertThenSave(tempEpsSource, destinationKind, optionCollection, logger))
                {
                    yield return(savedImage);
                }
            }
        }
示例#13
0
        protected override IEnumerable <DestinationFile> ConvertApplicableImageThenSave(
            SourceFile source,
            ImageFileKind destinationKind,
            ArgumentOptionCollection optionCollection,
            ILogger logger)
        {
            var image       = Image.GetInstance(source.Path);
            var destination = source.GetConvertionDestination(destinationKind);

            if (!DeleteExistingFileIfNecessary(destination, optionCollection, logger))
            {
                yield break;
            }

            try
            {
                using (var fileStream = new FileStream(destination.Path, FileMode.Create, FileAccess.Write))
                {
                    var document = new Document(
                        pageSize: new Rectangle(image.Width, image.Height),
                        marginLeft: 0f,
                        marginRight: 0f,
                        marginTop: 0f,
                        marginBottom: 0f);
                    image.SetAbsolutePosition(0f, 0f);
                    PdfWriter.GetInstance(document, fileStream);
                    document.Open();
                    document.Add(image);
                    document.Close();
                }
            }
            catch (Exception e)
            {
                logger.WriteLog($"Failed to convert an image: {e.Message}", LogLevel.Error);
                yield break;
            }

            yield return(destination);
        }
示例#14
0
        protected static bool DeleteExistingFileIfNecessary(DestinationFile destination, ArgumentOptionCollection optionCollection, ILogger logger)
        {
            try
            {
                if (File.Exists(destination.Path))
                {
                    if (optionCollection.AllowDestinationOverwrite)
                    {
                        File.Delete(destination.Path);
                        logger.WriteLog($"A destination image {destination.Path} has existed so been deleted.", LogLevel.Warn);
                    }
                    else
                    {
                        logger.WriteLog($"A destination image {destination.Path} has existed.", LogLevel.Error);
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                logger.WriteLog($"Cannot convert an image: {e.Message}", LogLevel.Error);
                return(false);
            }

            return(true);
        }
示例#15
0
 protected abstract IEnumerable <DestinationFile> ConvertApplicableImageThenSave(
     SourceFile source,
     ImageFileKind destinationKind,
     ArgumentOptionCollection optionCollection,
     ILogger logger);
示例#16
0
        public IEnumerable <DestinationFile> ConvertThenSave(SourceFile source, ImageFileKind destinationKind, ArgumentOptionCollection optionCollection, ILogger logger)
        {
            if (!this.GetApplicableImageFileKinds().Contains(source.ImageFileKind))
            {
                logger.WriteLog($"Unsuitable source image kind: {source.ImageFileKind}", LogLevel.Error);
                yield break;
            }
            if (!this.GetAvailableDestinationImageFileKinds().Contains(destinationKind))
            {
                logger.WriteLog($"Unsuitable source image kind: {destinationKind}", LogLevel.Error);
                yield break;
            }

            foreach (var savedDestination in this.ConvertApplicableImageThenSave(source, destinationKind, optionCollection, logger))
            {
                yield return(savedDestination);
            }
        }