C#开发pdf转换为图片并增加pdf特定文件右键菜单

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
using PdfiumViewer;

using System;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;

namespace PdfToImage
{
class Program
{

static void Main(string[] args)
{

Console.Title = Guid.NewGuid().ToString();
ConsoleHelper.hideConsole();

try
{
using (var document = PdfDocument.Load(args[0]))
{
var pageCount = document.PageCount;

for (int i = 0; i < pageCount; i++)
{
//var dpi = 300;
// 图片质量 150就够了
var dpi = 150;

using (var image = document.Render(i, dpi, dpi, PdfRenderFlags.CorrectFromDpi))
{
var encoder = ImageCodecInfo.GetImageEncoders()
.First(c => c.FormatID == ImageFormat.Jpeg.Guid);
var encParams = new EncoderParameters(1);
encParams.Param[0] = new EncoderParameter(
Encoder.Quality, 100L);

image.Save(Path.GetFileNameWithoutExtension(args[0]) + i + ".png", encoder, encParams);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

}
}
}

image-20240424160412684

screenshots