-
Notifications
You must be signed in to change notification settings - Fork 89
/
PdfToImageExamples.cs
157 lines (109 loc) · 4.83 KB
/
PdfToImageExamples.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
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using Docnet.Core.Converters;
using Docnet.Core.Models;
using Xunit;
namespace NugetUsageAnyCpu
{
[Collection("Example collection")]
public class PdfToImageExamples
{
private const string Path = "Assets/annotations_01.pdf";
private readonly ExampleFixture _fixture;
public PdfToImageExamples(ExampleFixture fixture)
{
_fixture = fixture;
}
[Fact]
public void ConvertPageToSimpleImageWithLetterOutlines_WhenCalled_ShouldSucceed()
{
using var docReader = _fixture.DocNet.GetDocReader(
Path,
new PageDimensions(1080, 1920));
using var pageReader = docReader.GetPageReader(0);
var rawBytes = pageReader.GetImage();
var width = pageReader.GetPageWidth();
var height = pageReader.GetPageHeight();
var characters = pageReader.GetCharacters();
using var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
AddBytes(bmp, rawBytes);
DrawRectangles(bmp, characters);
using var stream = new MemoryStream();
bmp.Save(stream, ImageFormat.Png);
File.WriteAllBytes("../../../output_image.png", stream.ToArray());
}
[Fact]
public void ConvertPageToSimpleImageWithLetterOutlinesUsingScaling_WhenCalled_ShouldSucceed()
{
using var docReader = _fixture.DocNet.GetDocReader(
Path,
new PageDimensions(1080, 1920));
using var pageReader = docReader.GetPageReader(0);
var rawBytes = pageReader.GetImage();
var width = pageReader.GetPageWidth();
var height = pageReader.GetPageHeight();
var characters = pageReader.GetCharacters();
using var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
AddBytes(bmp, rawBytes);
DrawRectangles(bmp, characters);
using var stream = new MemoryStream();
bmp.Save(stream, ImageFormat.Png);
File.WriteAllBytes("../../../output_image.png", stream.ToArray());
}
[Fact]
public void ConvertPageToSimpleImageWithoutTransparency_WhenCalled_ShouldSucceed()
{
using var docReader = _fixture.DocNet.GetDocReader(
Path,
new PageDimensions(1080, 1920));
using var pageReader = docReader.GetPageReader(0);
var rawBytes = pageReader.GetImage(new NaiveTransparencyRemover(120, 120, 0));
var width = pageReader.GetPageWidth();
var height = pageReader.GetPageHeight();
var characters = pageReader.GetCharacters();
using var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
AddBytes(bmp, rawBytes);
DrawRectangles(bmp, characters);
using var stream = new MemoryStream();
bmp.Save(stream, ImageFormat.Png);
File.WriteAllBytes("../../../output_image.png", stream.ToArray());
}
[Fact]
public void ConvertPageToGreyscaleImageIncludeAnnotations_WhenCalled_ShouldSucceed()
{
using var docReader = _fixture.DocNet.GetDocReader(
Path,
new PageDimensions(1080, 1920));
using var pageReader = docReader.GetPageReader(0);
var rawBytes = pageReader.GetImage(RenderFlags.RenderAnnotations | RenderFlags.Grayscale);
var width = pageReader.GetPageWidth();
var height = pageReader.GetPageHeight();
using var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
AddBytes(bmp, rawBytes);
using var stream = new MemoryStream();
bmp.Save(stream, ImageFormat.Png);
File.WriteAllBytes("../../../output_image.png", stream.ToArray());
}
private static void AddBytes(Bitmap bmp, byte[] rawBytes)
{
var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
var bmpData = bmp.LockBits(rect, ImageLockMode.WriteOnly, bmp.PixelFormat);
var pNative = bmpData.Scan0;
Marshal.Copy(rawBytes, 0, pNative, rawBytes.Length);
bmp.UnlockBits(bmpData);
}
private static void DrawRectangles(Bitmap bmp, IEnumerable<Character> characters)
{
var pen = new Pen(Color.Red);
using var graphics = Graphics.FromImage(bmp);
foreach (var c in characters)
{
var rect = new Rectangle(c.Box.Left, c.Box.Top, c.Box.Right - c.Box.Left, c.Box.Bottom - c.Box.Top);
graphics.DrawRectangle(pen, rect);
}
}
}
}