ImageConverterService
Represents a service class object to convert images to their multi-page representation.
using iTextSharp.text;
using iTextSharp.text.pdf;
using Relativity.DataExchange.Io;
using Relativity.DataExchange.Resources;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
namespace Relativity.DataExchange.Media
{
internal class ImageConverterService : IImageConverter
{
private readonly IFileSystem fileSystem;
public ImageConverterService()
: this(FileSystem.Instance)
{
}
public ImageConverterService(IFileSystem fileSystem)
{
if (fileSystem == null)
throw new ArgumentNullException("fileSystem");
this.fileSystem = fileSystem;
}
public void ConvertTiffsToMultiPageTiff(IEnumerable<string> inputFiles, string outputFile)
{
if (inputFiles == null)
throw new ArgumentNullException("inputFiles");
List<string> list = inputFiles.ToList();
if (list.Count == 0)
throw new ArgumentOutOfRangeException("inputFiles", "The multi-page TIFF conversion requires at least 1 image.");
if (string.IsNullOrEmpty(outputFile))
throw new ArgumentNullException("outputFile");
ImageCodecInfo val = null;
ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo val2 in imageEncoders) {
if (string.Compare(val2.get_MimeType(), "image/tiff", StringComparison.OrdinalIgnoreCase) == 0)
val = val2;
}
if (val == null)
throw new ImageConversionException(Strings.ImageConversionTiffCodecNotFoundMessage);
Encoder val3 = Encoder.SaveFlag;
Encoder val4 = Encoder.Compression;
Image val5 = null;
int j = -1;
EncoderParameters val6 = null;
try {
outputFile = fileSystem.Path.GetFullPath(outputFile);
val6 = new EncoderParameters(2);
val6.get_Param()[0] = new EncoderParameter(val3, 18);
for (j = 0; j < list.Count; j++) {
string path = list[j];
if (j == 0) {
val5 = GetImage(path);
val6.get_Param()[1] = (((int)val5.get_PixelFormat() == 196865) ? new EncoderParameter(val4, 4) : new EncoderParameter(val4, 2));
val5.Save(outputFile, val, val6);
} else {
val6.Dispose();
val6 = null;
val6 = new EncoderParameters(2);
val6.get_Param()[0] = new EncoderParameter(val3, 23);
Image image = GetImage(path);
val6.get_Param()[1] = (((int)image.get_PixelFormat() == 196865) ? new EncoderParameter(val4, 4) : new EncoderParameter(val4, 2));
val5.SaveAdd(image, val6);
image.Dispose();
}
if (j == list.Count - 1) {
val6.get_Param()[0] = new EncoderParameter(val3, 20);
val5.SaveAdd(val6);
}
}
if (val5 != null)
val5.Dispose();
} catch (Exception innerException) {
if (val5 != null)
val5.Dispose();
if (j > -1)
throw new MultiPageTiffConversionException(list[j], j, list.Count, innerException);
throw;
} finally {
if (val6 != null)
val6.Dispose();
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "For backwards compatibility.")]
public void ConvertImagesToMultiPagePdf(IEnumerable<string> inputFiles, string outputFile)
{
if (inputFiles == null)
throw new ArgumentNullException("inputFiles");
List<string> list = inputFiles.ToList();
if (list.Count == 0)
throw new ArgumentOutOfRangeException("inputFiles", "The multi-page PDF conversion requires at least 1 image.");
if (string.IsNullOrEmpty(outputFile))
throw new ArgumentNullException("outputFile");
Document val = null;
int i = -1;
try {
outputFile = fileSystem.Path.GetFullPath(outputFile);
val = new Document();
using (FileStream fileStream = new FileStream(outputFile, FileMode.Create)) {
PdfWriter val2 = PdfWriter.GetInstance(val, (Stream)fileStream);
val.Open();
for (i = 0; i < list.Count; i++) {
string path = list[i];
Image image = GetImage(path);
Rectangle val3 = new Rectangle((float)((double)((float)image.get_Width() / image.get_HorizontalResolution()) * 72), (float)((double)((float)image.get_Height() / image.get_VerticalResolution()) * 72));
val.SetPageSize(val3);
val.NewPage();
int frameCount = image.GetFrameCount(FrameDimension.get_Page());
PdfContentByte val4 = val2.get_DirectContent();
if (frameCount > 1) {
for (int j = 0; j < frameCount; j++) {
image.SelectActiveFrame(FrameDimension.get_Page(), j);
Image val5 = Image.GetInstance(image, ImageFormat.get_Bmp());
val5.ScaleToFit(val.get_PageSize().get_Width(), val.get_PageSize().get_Height());
float num = (val.get_PageSize().get_Width() - val5.get_ScaledWidth()) / 2;
float num2 = (val.get_PageSize().get_Height() - val5.get_ScaledHeight()) / 2;
val5.SetAbsolutePosition(num, num2);
val4.AddImage(val5);
}
image.Dispose();
} else {
image.Dispose();
using (FileStream fileStream2 = new FileStream(path, FileMode.Open)) {
Image val6 = Image.GetInstance((Stream)fileStream2);
val6.ScaleToFit(val.get_PageSize().get_Width(), val.get_PageSize().get_Height());
float num3 = (val.get_PageSize().get_Width() - val6.get_ScaledWidth()) / 2;
float num4 = (val.get_PageSize().get_Height() - val6.get_ScaledHeight()) / 2;
val6.SetAbsolutePosition(num3, num4);
val4.AddImage(val6);
}
}
}
val.Close();
}
} catch (Exception innerException) {
if (val != null)
try {
val.Close();
} catch {
}
if (i > -1)
throw new MultiPagePdfConversionException(list[i], i, list.Count, innerException);
throw;
}
}
public int GetTiffImageCount(string file)
{
if (!string.IsNullOrEmpty(file)) {
Image image = GetImage(file);
try {
return image.GetFrameCount(FrameDimension.get_Page());
} finally {
((IDisposable)image)?.Dispose();
}
}
throw new ArgumentNullException("file");
}
public int GetPdfPageCount(string file)
{
if (!string.IsNullOrEmpty(file)) {
file = fileSystem.Path.GetFullPath(file);
PdfReader val = null;
try {
val = new PdfReader(file);
return val.get_NumberOfPages();
} finally {
if (val != null)
val.Close();
}
}
throw new ArgumentNullException("file");
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "For backwards compatibility.")]
private Image GetImage(string path)
{
Image val = null;
try {
path = fileSystem.Path.GetFullPath(path);
val = Image.FromFile(path);
return val;
} catch (Exception) {
if (val != null)
val.Dispose();
return Image.FromFile(path);
}
}
}
}