-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageConverter.cs
48 lines (42 loc) · 1.34 KB
/
ImageConverter.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
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Drawing;
using System.IO;
namespace HealthShare.PDFConverter
{
public class ImageConverter : IConverter
{
public bool ConvertToPDF(string inputFile, string outputFile)
{
bool converted = false;
iTextSharp.text.Rectangle pageSize = null;
using (var srcImage = new Bitmap(inputFile))
{
pageSize = new iTextSharp.text.Rectangle(0, 0, srcImage.Width, srcImage.Height);
}
using (var ms = new MemoryStream())
{
Document document = new Document(pageSize, 0, 0, 0, 0);
try
{
PdfWriter.GetInstance(document, ms).SetFullCompression();
document.Open();
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputFile);
document.Add(image);
document.Close();
File.WriteAllBytes(outputFile, ms.ToArray());
converted = true;
}
catch
{
converted = false;
throw;
}
finally
{
}
}
return converted;
}
}
}