-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathPdfHelper.cs
More file actions
115 lines (99 loc) · 3.86 KB
/
Copy pathPdfHelper.cs
File metadata and controls
115 lines (99 loc) · 3.86 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using ImageMagick;
using PdfSharpCore.Pdf;
namespace PdfSharpCore.Test.Helpers
{
public class PdfHelper
{
private static readonly string _rootPath = PathHelper.GetInstance().RootDir;
/// <summary>
/// Rasterize all pages within a PDF to PNG images
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static RasterizeOutput Rasterize(PdfDocument document)
{
var readerSettings = new MagickReadSettings
{
Density = new Density(300, 300),
BackgroundColor = MagickColors.White
};
var images = new MagickImageCollection();
// Add all pages to the collection
using var ms = new MemoryStream();
document.Save(ms);
try
{
images.Read(ms, readerSettings);
}
catch (MagickDelegateErrorException ex)
{
throw new Exception("Ghostscript is not installed or is an incompatible version, unable to rasterize PDF", ex);
}
// Remove transparency to guarantee a standard white background
foreach (var img in images)
{
img.Alpha(AlphaOption.Deactivate);
img.BackgroundColor = MagickColors.White;
}
return new RasterizeOutput
{
ImageCollection = images,
};
}
public static List<string> WriteImageCollection(MagickImageCollection images, string outDir, string filePrefix)
{
var outPaths = new List<string>();
for (var pageNum = 0; pageNum < images.Count; pageNum++)
{
var outPath = GetOutFilePath(outDir, $"{filePrefix}_{pageNum+1}.png");
images[pageNum].Write(outPath);
outPaths.Add(outPath);
}
return outPaths;
}
public static string WriteImage(IMagickImage image, string outDir, string fileNameWithoutExtension)
{
var outPath = GetOutFilePath(outDir, $"{fileNameWithoutExtension}.png");
image.Write(outPath);
return outPath;
}
// Note: For diff to function properly, it requires the underlying image to be in the proper format
// For instance, actual and expected must both be sourced from .png files
public static DiffOutput Diff(string actualImagePath, string expectedImagePath, string outputPath = null, string filePrefix = null, int fuzzPct = 4)
{
var actual = new MagickImage(actualImagePath);
var expected = new MagickImage(expectedImagePath);
// Allow for subtle differences due to cross-platform rendering of the PDF fonts
actual.ColorFuzz = new Percentage(fuzzPct);
var diffImg = actual.Compare(expected, ErrorMetric.Absolute, Channels.All, out double diffVal);
if (diffVal > 0 && outputPath != null && filePrefix != null)
{
WriteImage(diffImg, outputPath, $"{filePrefix}_diff");
}
return new DiffOutput
{
DiffValue = diffVal,
DiffImage = diffImg
};
}
private static string GetOutFilePath(string outDir, string name)
{
var dir = Path.Combine(_rootPath, outDir);
Directory.CreateDirectory(dir);
return Path.Combine(dir, name);
}
}
public class RasterizeOutput
{
public List<string> OutputPaths;
public MagickImageCollection ImageCollection;
}
public class DiffOutput
{
public IMagickImage DiffImage;
public double DiffValue;
}
}