mirror of
https://github.com/Tichau/FileConverter.git
synced 2026-11-25 03:53:28 +01:00
Add possibility to convert word documents to images.
This commit is contained in:
1 parent
8831babb8c
commit
7daa49e95d
3 files changed
+165
-50
No files matched your search
@@ -19,7 +19,6 @@ namespace FileConverter.ConversionJobs
|
||||
private CancelConversionJobCommand cancelCommand;
|
||||
|
||||
private string initialInputPath = string.Empty;
|
||||
private string[] outputFilePaths;
|
||||
private int currentOuputFilePathIndex;
|
||||
|
||||
public ConversionJob()
|
||||
@@ -67,22 +66,22 @@ namespace FileConverter.ConversionJobs
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.outputFilePaths == null)
|
||||
if (this.OutputFilePaths == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (this.CurrentOuputFilePathIndex < 0)
|
||||
{
|
||||
return this.outputFilePaths[0];
|
||||
return this.OutputFilePaths[0];
|
||||
}
|
||||
|
||||
if (this.CurrentOuputFilePathIndex >= this.outputFilePaths.Length)
|
||||
if (this.CurrentOuputFilePathIndex >= this.OutputFilePaths.Length)
|
||||
{
|
||||
return this.outputFilePaths[this.outputFilePaths.Length - 1];
|
||||
return this.OutputFilePaths[this.OutputFilePaths.Length - 1];
|
||||
}
|
||||
|
||||
return this.outputFilePaths[this.CurrentOuputFilePathIndex];
|
||||
return this.OutputFilePaths[this.CurrentOuputFilePathIndex];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,12 +199,18 @@ namespace FileConverter.ConversionJobs
|
||||
}
|
||||
}
|
||||
|
||||
protected string[] OutputFilePaths
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public virtual bool CanStartConversion(ConversionFlags conversionFlags)
|
||||
{
|
||||
return (conversionFlags & ConversionFlags.CdDriveExtraction) == 0;
|
||||
}
|
||||
|
||||
public void PrepareConversion(string inputFilePath, string outputFilePath = null)
|
||||
public void PrepareConversion(string inputFilePath, params string[] outputFilePaths)
|
||||
{
|
||||
if (string.IsNullOrEmpty(inputFilePath))
|
||||
{
|
||||
@@ -229,12 +234,22 @@ namespace FileConverter.ConversionJobs
|
||||
this.initialInputPath = inputFilePath;
|
||||
this.InputFilePath = inputFilePath;
|
||||
|
||||
int outputFilesCount = this.GetOuputFilesCount();
|
||||
this.outputFilePaths = new string[outputFilesCount];
|
||||
|
||||
for (int index = 0; index < outputFilesCount; index++)
|
||||
this.OutputFilePaths = outputFilePaths;
|
||||
if (this.OutputFilePaths.Length == 0)
|
||||
{
|
||||
string path = outputFilePath ?? this.ConversionPreset.GenerateOutputFilePath(inputFilePath, index + 1, outputFilesCount);
|
||||
int outputFilesCount = this.GetOuputFilesCount();
|
||||
this.OutputFilePaths = new string[outputFilesCount];
|
||||
}
|
||||
|
||||
for (int index = 0; index < this.OutputFilePaths.Length; index++)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(this.OutputFilePaths[index]))
|
||||
{
|
||||
// Don't generate a path if it has already been set.
|
||||
continue;
|
||||
}
|
||||
|
||||
string path = this.ConversionPreset.GenerateOutputFilePath(inputFilePath, index + 1, this.OutputFilePaths.Length);
|
||||
|
||||
if (!PathHelpers.IsPathValid(path))
|
||||
{
|
||||
@@ -266,7 +281,7 @@ namespace FileConverter.ConversionJobs
|
||||
// Make the output path valid.
|
||||
try
|
||||
{
|
||||
path = PathHelpers.GenerateUniquePath(path, this.outputFilePaths);
|
||||
path = PathHelpers.GenerateUniquePath(path, this.OutputFilePaths);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
@@ -275,7 +290,7 @@ namespace FileConverter.ConversionJobs
|
||||
return;
|
||||
}
|
||||
|
||||
this.outputFilePaths[index] = path;
|
||||
this.OutputFilePaths[index] = path;
|
||||
}
|
||||
|
||||
this.CurrentOuputFilePathIndex = 0;
|
||||
@@ -372,9 +387,9 @@ namespace FileConverter.ConversionJobs
|
||||
{
|
||||
Debug.Log("Conversion Failed.");
|
||||
|
||||
for (int index = 0; index < this.outputFilePaths.Length; index++)
|
||||
for (int index = 0; index < this.OutputFilePaths.Length; index++)
|
||||
{
|
||||
string outputFilePath = this.outputFilePaths[index];
|
||||
string outputFilePath = this.OutputFilePaths[index];
|
||||
try
|
||||
{
|
||||
if (System.IO.File.Exists(outputFilePath))
|
||||
@@ -453,9 +468,9 @@ namespace FileConverter.ConversionJobs
|
||||
|
||||
private bool AllOuputFilesExists()
|
||||
{
|
||||
for (int index = 0; index < this.outputFilePaths.Length; index++)
|
||||
for (int index = 0; index < this.OutputFilePaths.Length; index++)
|
||||
{
|
||||
string outputFilePath = this.outputFilePaths[index];
|
||||
string outputFilePath = this.OutputFilePaths[index];
|
||||
if (!System.IO.File.Exists(outputFilePath))
|
||||
{
|
||||
return false;
|
||||
@@ -467,9 +482,9 @@ namespace FileConverter.ConversionJobs
|
||||
|
||||
private bool AtLeastOneOuputFilesExists()
|
||||
{
|
||||
for (int index = 0; index < this.outputFilePaths.Length; index++)
|
||||
for (int index = 0; index < this.OutputFilePaths.Length; index++)
|
||||
{
|
||||
string outputFilePath = this.outputFilePaths[index];
|
||||
string outputFilePath = this.OutputFilePaths[index];
|
||||
if (System.IO.File.Exists(outputFilePath))
|
||||
{
|
||||
return true;
|
||||
|
||||
@@ -2,19 +2,43 @@
|
||||
|
||||
namespace FileConverter.ConversionJobs
|
||||
{
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Word = Microsoft.Office.Interop.Word;
|
||||
|
||||
public class ConversionJob_Word : ConversionJob
|
||||
{
|
||||
private static Microsoft.Office.Interop.Word.Application wordApplication;
|
||||
private static Word.Application wordApplication;
|
||||
private static int wordApplicationRefCount = 0;
|
||||
|
||||
private string intermediateFilePath = string.Empty;
|
||||
private ConversionJob pdf2ImageConversionJob = null;
|
||||
private Word.Document document;
|
||||
|
||||
public ConversionJob_Word() : base()
|
||||
{
|
||||
ConversionJob_Word.InitializeWordApplicationInstance();
|
||||
}
|
||||
|
||||
public ConversionJob_Word(ConversionPreset conversionPreset) : base(conversionPreset)
|
||||
{
|
||||
ConversionJob_Word.InitializeWordApplicationInstance();
|
||||
}
|
||||
|
||||
protected override int GetOuputFilesCount()
|
||||
{
|
||||
if (this.ConversionPreset.OutputType == OutputType.Pdf)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
Diagnostics.Debug.Log("Load word document '{0}'.", this.InputFilePath);
|
||||
this.document = ConversionJob_Word.wordApplication.Documents.Open(this.InputFilePath, System.Reflection.Missing.Value, true);
|
||||
|
||||
int pagesCount = this.document.ComputeStatistics(Word.WdStatistic.wdStatisticPages);
|
||||
|
||||
return pagesCount;
|
||||
}
|
||||
|
||||
protected override void Initialize()
|
||||
@@ -23,15 +47,95 @@ namespace FileConverter.ConversionJobs
|
||||
|
||||
if (this.ConversionPreset == null)
|
||||
{
|
||||
throw new Exception("The conversion preset must be valid.");
|
||||
throw new System.Exception("The conversion preset must be valid.");
|
||||
}
|
||||
|
||||
if (this.ConversionPreset.OutputType != OutputType.Pdf)
|
||||
// Initialize converters.
|
||||
if (this.ConversionPreset.OutputType == OutputType.Pdf)
|
||||
{
|
||||
this.ConversionFailed("This conversion is not allowed");
|
||||
return;
|
||||
this.intermediateFilePath = this.OutputFilePath;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Generate intermediate file path.
|
||||
string fileName = Path.GetFileNameWithoutExtension(this.InputFilePath);
|
||||
string tempPath = Path.GetTempPath();
|
||||
this.intermediateFilePath = PathHelpers.GenerateUniquePath(tempPath + fileName + ".pdf");
|
||||
|
||||
ConversionPreset intermediatePreset = new ConversionPreset("Pdf to image", this.ConversionPreset, "pdf");
|
||||
this.pdf2ImageConversionJob = ConversionJobFactory.Create(intermediatePreset, this.intermediateFilePath);
|
||||
this.pdf2ImageConversionJob.PrepareConversion(this.intermediateFilePath, this.OutputFilePaths);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Convert()
|
||||
{
|
||||
if (this.ConversionPreset == null)
|
||||
{
|
||||
throw new System.Exception("The conversion preset must be valid.");
|
||||
}
|
||||
|
||||
this.UserState = Properties.Resources.ConversionStateReadDocument;
|
||||
|
||||
if (this.document == null)
|
||||
{
|
||||
Diagnostics.Debug.Log("Load word document '{0}'.", this.InputFilePath);
|
||||
this.document = ConversionJob_Word.wordApplication.Documents.Open(this.InputFilePath, System.Reflection.Missing.Value, true);
|
||||
}
|
||||
|
||||
// Make this document the active document.
|
||||
this.document.Activate();
|
||||
|
||||
this.UserState = Properties.Resources.ConversionStateConversion;
|
||||
|
||||
Diagnostics.Debug.Log("Load word document to pdf.");
|
||||
this.document.SaveAs(this.intermediateFilePath, Word.WdSaveFormat.wdFormatPDF);
|
||||
|
||||
Diagnostics.Debug.Log("Close word document '{0}'.", this.InputFilePath);
|
||||
this.document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
|
||||
this.document = null;
|
||||
|
||||
System.Threading.Interlocked.Decrement(ref ConversionJob_Word.wordApplicationRefCount);
|
||||
if (ConversionJob_Word.wordApplicationRefCount == 0)
|
||||
{
|
||||
Diagnostics.Debug.Log("Quit word application via interop.");
|
||||
ConversionJob_Word.wordApplication.Quit();
|
||||
}
|
||||
|
||||
if (this.pdf2ImageConversionJob != null)
|
||||
{
|
||||
if (!System.IO.File.Exists(this.intermediateFilePath))
|
||||
{
|
||||
this.ConversionFailed(Properties.Resources.ErrorCantFindOutputFiles);
|
||||
return;
|
||||
}
|
||||
|
||||
Task updateProgress = this.UpdateProgress();
|
||||
|
||||
Diagnostics.Debug.Log("Convert pdf to images.");
|
||||
|
||||
this.pdf2ImageConversionJob.StartConvertion();
|
||||
|
||||
if (this.pdf2ImageConversionJob.State != ConversionState.Done)
|
||||
{
|
||||
this.ConversionFailed(this.pdf2ImageConversionJob.ErrorMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(this.intermediateFilePath))
|
||||
{
|
||||
Diagnostics.Debug.Log("Delete intermediate file {0}.", this.intermediateFilePath);
|
||||
|
||||
File.Delete(this.intermediateFilePath);
|
||||
}
|
||||
|
||||
updateProgress.Wait();
|
||||
}
|
||||
}
|
||||
|
||||
private static void InitializeWordApplicationInstance()
|
||||
{
|
||||
// Initialize word application.
|
||||
System.Threading.Interlocked.Increment(ref ConversionJob_Word.wordApplicationRefCount);
|
||||
if (ConversionJob_Word.wordApplication == null)
|
||||
{
|
||||
@@ -43,32 +147,23 @@ namespace FileConverter.ConversionJobs
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Convert()
|
||||
private async Task UpdateProgress()
|
||||
{
|
||||
if (this.ConversionPreset == null)
|
||||
while (this.pdf2ImageConversionJob.State != ConversionState.Done &&
|
||||
this.pdf2ImageConversionJob.State != ConversionState.Failed)
|
||||
{
|
||||
throw new Exception("The conversion preset must be valid.");
|
||||
}
|
||||
if (this.pdf2ImageConversionJob != null && this.pdf2ImageConversionJob.State == ConversionState.InProgress)
|
||||
{
|
||||
this.Progress = this.pdf2ImageConversionJob.Progress;
|
||||
}
|
||||
|
||||
this.UserState = Properties.Resources.ConversionStateReadDocument;
|
||||
if (this.pdf2ImageConversionJob != null && this.pdf2ImageConversionJob.State == ConversionState.InProgress)
|
||||
{
|
||||
this.Progress = this.pdf2ImageConversionJob.Progress;
|
||||
this.UserState = this.pdf2ImageConversionJob.UserState;
|
||||
}
|
||||
|
||||
object inputFilePath = this.InputFilePath;
|
||||
Microsoft.Office.Interop.Word.Document document = ConversionJob_Word.wordApplication.Documents.Open(ref inputFilePath);
|
||||
|
||||
// Make this document the active document.
|
||||
document.Activate();
|
||||
|
||||
this.UserState = Properties.Resources.ConversionStateConversion;
|
||||
|
||||
object outputFilePath = this.OutputFilePath;
|
||||
object outputFileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
|
||||
document.SaveAs(ref outputFilePath, ref outputFileFormat);
|
||||
|
||||
System.Threading.Interlocked.Decrement(ref ConversionJob_Word.wordApplicationRefCount);
|
||||
if (ConversionJob_Word.wordApplicationRefCount == 0)
|
||||
{
|
||||
Diagnostics.Debug.Log("Quit word application via interop.");
|
||||
ConversionJob_Word.wordApplication.Quit();
|
||||
await Task.Delay(40);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace FileConverter
|
||||
this.Name = Properties.Resources.DefaultPresetName;
|
||||
}
|
||||
|
||||
public ConversionPreset(string name, OutputType outputType, string[] inputTypes)
|
||||
public ConversionPreset(string name, OutputType outputType, params string[] inputTypes)
|
||||
{
|
||||
this.Name = name;
|
||||
this.OutputType = outputType;
|
||||
@@ -41,7 +41,7 @@ namespace FileConverter
|
||||
this.outputFileNameTemplate = "(p)(f)";
|
||||
}
|
||||
|
||||
public ConversionPreset(string name, ConversionPreset source)
|
||||
public ConversionPreset(string name, ConversionPreset source, params string[] additionalInputTypes)
|
||||
{
|
||||
this.Name = name;
|
||||
this.OutputType = source.outputType;
|
||||
@@ -51,6 +51,11 @@ namespace FileConverter
|
||||
inputTypeList.AddRange(source.inputTypes);
|
||||
}
|
||||
|
||||
if (additionalInputTypes != null)
|
||||
{
|
||||
inputTypeList.AddRange(additionalInputTypes);
|
||||
}
|
||||
|
||||
this.InputTypes = inputTypeList;
|
||||
|
||||
this.outputFileNameTemplate = source.OutputFileNameTemplate;
|
||||
|
||||
Reference in new issue
Block a user