-
-
Notifications
You must be signed in to change notification settings - Fork 451
Expand file tree
/
Copy pathConvertPDF.cs
More file actions
111 lines (87 loc) · 3.7 KB
/
Copy pathConvertPDF.cs
File metadata and controls
111 lines (87 loc) · 3.7 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
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.
using ImageMagick;
namespace Magick.NET.Samples;
/// <summary>
/// You need to install the latest version of GhostScript before you can convert a pdf using
/// Magick.NET. Make sure you only install the version of GhostScript with the same platform. If
/// you use the 64-bit version of Magick.NET you should also install the 64-bit version of
/// Ghostscript. You can use the 32-bit version together with the 64-version but you will get a
/// better performance if you keep the platforms the same.
/// </summary>
public static class ConvertPDFSamples
{
public static void ConvertPDFToMultipleImages()
{
// Settings the density to 300 dpi will create an image with a better quality
var settings = new MagickReadSettings
{
Density = new Density(300, 300)
};
using var images = new MagickImageCollection();
// Add all the pages of the pdf file to the collection
images.Read(SampleFiles.SnakewarePdf, settings);
var page = 1;
foreach (var image in images)
{
// Write page to file that contains the page number
image.Write(SampleFiles.OutputDirectory + "Snakeware.Page" + page + ".png");
// Writing to a specific format works the same as for a single image
image.Format = MagickFormat.Ptif;
image.Write(SampleFiles.OutputDirectory + "Snakeware.Page" + page + ".tif");
page++;
}
}
public static void ConvertPDFTOneImage()
{
// Settings the density to 300 dpi will create an image with a better quality
var settings = new MagickReadSettings
{
Density = new Density(300)
};
using var images = new MagickImageCollection();
// Add all the pages of the pdf file to the collection
images.Read(SampleFiles.SnakewarePdf, settings);
// Create new image that appends all the pages horizontally
using var horizontal = images.AppendHorizontally();
// Save result as a png
horizontal.Write(SampleFiles.OutputDirectory + "Snakeware.horizontal.png");
// Create new image that appends all the pages vertically
using var vertical = images.AppendVertically();
// Save result as a png
vertical.Write(SampleFiles.OutputDirectory + "Snakeware.vertical.png");
}
public static void CreatePDFFromTwoImages()
{
using var images = new MagickImageCollection();
// Add first page
images.Add(new MagickImage(SampleFiles.SnakewareJpg));
// Add second page
images.Add(new MagickImage(SampleFiles.SnakewareJpg));
// Create pdf file with two pages
images.Write(SampleFiles.OutputDirectory + "Snakeware.pdf");
}
public static void CreatePDFFromSingleImage()
{
// Read image from file
using var image = new MagickImage(SampleFiles.SnakewareJpg);
// Create pdf file with a single page
image.Write(SampleFiles.OutputDirectory + "Snakeware.pdf");
}
public static void ReadSinglePageFromPDF()
{
var settings = new MagickReadSettings
{
FrameIndex = 0, // First page
FrameCount = 1, // Number of pages
};
using var images = new MagickImageCollection();
// Read only the first page of the pdf file
images.Read(SampleFiles.SnakewarePdf, settings);
// Clear the collection
images.Clear();
settings.FrameCount = 2; // Number of pages
// Read the first two pages of the pdf file
images.Read(SampleFiles.SnakewarePdf, settings);
}
}