-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathProgram.cs
More file actions
103 lines (90 loc) · 3.85 KB
/
Program.cs
File metadata and controls
103 lines (90 loc) · 3.85 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
using System;
using SixLabors.ImageSharp;
using System.IO;
using System.Linq;
using PdfSharp.Pdf;
using PdfSharp.Pdf.Drawing;
using PdfSharpCore.Pdf;
using PdfSharpCore.Pdf.IO;
namespace PDFSharp.Extensions.Sample
{
internal static class Program
{
private static void Main(string[] args)
{
var root = args.FirstOrDefault() ?? Path.Combine("res");
var output = Directory.CreateDirectory("out").Name;
const SearchOption o = SearchOption.AllDirectories;
var files = Directory.GetFiles(root, "*.pdf", o);
foreach (var file in files)
try
{
ExtractImages(output, file);
}
catch (Exception e)
{
Console.Error.WriteLine($"{file} -> {e.Message}");
}
var doc = files.FirstOrDefault(f => f.Contains("sample"));
ExtractDocument(output, doc);
var imgs = Directory.GetFiles(root, "*.png", o);
CombineImages(output, imgs);
}
private static void CombineImages(string output, string[] filenames)
{
var images = filenames.Select(Image.Load).ToArray();
var name = Path.GetFileNameWithoutExtension(filenames.First());
var path = Path.Combine(output, $"{name}.pdf");
using (PdfDocument pdf = images.First().ToPdf())
pdf.Save(path);
name = Path.GetFileNameWithoutExtension(filenames.Last());
path = Path.Combine(output, $"{name}.pdf");
using (PdfDocument pdf = images.ToPdf())
pdf.Save(path);
}
private static void ExtractDocument(string output, string filename)
{
using (var document = PdfReader.Open(filename, PdfDocumentOpenMode.Import))
{
using var image = document.GetImages().Single();
var name = Path.GetFileNameWithoutExtension(filename);
var path = Path.Combine(output, $"{name}.png");
image.SaveAsPng(path);
var page = document.Pages[0];
var elements = page.Elements;
var array = elements.Values.OfType<PdfArray>().Single();
Console.WriteLine($" {nameof(PdfArray)} " +
$"{nameof(PdfArrayExtensions.IsEmpty)} " +
$"= {array.IsEmpty()}");
array.Dump();
var resources = page.Resources.Elements;
var dict = resources.Values.OfType<PdfDictionary>().First();
dict.Dump();
}
}
private static void ExtractImages(string output, string filename)
{
Console.WriteLine("Processing file: {0}", filename);
using (var document = PdfReader.Open(filename, PdfDocumentOpenMode.Import))
{
var pageIndex = 0;
foreach (PdfPage page in document.Pages)
{
var imageIndex = 0;
foreach (var image in page.GetImages())
{
var currPage = pageIndex + 1;
var currImg = imageIndex + 1;
Console.WriteLine("\r\nExtracting image {1} from page {0}", currPage, currImg);
var pre = Path.GetFileNameWithoutExtension(filename);
var path = string.Format(@"{2} {0:00000000}-{1:000}.png", currPage, currImg, pre);
path = Path.Combine(output, path);
image.SaveAsPng(path);
imageIndex++;
}
pageIndex++;
}
}
}
}
}