forked from hadasg/RIP2Image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConverterService.cs
689 lines (580 loc) · 27.9 KB
/
ConverterService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*******************************************************************************
RIP2Image is a program that efficiently converts formats such as PDF or Postscript to image formats such as Jpeg or PNG.
Copyright (C) 2013 XMPie Ltd.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This license covers only the RIP2Image files and not any file that RIP2Image links against or otherwise uses.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Threading;
using System.Drawing;
using System.Drawing.Imaging;
// using log4net;
// using log4net.Config;
/*[assembly: log4net.Config.XmlConfigurator(Watch = true)]*/
namespace RIP2Image
{
/// <summary>
/// Uniting all convert utilities.
/// </summary>
class ConverterService : IConverterService
{
//private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// Constructor.
/// </summary>
public ConverterService()
{
}
#region Convert methods
public bool ConvertPDF2JPG(string inConvertFilePath,
string inNewFileTargetFolderPath,
double inResolutionX,
double inResolutionY,
double inGraphicsAlphaBitsValue,
double inTextAlphaBitsValue,
double inQuality)
{
// logger.Info("inConvertFilePath = " + inConvertFilePath + ", inNewFileTargetFolderPath = " + inNewFileTargetFolderPath +
// ", inResolutionX = " + inResolutionX + ", inResolutionY = " + inResolutionY + ", inGraphicsAlphaBitsValue = " + inGraphicsAlphaBitsValue +
// ", inTextAlphaBitsValue = " + inTextAlphaBitsValue + ", inQuality = " + inQuality);
bool conversionSucceed;
CheckJPGParamValidation(inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue, inQuality);
// Make the conversion.
FileConverter fileConvertor = InstancesManager.GetObject(InstancesManager.ConversionType.PDF2JPG);
conversionSucceed = fileConvertor.ConvertPDF2JPG(inConvertFilePath, inNewFileTargetFolderPath, inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue, inQuality);
InstancesManager.PutObject(InstancesManager.ConversionType.PDF2JPG, fileConvertor);
// Rename JPG names to the correct page counter.
RenameImagesNames(inNewFileTargetFolderPath, inConvertFilePath, "jpg");
return conversionSucceed;
}
public bool ConvertPDF2PNG(string inConvertFilePath,
string inNewFileTargetFolderPath,
double inResolutionX,
double inResolutionY,
double inGraphicsAlphaBitsValue,
double inTextAlphaBitsValue)
{
// logger.Info("inConvertFilePath = " + inConvertFilePath + ", inNewFileTargetFolderPath = " + inNewFileTargetFolderPath +
// ", inResolutionX = " + inResolutionX + ", inResolutionY = " + inResolutionY + ", inGraphicsAlphaBitsValue = " + inGraphicsAlphaBitsValue +
// ", inTextAlphaBitsValue = " + inTextAlphaBitsValue + ");
bool conversionSucceed;
CheckBaseParamsValidation(inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue);
// Make the conversion.
FileConverter fileConvertor = InstancesManager.GetObject(InstancesManager.ConversionType.PDF2PNG);
conversionSucceed = fileConvertor.ConvertPDF2PNG(inConvertFilePath, inNewFileTargetFolderPath, inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue);
InstancesManager.PutObject(InstancesManager.ConversionType.PDF2PNG, fileConvertor);
// Rename PNG names to the correct page counter.
RenameImagesNames(inNewFileTargetFolderPath, inConvertFilePath, "png");
return conversionSucceed;
}
public bool ConvertPDF2PNGSingle(string inConvertFilePath,
string inNewFileTargetPath,
double inResolutionX,
double inResolutionY,
double inGraphicsAlphaBitsValue,
double inTextAlphaBitsValue)
{
// logger.Info("inConvertFilePath = " + inConvertFilePath + ", inNewFileTargetFolderPath = " + inNewFileTargetFolderPath +
// ", inResolutionX = " + inResolutionX + ", inResolutionY = " + inResolutionY + ", inGraphicsAlphaBitsValue = " + inGraphicsAlphaBitsValue +
// ", inTextAlphaBitsValue = " + inTextAlphaBitsValue + ");
bool conversionSucceed;
CheckBaseParamsValidation(inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue);
// Make the conversion.
FileConverter fileConvertor = InstancesManager.GetObject(InstancesManager.ConversionType.PDF2PNGSingle);
conversionSucceed = fileConvertor.ConvertPDF2PNGSingle(inConvertFilePath, inNewFileTargetPath, inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue);
InstancesManager.PutObject(InstancesManager.ConversionType.PDF2PNGSingle, fileConvertor);
return conversionSucceed;
}
public bool ConvertPDF2EPS(string inConvertFilePath, string inNewFileTargetPath, double inFirstPageToConvert, double inLastPageToConvert)
{
// logger.Info("inConvertFilePath = " + inConvertFilePath + ", inNewFileTargetPath = " + inNewFileTargetPath +
// ", inFirstPageToConvert = " + inFirstPageToConvert + ", inLastPageToConvert = " + inLastPageToConvert);
bool conversionSucceed;
// Make the conversion.
FileConverter fileConvertor = InstancesManager.GetObject(InstancesManager.ConversionType.PDF2EPS);
conversionSucceed = fileConvertor.ConvertPDF2EPS(inConvertFilePath, inNewFileTargetPath, inFirstPageToConvert, inLastPageToConvert);
InstancesManager.PutObject(InstancesManager.ConversionType.PDF2EPS, fileConvertor);
return conversionSucceed;
}
public bool ConvertEPS2PDF(string inConvertFilePath, string inNewFileTargetPath)
{
// logger.Info("inConvertFilePath = " + inConvertFilePath + ", inNewFileTargetPath = " + inNewFileTargetPath);
bool conversionSucceed;
// Make the conversion.
FileConverter fileConvertor = InstancesManager.GetObject(InstancesManager.ConversionType.EPS2PDF);
conversionSucceed = fileConvertor.ConvertEPS2PDF(inConvertFilePath, inNewFileTargetPath);
InstancesManager.PutObject(InstancesManager.ConversionType.EPS2PDF, fileConvertor);
return conversionSucceed;
}
public bool ConvertEPS2LowResPDF(string inConvertFilePath, string inNewFileTargetPath)
{
// logger.Info("inConvertFilePath = " + inConvertFilePath + ", inNewFileTargetPath = " + inNewFileTargetPath);
bool conversionSucceed;
// Make the conversion.
FileConverter fileConvertor = InstancesManager.GetObject(InstancesManager.ConversionType.EPS2LowResPDF);
conversionSucceed = fileConvertor.ConvertEPS2LowResPDF(inConvertFilePath, inNewFileTargetPath);
InstancesManager.PutObject(InstancesManager.ConversionType.EPS2LowResPDF, fileConvertor);
return conversionSucceed;
}
public bool ConvertPDF2LowResPDF(string inConvertFilePath, string inNewFileTargetPath)
{
// logger.Info("inConvertFilePath = " + inConvertFilePath + ", inNewFileTargetPath = " + inNewFileTargetPath);
bool conversionSucceed;
// Make the conversion.
FileConverter fileConvertor = InstancesManager.GetObject(InstancesManager.ConversionType.PDF2LowResPDF);
conversionSucceed = fileConvertor.ConvertPDF2LowResPDF(inConvertFilePath, inNewFileTargetPath);
InstancesManager.PutObject(InstancesManager.ConversionType.PDF2LowResPDF, fileConvertor);
return conversionSucceed;
}
public bool ConvertImage2LowResImage(string inConvertFilePath, string inNewFileTargetPath)
{
ImageFormat targetFormat = ImageFormat.Jpeg;
string typeStr = Path.GetExtension(inConvertFilePath).Substring(1).ToUpperInvariant();
switch (typeStr)
{
case "JPEG":
case "JPG":
targetFormat = ImageFormat.Jpeg;
break;
case "TIF":
case "TIFF":
targetFormat = ImageFormat.Tiff;
break;
case "BMP":
targetFormat = ImageFormat.Bmp;
break;
case "GIF":
targetFormat = ImageFormat.Gif;
break;
case "PNG":
targetFormat = ImageFormat.Png;
break;
}
try
{
using (Bitmap bitmap = new Bitmap(inConvertFilePath))
{
float targetResolution = 72;
// 96 means that windows might have failed reading the original resolution so we don't change it
if (bitmap.HorizontalResolution == 96 && bitmap.VerticalResolution == 96)
return false;
if (bitmap.HorizontalResolution <= 72 || bitmap.VerticalResolution <= 72)
return false;
using (Bitmap smallBitmap = new Bitmap(bitmap, new Size((int)(bitmap.Width / (bitmap.HorizontalResolution / targetResolution)), (int)(bitmap.Height / (bitmap.VerticalResolution / targetResolution)))))
{
smallBitmap.SetResolution(targetResolution, targetResolution);
ImageCodecInfo encoder = GetEncoder(targetFormat);
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, Image.GetPixelFormatSize(bitmap.PixelFormat));
smallBitmap.Save(inNewFileTargetPath, encoder, encoderParams);
}
}
}
catch
{
return false;
}
return true;
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
return codec;
}
return null;
}
#endregion
#region Folder conversion.
public bool ConvertPDFFolder2JPG(string inConvertFolderPath,
string inTargetFolderPath,
string inConvertFileWildCard,
bool inDeleteSourcePDF,
bool inSearchSubFolders,
double inResolutionX,
double inResolutionY,
double inGraphicsAlphaBitsValue,
double inTextAlphaBitsValue,
double inQuality)
{
// logger.Info("inConvertFolderPath = " + inConvertFolderPath + ", inTargetFolderPath = " + inTargetFolderPath +
// ", inConvertFileWildCard = " + inConvertFileWildCard + ", inDeleteSourcePDF = " + inDeleteSourcePDF + ", inSearchSubFolders = " + inSearchSubFolders +
// ", inResolutionX = " + inResolutionX + ", inResolutionY = " + inResolutionY + ", inGraphicsAlphaBitsValue = " + inGraphicsAlphaBitsValue +
// ", inTextAlphaBitsValue = " + inTextAlphaBitsValue + ", inQuality = " + inQuality);
bool conversionSucceed;
CheckJPGParamValidation(inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue, inQuality);
System.IO.DirectoryInfo root = new System.IO.DirectoryInfo(inConvertFolderPath);
// Convert all files in folder.
FileConverter fileConvertor = InstancesManager.GetObject(InstancesManager.ConversionType.PDF2JPG);
conversionSucceed = WalkDirectoryTreePDF2JPG(fileConvertor, root, inTargetFolderPath, inConvertFileWildCard, inDeleteSourcePDF, inSearchSubFolders, inConvertFolderPath.Equals(inTargetFolderPath), inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue, inQuality);
InstancesManager.PutObject(InstancesManager.ConversionType.PDF2JPG, fileConvertor);
return conversionSucceed;
}
public bool ConvertPDFFolder2PNG(string inConvertFolderPath,
string inTargetFolderPath,
string inConvertFileWildCard,
bool inDeleteSourcePDF,
bool inSearchSubFolders,
double inResolutionX,
double inResolutionY,
double inGraphicsAlphaBitsValue,
double inTextAlphaBitsValue)
{
// logger.Info("inConvertFolderPath = " + inConvertFolderPath + ", inTargetFolderPath = " + inTargetFolderPath +
// ", inConvertFileWildCard = " + inConvertFileWildCard + ", inDeleteSourcePDF = " + inDeleteSourcePDF + ", inSearchSubFolders = " + inSearchSubFolders +
// ", inResolutionX = " + inResolutionX + ", inResolutionY = " + inResolutionY + ", inGraphicsAlphaBitsValue = " + inGraphicsAlphaBitsValue +
// ", inTextAlphaBitsValue = " + inTextAlphaBitsValue);
bool conversionSucceed;
CheckBaseParamsValidation(inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue);
System.IO.DirectoryInfo root = new System.IO.DirectoryInfo(inConvertFolderPath);
// Convert all files in folder.
FileConverter fileConvertor = InstancesManager.GetObject(InstancesManager.ConversionType.PDF2PNG);
conversionSucceed = WalkDirectoryTreePDF2PNG(fileConvertor, root, inTargetFolderPath, inConvertFileWildCard, inDeleteSourcePDF, inSearchSubFolders, inConvertFolderPath.Equals(inTargetFolderPath), inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue);
InstancesManager.PutObject(InstancesManager.ConversionType.PDF2PNG, fileConvertor);
return conversionSucceed;
}
public bool ConvertPDFFolder2EPS(string inConvertFolderPath,
string inTargetFolderPath,
string inConvertFileWildCard,
bool inDeleteSourcePDF,
bool inSearchSubFolders,
double inFirstPageToConvert,
double inLastPageToConvert)
{
// logger.Info("inConvertFolderPath = " + inConvertFolderPath + ", inTargetFolderPath = " + inTargetFolderPath +
// ", inConvertFileWildCard = " + inConvertFileWildCard + ", inDeleteSourcePDF = " + inDeleteSourcePDF + ", inSearchSubFolders = " + inSearchSubFolders +
// ", inFirstPageToConvert = " + inFirstPageToConvert + ", inLastPageToConvert = " + inLastPageToConvert);
bool conversionSucceed;
System.IO.DirectoryInfo root = new System.IO.DirectoryInfo(inConvertFolderPath);
// Convert all files in folder.
FileConverter fileConvertor = InstancesManager.GetObject(InstancesManager.ConversionType.PDF2EPS);
conversionSucceed = WalkDirectoryTreePDF2EPS(fileConvertor, root, inTargetFolderPath, inConvertFileWildCard, inDeleteSourcePDF, inSearchSubFolders,
inConvertFolderPath.Equals(inTargetFolderPath), inFirstPageToConvert, inLastPageToConvert);
InstancesManager.PutObject(InstancesManager.ConversionType.PDF2EPS, fileConvertor);
return conversionSucceed;
}
#endregion
#region Help Method
/// <summary>
/// Check base parameters validation.
/// </summary>
/// <param name="inResolutionX"></param>
/// <param name="inResolutionY"></param>
/// <param name="inGraphicsAlphaBitsValue"></param>
/// <param name="inTextAlphaBitsValue"></param>
/// <returns></returns>
private void CheckBaseParamsValidation(double inResolutionX, double inResolutionY, double inGraphicsAlphaBitsValue, double inTextAlphaBitsValue)
{
if (inResolutionX <= 0 || inResolutionY <= 0)
throw new ArgumentException("Resolution cannot be <= 0");
else if (!(inGraphicsAlphaBitsValue == 1 || inGraphicsAlphaBitsValue == 2 || inGraphicsAlphaBitsValue == 4))
throw new ArgumentException("GraphicsAlphaBits values are 1, 2 or 4");
else if (!(inTextAlphaBitsValue == 1 || inTextAlphaBitsValue == 2 || inTextAlphaBitsValue == 4))
throw new ArgumentException("TextAlphaBits values are 1, 2 or 4");
}
/// <summary>
/// Check JPG parameters validation.
/// </summary>
/// <param name="inResolutionX"></param>
/// <param name="inResolutionY"></param>
/// <param name="inGraphicsAlphaBitsValue"></param>
/// <param name="inTextAlphaBitsValue"></param>
/// <param name="inQuality"></param>
private void CheckJPGParamValidation(double inResolutionX, double inResolutionY, double inGraphicsAlphaBitsValue, double inTextAlphaBitsValue, double inQuality)
{
CheckBaseParamsValidation(inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue);
if (inQuality < 0 || inQuality > 100)
throw new ArgumentException("File quality range is 0-100");
}
/// <summary>
/// Walking traverse all folders under inRoot looking for PDF files need to convert to JPG.
/// </summary>
/// <param name="inSearchSubFolders"> If true traverse each sub-folders and convert them, except if one of the sub-folders is the target folder. </param>
/// <param name="inSameTargetFolder"> If false create new sub folder under target folder path with the same name as the root sub-folder. </param>
private bool WalkDirectoryTreePDF2JPG(FileConverter inFileConvertor,
System.IO.DirectoryInfo inRoot,
string inTargetFolderPath,
string inConvertFileWildCard,
bool inDeleteSourcePDF,
bool inSearchSubFolders,
bool inSameTargetFolder,
double inResolutionX,
double inResolutionY,
double inGraphicsAlphaBitsValue,
double inTextAlphaBitsValue,
double inQuality)
{
bool fileConversion;
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo[] subDirs = null;
// First, process all the files directly under this folder
files = inRoot.GetFiles(inConvertFileWildCard);
if (files != null)
{
foreach (System.IO.FileInfo file in files)
{
// Make file conversion.
fileConversion = inFileConvertor.ConvertPDF2JPG(file.FullName, inTargetFolderPath, inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue, inQuality);
if (!fileConversion)
return false;
//Delete old files.
if (inDeleteSourcePDF)
FileDelete(file.FullName);
// Rename JPG names to the correct page counter.
RenameImagesNames(inTargetFolderPath, file.FullName, "jpg");
}
if (inSearchSubFolders)
{
// Now find all the subdirectories under this directory.
subDirs = inRoot.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs)
{
// In case the target folder is sub directory of the converted folder don't check it.
if (inTargetFolderPath.Contains(dirInfo.FullName))
continue;
if (!inSameTargetFolder)
{
//Create a new sub folder under target folder path
string newPath = System.IO.Path.Combine(inTargetFolderPath, dirInfo.Name);
//Create the sub folder
System.IO.Directory.CreateDirectory(newPath);
//Recursive call for each subdirectory.
WalkDirectoryTreePDF2JPG(inFileConvertor, dirInfo, newPath, inConvertFileWildCard, inDeleteSourcePDF, inSearchSubFolders, inSameTargetFolder, inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue, inQuality);
}
else
{
// Recursive call for each subdirectory.
WalkDirectoryTreePDF2JPG(inFileConvertor, dirInfo, dirInfo.FullName, inConvertFileWildCard, inDeleteSourcePDF, inSearchSubFolders, inSameTargetFolder, inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue, inQuality);
}
}
}
}
return true;
}
/// <summary>
/// Walking traverse all folders under inRoot looking for PDF files need to convert to PNG.
/// </summary>
/// <param name="inSearchSubFolders"> If true traverse each sub-folders and convert them, except if one of the sub-folders is the target folder. </param>
/// <param name="inSameTargetFolder"> If false create new sub folder under target folder path with the same name as the root sub-folder. </param>
private bool WalkDirectoryTreePDF2PNG(FileConverter inFileConvertor,
System.IO.DirectoryInfo inRoot,
string inTargetFolderPath,
string inConvertFileWildCard,
bool inDeleteSourcePDF,
bool inSearchSubFolders,
bool inSameTargetFolder,
double inResolutionX,
double inResolutionY,
double inGraphicsAlphaBitsValue,
double inTextAlphaBitsValue)
{
bool fileConversion;
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo[] subDirs = null;
// First, process all the files directly under this folder
files = inRoot.GetFiles(inConvertFileWildCard);
if (files != null)
{
foreach (System.IO.FileInfo file in files)
{
// Make file conversion.
fileConversion = inFileConvertor.ConvertPDF2PNG(file.FullName, inTargetFolderPath, inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue);
if (!fileConversion)
return false;
//Delete old files.
if (inDeleteSourcePDF)
FileDelete(file.FullName);
// Rename PNG names to the correct page counter.
RenameImagesNames(inTargetFolderPath, file.FullName, "png");
}
if (inSearchSubFolders)
{
// Now find all the subdirectories under this directory.
subDirs = inRoot.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs)
{
// In case the target folder is sub directory of the converted folder don't check it.
if (inTargetFolderPath.Contains(dirInfo.FullName))
continue;
if (!inSameTargetFolder)
{
//Create a new sub folder under target folder path
string newPath = System.IO.Path.Combine(inTargetFolderPath, dirInfo.Name);
//Create the sub folder
System.IO.Directory.CreateDirectory(newPath);
//Recursive call for each subdirectory.
WalkDirectoryTreePDF2PNG(inFileConvertor, dirInfo, newPath, inConvertFileWildCard, inDeleteSourcePDF, inSearchSubFolders, inSameTargetFolder, inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue);
}
else
{
// Recursive call for each subdirectory.
WalkDirectoryTreePDF2PNG(inFileConvertor, dirInfo, dirInfo.FullName, inConvertFileWildCard, inDeleteSourcePDF, inSearchSubFolders, inSameTargetFolder, inResolutionX, inResolutionY, inGraphicsAlphaBitsValue, inTextAlphaBitsValue);
}
}
}
}
return true;
}
/// <summary>
/// Walking traverse all folders under inRoot looking for PDF files need to convert to EPS.
/// </summary>
/// <param name="inFileConvertor"></param>
/// <param name="inRoot"></param>
/// <param name="inTargetFolderPath"></param>
/// <param name="inConvertFileWildCard"></param>
/// <param name="inDeleteSourcePDF"></param>
/// <param name="inSearchSubFolders"> If true traverse each sub-folders and convert them, except if one of the sub-folders is the target folder. </param>
/// <param name="inSameTargetFolder"> If false create new sub folder under target folder path with the same name as the root sub-folder. </param>
/// <param name="inFirstPageToConvert"></param>
/// <param name="inLastPageToConvert"></param>
/// <returns></returns>
private bool WalkDirectoryTreePDF2EPS(FileConverter inFileConvertor, System.IO.DirectoryInfo inRoot, string inTargetFolderPath, string inConvertFileWildCard,
bool inDeleteSourcePDF, bool inSearchSubFolders, bool inSameTargetFolder, double inFirstPageToConvert, double inLastPageToConvert)
{
bool fileConversion;
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo[] subDirs = null;
// First, process all the files directly under this folder
files = inRoot.GetFiles(inConvertFileWildCard);
if (files != null)
{
foreach (System.IO.FileInfo file in files)
{
// Create converted EPS path.
string convertedEPSPath = inTargetFolderPath + "\\" + Path.GetFileNameWithoutExtension(file.FullName) + ".eps";
// Make file conversion.
fileConversion = inFileConvertor.ConvertPDF2EPS(file.FullName, convertedEPSPath, inFirstPageToConvert, inLastPageToConvert);
if (!fileConversion)
return false;
//Delete old files.
if (inDeleteSourcePDF)
FileDelete(file.FullName);
}
if (inSearchSubFolders)
{
// Now find all the subdirectories under this directory.
subDirs = inRoot.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs)
{
// In case the target folder is sub directory of the converted folder don't check it.
if (inTargetFolderPath.Contains(dirInfo.FullName))
continue;
if (!inSameTargetFolder)
{
//Create new sub folder under target folder path
string newPath = System.IO.Path.Combine(inTargetFolderPath, dirInfo.Name);
//Create the sub folder
System.IO.Directory.CreateDirectory(newPath);
//Recursive call for each subdirectory.
WalkDirectoryTreePDF2EPS(inFileConvertor, dirInfo, newPath, inConvertFileWildCard, inDeleteSourcePDF, inSearchSubFolders, inSameTargetFolder, inFirstPageToConvert, inLastPageToConvert);
}
else
{
// Recursive call for each subdirectory.
WalkDirectoryTreePDF2EPS(inFileConvertor, dirInfo, dirInfo.FullName, inConvertFileWildCard, inDeleteSourcePDF, inSearchSubFolders, inSameTargetFolder, inFirstPageToConvert, inLastPageToConvert);
}
}
}
}
return true;
}
/// <summary>
/// Rename images names to the correct name and page counter.
/// </summary>
/// <param name="inFileDir">Target folder path</param>
/// <param name="inFileFullName">File full path name</param>
/// <param name="inFileExtension">File extension</param>
private void RenameImagesNames(string inFileDir, string inFileFullName, string inFileExtension)
{
string[] filesNameWithTheSamePrefix = Directory.GetFiles(inFileDir, Path.GetFileNameWithoutExtension(inFileFullName) + "-*." + inFileExtension);
int filesCounter = 1;
foreach (string fileName in filesNameWithTheSamePrefix)
{
string pageNumberOutputFormat = GeneratePageNumberOutputFormat(filesCounter);
string fileNewName = inFileDir + "\\" + Path.GetFileNameWithoutExtension(inFileFullName) + pageNumberOutputFormat + filesCounter + "." + inFileExtension;
// Rename file.
FileMove(fileName, fileNewName);
filesCounter++;
}
}
/// <summary>
/// Generate page number prefix format.
/// </summary>
/// <param name="inFilesCounter"></param>
/// <returns></returns>
private string GeneratePageNumberOutputFormat(int inFilesCounter)
{
if (inFilesCounter >= 1 && inFilesCounter <= 9)
return "_p00";
else if (inFilesCounter >= 10 && inFilesCounter <= 99)
return "_p0";
else if (inFilesCounter >= 100 && inFilesCounter <= 999)
return "_p";
return null;
}
/// <summary>
/// Tries to move (or rename) file several times in order to avoid unavailable/locked files issues
/// </summary>
private void FileMove(string sourceFileName, string destFileName)
{
// try to move file
int i = 0;
while (true)
{
try
{
if (File.Exists(destFileName))
File.Delete(destFileName);
File.Move(sourceFileName, destFileName);
break;
}
catch (Exception ex)
{
if (i < 3)
Thread.Sleep(++i * 100);
else
throw ex;
}
}
}
/// <summary>
/// Tries to delete the file several times in order to avoid unavailable/locked files issues
/// </summary>
private void FileDelete(string filePath)
{
int i = 0;
while (true)
{
try
{
File.Delete(filePath);
break;
}
catch (Exception ex)
{
if (i < 3)
Thread.Sleep(++i * 100);
else
throw ex;
}
}
}
#endregion
}
}