-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
151 lines (137 loc) · 6.67 KB
/
Program.cs
File metadata and controls
151 lines (137 loc) · 6.67 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
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
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Quality;
using PdfSharp.Snippets.Font;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
namespace PDFMerger
{
class Program
{
Display Display = new ();
public static void Main (string [] args)
{
if (Capabilities.Build.IsCoreBuild)
{
GlobalFontSettings.FontResolver = new FailsafeFontResolver ();
}
StartMergeProcess ();
}
public static void StartMergeProcess ()
{
List<PdfDocument> fileList = new List<PdfDocument> ();
Display.OutputMessage ("Accepted document extensions: .pdf .png .jpg .bmp", OutputType.Warning);
GetDocuments (fileList);
MergeDocuments (fileList, out PdfDocument outputDocument);
bool isRelativeToFirstDocument = Display.InputMessage ("Save in the same path as the first document?", true);
if (isRelativeToFirstDocument)
{
string path = fileList [0].IsImported ?
new FileInfo (fileList [0].FullPath).Directory.FullName :
new FileInfo (fileList [0].Info.Comment).Directory.FullName;
Display.OutputMessage ("path: " + path, OutputType.Success);
SaveOutput (outputDocument, true, path);
} else
SaveOutput (outputDocument, false);
}
public static void GetDocuments (List<PdfDocument> fileList)
{
bool inputingFiles = true;
while (inputingFiles)
{
string newDocumentPath = @"" + Display.InputMessage ("Input file path: ");
newDocumentPath = newDocumentPath.Replace ('\\', '/').Trim ('"');
try
{
if (string.IsNullOrEmpty (newDocumentPath))
{
inputingFiles = false;
} else if (File.Exists (newDocumentPath))
{
PdfDocument newDocument;
switch (Path.GetExtension (newDocumentPath))
{
case ".png" or ".bmp" or ".jpg" or ".jpeg":
Image pathImage = Image.FromFile (newDocumentPath);
XImage newImage;
using (MemoryStream ms = new ())
{
pathImage.Save (ms, ImageFormat.Png);
byte [] imageBytes = ms.ToArray ();
newImage = XImage.FromStream (ms);
}
newDocument = new PdfDocument (new FileInfo (newDocumentPath).Directory.FullName + "/new_fileWithImage.pdf");
PdfPage page1 = newDocument.AddPage ();
page1.Width = XUnitPt.FromPoint (newImage.PointWidth);
page1.Height = XUnit.FromPoint (newImage.PointHeight);
XGraphics gfx = XGraphics.FromPdfPage (page1);
newDocument.Info.Comment = new FileInfo (newDocumentPath).Directory.FullName + "/new_fileWithImage.pdf";
gfx.DrawImage (newImage, 0, 0, newImage.PixelWidth, newImage.PixelHeight);
fileList.Add (newDocument);
newDocument.Close ();
Display.OutputMessage (new FileInfo (newDocumentPath).Name + " - Successfully added to merge list!", OutputType.Success);
break;
case ".pdf":
newDocument = PdfReader.Open (newDocumentPath, PdfDocumentOpenMode.Import);
fileList.Add (newDocument);
Display.OutputMessage (new FileInfo (newDocumentPath).Name + " - Successfully added to merge list!", OutputType.Success);
break;
default:
Display.OutputMessage (("Warning: File extension not supported! - " + Path.GetExtension (newDocumentPath)), OutputType.Warning);
break;
}
} else { throw new FileNotFoundException ($"File {newDocumentPath}, not found "); }
} catch (Exception ex)
{
Display.OutputMessage (ex.Message, OutputType.Error);
}
}
}
public static void MergeDocuments (List<PdfDocument> fileList, out PdfDocument outputDocument)
{
outputDocument = new PdfDocument ();
for (int fileIdx = 0 ;fileIdx < fileList.Count ;fileIdx++)
{
PdfDocument specificFile = fileList [fileIdx];
bool isImported = specificFile.IsImported;
if (isImported)
{
for (int pageIdx = 0 ;pageIdx < specificFile.PageCount ;pageIdx++)
{
outputDocument.AddPage (specificFile.Pages [pageIdx]);
}
} else
{
outputDocument.AddPage (PdfReader.Open (specificFile.Info.Comment, PdfDocumentOpenMode.Import).Pages [0]);
}
}
Display.OutputMessage ($"Successfully merged {fileList.Count} documents!", OutputType.Success);
}
public static void SaveOutput (PdfDocument documentOutput, bool relativeTo, string outputFilePath = "")
{
string outputFileName = @"" + Display.InputMessage ("output file name: ");
outputFileName = string.IsNullOrEmpty (outputFileName) ? "New_File" + DateTime.Now.ToString ("dd_MM_yy ss-mm-HH") : outputFileName;
if (!relativeTo)
{
outputFilePath = Display.InputMessage ("output file path: ");
}
string savePath = outputFilePath + $"/{outputFileName}.pdf";
Display.OutputMessage ("Success! File saved at: " + savePath, OutputType.Success);
documentOutput.Save (savePath);
documentOutput.Close ();
if (Display.InputMessage ("Open the file's folder?", false))
{
Process.Start ("explorer.exe", outputFilePath);
}
if (Display.InputMessage ("Restart process? ", true))
{
StartProcess ();
} else
Environment.Exit (0);
}
}
}