-
Notifications
You must be signed in to change notification settings - Fork 10
/
PdfRasterizer.cs
69 lines (54 loc) · 2.2 KB
/
PdfRasterizer.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
using System.Drawing;
using System.IO;
using Ghostscript.NET;
using Ghostscript.NET.Rasterizer;
using System;
using System.Reflection;
namespace ImageProcessor.Plugins.Pdf
{
internal class PdfRasterizer
{
private readonly int _desiredXDpi;
private readonly int _desiredYDpi;
private readonly GhostscriptVersionInfo _lastInstalledVersion;
private readonly GhostscriptRasterizer _rasterizer;
public PdfRasterizer(int xDpi = 96, int yDpi = 96)
{
_desiredXDpi = xDpi;
_desiredYDpi = yDpi;
var dllPath = Path.Combine(GetBinPath(), Environment.Is64BitProcess ? "gsdll64.dll" : "gsdll32.dll");
if (File.Exists(dllPath))
{
// use DLL in bin folder
_lastInstalledVersion = new GhostscriptVersionInfo(new System.Version(0, 0, 0), dllPath, string.Empty, GhostscriptLicense.GPL | GhostscriptLicense.AFPL);
}
else
{
// try to use installed DLL
_lastInstalledVersion = GhostscriptVersionInfo.GetLastInstalledVersion(GhostscriptLicense.GPL | GhostscriptLicense.AFPL, GhostscriptLicense.GPL);
}
_rasterizer = new GhostscriptRasterizer();
}
public Image Rasterize(string pdfUri, int pageNumber = 1)
{
_rasterizer.Open(pdfUri, _lastInstalledVersion, true);
var img = _rasterizer.GetPage(_desiredXDpi, _desiredYDpi, pageNumber);
_rasterizer.Close();
return img;
}
public Image Rasterize(Stream pdfStream, int pageNumber = 1)
{
_rasterizer.Open(pdfStream, _lastInstalledVersion, true);
var img = _rasterizer.GetPage(_desiredXDpi, _desiredYDpi, pageNumber);
_rasterizer.Close();
return img;
}
private string GetBinPath()
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
}