Add converter from Images to Pdf

This commit is contained in:
Tichau committed 2016-07-05 08:42:00 +02:00
1 parent c01e618c55
commit b2bf4b7793
7 files changed
+53 -132

No files matched your search

@@ -29,11 +29,10 @@ namespace FileConverter.ConversionJobs
return new ConversionJob_ImageMagick(conversionPreset);
}
//if (conversionPreset.OutputType == OutputType.Pdf ||
// Helpers.GetExtensionCategory(inputFileExtension) == Helpers.InputCategoryNames.Document)
//{
// return new ConversionJob_Pdf(conversionPreset);
//}
if (conversionPreset.OutputType == OutputType.Pdf)
{
return new ConversionJob_ImageMagick(conversionPreset);
}
return new ConversionJob_FFMPEG(conversionPreset);
}
@@ -119,7 +119,7 @@ namespace FileConverter.ConversionJobs
{
image.Progress += this.Image_Progress;
if (!ignoreScale)
if (!ignoreScale && this.ConversionPreset.IsRelevantSetting(ConversionPreset.ConversionSettingKeys.ImageScale))
{
float scaleFactor = this.ConversionPreset.GetSettingsValue<float>(ConversionPreset.ConversionSettingKeys.ImageScale);
if (Math.Abs(scaleFactor - 1f) >= 0.005f)
@@ -130,38 +130,47 @@ namespace FileConverter.ConversionJobs
}
}
float rotateAngleInDegrees = this.ConversionPreset.GetSettingsValue<float>(ConversionPreset.ConversionSettingKeys.ImageRotation);
if (Math.Abs(rotateAngleInDegrees - 0f) >= 0.05f)
if (this.ConversionPreset.IsRelevantSetting(ConversionPreset.ConversionSettingKeys.ImageRotation))
{
Debug.Log("Apply rotation: {0}°.", rotateAngleInDegrees);
image.Rotate(rotateAngleInDegrees);
}
bool clampSizeToPowerOf2 = this.ConversionPreset.GetSettingsValue<bool>(ConversionPreset.ConversionSettingKeys.ImageClampSizePowerOf2);
if (clampSizeToPowerOf2)
{
int referenceSize = System.Math.Min(image.Width, image.Height);
int size = 2;
while (size*2 <= referenceSize)
float rotateAngleInDegrees = this.ConversionPreset.GetSettingsValue<float>(ConversionPreset.ConversionSettingKeys.ImageRotation);
if (Math.Abs(rotateAngleInDegrees - 0f) >= 0.05f)
{
size *= 2;
Debug.Log("Apply rotation: {0}°.", rotateAngleInDegrees);
image.Rotate(rotateAngleInDegrees);
}
Debug.Log("Clamp size to the nearest power of 2 size (from {0}x{1} to {2}x{2}).", image.Width, image.Height, size);
image.Scale(size, size);
}
int maximumSize = this.ConversionPreset.GetSettingsValue<int>(ConversionPreset.ConversionSettingKeys.ImageMaximumSize);
if (maximumSize > 0)
if (this.ConversionPreset.IsRelevantSetting(ConversionPreset.ConversionSettingKeys.ImageClampSizePowerOf2))
{
int width = System.Math.Min(image.Width, maximumSize);
int height = System.Math.Min(image.Height, maximumSize);
bool clampSizeToPowerOf2 = this.ConversionPreset.GetSettingsValue<bool>(ConversionPreset.ConversionSettingKeys.ImageClampSizePowerOf2);
if (clampSizeToPowerOf2)
{
int referenceSize = System.Math.Min(image.Width, image.Height);
int size = 2;
while (size*2 <= referenceSize)
{
size *= 2;
}
Debug.Log("Clamp size to maximum size of {2}x{2} (from {0}x{1} to {2}x{3}).", image.Width, image.Height, width, height);
Debug.Log("Clamp size to the nearest power of 2 size (from {0}x{1} to {2}x{2}).", image.Width, image.Height, size);
image.Scale(width, height);
image.Scale(size, size);
}
}
if (this.ConversionPreset.IsRelevantSetting(ConversionPreset.ConversionSettingKeys.ImageMaximumSize))
{
int maximumSize = this.ConversionPreset.GetSettingsValue<int>(ConversionPreset.ConversionSettingKeys.ImageMaximumSize);
if (maximumSize > 0)
{
int width = System.Math.Min(image.Width, maximumSize);
int height = System.Math.Min(image.Height, maximumSize);
Debug.Log("Clamp size to maximum size of {2}x{2} (from {0}x{1} to {2}x{3}).", image.Width, image.Height, width, height);
image.Scale(width, height);
}
}
Debug.Log("Convert image (output: {0}).", this.OutputFilePath);
@@ -175,6 +184,9 @@ namespace FileConverter.ConversionJobs
image.Quality = this.ConversionPreset.GetSettingsValue<int>(ConversionPreset.ConversionSettingKeys.ImageQuality);
break;
case OutputType.Pdf:
break;
default:
this.ConversionFailed($"Unsupported output format {this.ConversionPreset.OutputType}.");
image.Progress -= this.Image_Progress;
@@ -1,93 +0,0 @@
// <copyright file="ConversionJob_ImageMagick.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
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);
}
}
}
@@ -376,6 +376,11 @@ namespace FileConverter
return (T)System.Convert.ChangeType(settingsValue, type);
}
public bool IsRelevantSetting(string settingsKey)
{
return this.Settings.ContainsKey(settingsKey);
}
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
@@ -401,11 +406,6 @@ namespace FileConverter
}
}
private bool IsRelevantSetting(string settingsKey)
{
return this.Settings.ContainsKey(settingsKey);
}
private void InitializeDefaultSettings(OutputType outputType)
{
this.settings.Clear();
@@ -496,11 +496,14 @@ namespace FileConverter
this.InitializeSettingsValue(ConversionPreset.ConversionSettingKeys.VideoRotation, "0");
break;
case OutputType.Pdf:
break;
default:
throw new System.Exception("Missing default settings for type " + outputType);
}
this.OnPropertyChanged("Settings");
this.OnPropertyChanged(nameof(this.Settings));
}
private void InitializeSettingsValue(string settingsKey, string value, bool force = false)
@@ -101,7 +101,6 @@
<Compile Include="ConversionJobs\ConversionJob_FFMPEG.cs" />
<Compile Include="ConversionJobs\ConversionJob_Gif.cs" />
<Compile Include="ConversionJobs\ConversionJob_Ico.cs" />
<Compile Include="ConversionJobs\ConversionJob_Pdf.cs" />
<Compile Include="ConversionJobs\ConversionJob_ImageMagick.cs" />
<Compile Include="ConversionJobs\VideoEncodingSpeed.cs" />
<Compile Include="ConversionPreset\ConversionSettingsOverride.cs" />
+1 -1
View File
@@ -111,7 +111,7 @@ namespace FileConverter
return category == InputCategoryNames.Image || category == InputCategoryNames.Video || category == InputCategoryNames.AnimatedImage;
case OutputType.Pdf:
return category == InputCategoryNames.Image || category == InputCategoryNames.Document;
return category == InputCategoryNames.Image;
default:
return false;
@@ -59,6 +59,7 @@ namespace FileConverter
OutputType.Jpg,
OutputType.Png,
OutputType.Gif,
OutputType.Pdf,
};
this.OutputFormats.ItemsSource = outputTypes;
@@ -452,7 +453,7 @@ namespace FileConverter
}
this.inputCategories = categories.ToArray();
this.OnPropertyChanged("InputCategories");
this.OnPropertyChanged(nameof(this.InputCategories));
}
}
}