diff --git a/Application/FileConverter/ConversionJobs/ConversionJobFactory.cs b/Application/FileConverter/ConversionJobs/ConversionJobFactory.cs index f49ebdc..0e8e660 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJobFactory.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJobFactory.cs @@ -6,9 +6,9 @@ namespace FileConverter.ConversionJobs { public static ConversionJob Create(ConversionPreset conversionPreset, string inputFilePath) { - string extension = System.IO.Path.GetExtension(inputFilePath); - extension = extension.ToLowerInvariant().Substring(1, extension.Length - 1); - if (extension == "cda") + string inputFileExtension = System.IO.Path.GetExtension(inputFilePath); + inputFileExtension = inputFileExtension.ToLowerInvariant().Substring(1, inputFileExtension.Length - 1); + if (inputFileExtension == "cda") { return new ConversionJob_ExtractCDA(conversionPreset); } @@ -23,11 +23,17 @@ namespace FileConverter.ConversionJobs return new ConversionJob_Gif(conversionPreset); } - if (Helpers.GetExtensionCategory(extension) == Helpers.InputCategoryNames.Image) + if (Helpers.GetExtensionCategory(inputFileExtension) == Helpers.InputCategoryNames.Image) { return new ConversionJob_ImageMagick(conversionPreset); } + if (conversionPreset.OutputType == OutputType.Pdf || + Helpers.GetExtensionCategory(inputFileExtension) == Helpers.InputCategoryNames.Document) + { + return new ConversionJob_Pdf(conversionPreset); + } + return new ConversionJob_FFMPEG(conversionPreset); } } diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_Pdf.cs b/Application/FileConverter/ConversionJobs/ConversionJob_Pdf.cs new file mode 100644 index 0000000..8360e9a --- /dev/null +++ b/Application/FileConverter/ConversionJobs/ConversionJob_Pdf.cs @@ -0,0 +1,93 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +namespace FileConverter.ConversionJobs +{ + using System; + + using FileConverter.Diagnostics; + using ImageMagick; + + public class ConversionJob_Pdf : ConversionJob + { + private int pageCount = 0; + + public ConversionJob_Pdf() : base() + { + } + + public ConversionJob_Pdf(ConversionPreset conversionPreset) : base(conversionPreset) + { + } + + protected override void Initialize() + { + base.Initialize(); + + string applicationDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); + MagickNET.SetGhostscriptDirectory(applicationDirectory); + + if (this.ConversionPreset == null) + { + throw new Exception("The conversion preset must be valid."); + } + } + + protected override int GetOuputFilesCount() + { + using (MagickImageCollection images = new MagickImageCollection()) + { + MagickReadSettings settings = new MagickReadSettings(); + settings.Density = new Density(1, 1); + images.Read(this.InputFilePath); + + return images.Count; + } + } + + protected override void Convert() + { + if (this.ConversionPreset == null) + { + throw new Exception("The conversion preset must be valid."); + } + + this.UserState = Properties.Resources.ConversionStateReadPdf; + + MagickReadSettings settings = new MagickReadSettings(); + // Settings the density to 300 dpi will create an image with a better quality + settings.Density = new Density(300, 300); + + using (MagickImageCollection images = new MagickImageCollection()) + { + // Add all the pages of the pdf file to the collection + images.Read(this.InputFilePath, settings); + Debug.Log("Load pdf {0} succeed.", this.InputFilePath); + + this.pageCount = images.Count; + + this.CurrentOuputFilePathIndex = 0; + + foreach (MagickImage image in images) + { + this.UserState = Properties.Resources.ConversionStateConversion; + + Debug.Log("Write page {0}/{1}.", this.CurrentOuputFilePathIndex + 1, this.pageCount); + + image.Progress += this.Image_Progress; + + // Write page to file that contains the page number + image.Write(this.OutputFilePath); + this.CurrentOuputFilePathIndex++; + + image.Progress -= this.Image_Progress; + } + } + } + + private void Image_Progress(object sender, ProgressEventArgs eventArgs) + { + float alreadyCompletedPages = this.CurrentOuputFilePathIndex / (float)this.pageCount; + this.Progress = alreadyCompletedPages + (float)eventArgs.Progress.ToDouble() / (100f * this.pageCount); + } + } +} diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index 52447d2..9f227db 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -47,7 +47,7 @@ - ..\..\packages\Magick.NET-Q16-AnyCPU.7.0.0.0101\lib\net40-client\Magick.NET-Q16-AnyCPU.dll + ..\..\packages\Magick.NET-Q16-AnyCPU.7.0.2.100\lib\net40-client\Magick.NET-Q16-AnyCPU.dll True @@ -101,6 +101,7 @@ + @@ -268,6 +269,8 @@ copy /Y "$(SolutionDir)Middleware\ffmpeg.exe" "$(TargetDir)ffmpeg.exe" +copy /Y "$(SolutionDir)Middleware\gsdll64.dll" "$(TargetDir)gsdll64.dll" +copy /Y "$(SolutionDir)Middleware\gswin64c.exe" "$(TargetDir)gswin64c.exe" copy /Y "$(ProjectDir)Settings.default.xml" "$(TargetDir)Settings.default.xml"