This repository was archived by the owner on May 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
200 lines (168 loc) · 7.11 KB
/
Program.cs
File metadata and controls
200 lines (168 loc) · 7.11 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace SpeedPdfMerge
{
class Program
{
// sample usage: SpeedPdfMerge.exe -SOURCE "file1.pdf" "file2.pdf" -DEST "newfile.pdf"
static int Main(string[] arrParams)
{
if (arrParams == null || arrParams.Length < 1)
{
Console.WriteLine("STATUS:ERROR - No files. Usage: SpeedPdfMerge.exe -SOURCE file1.pdf file2.pdf -DEST output.pdf");
return 0;
}
List<string> listSourceFiles = new List<string>();
string strDestFile = "";
string strParamKey = "";
foreach (string strParam in arrParams)
{
if (strParam == "-SOURCE")
{
strParamKey = "source";
continue;
}
if (strParam == "-DEST")
{
strParamKey = "dest";
continue;
}
if (strParamKey == "dest")
{
strDestFile = strParam;
continue;
}
if (strParamKey == "source")
{
listSourceFiles.Add(strParam);
continue;
}
}
if (String.IsNullOrEmpty(strDestFile) == true)
{
Console.WriteLine("STATUS:ERROR - Destination file -DEST is required");
return 0;
}
if (listSourceFiles.Count < 1)
{
Console.WriteLine("STATUS:ERROR - Source files -SOURCE is required");
return 0;
}
// You can convert it back to an array if you would like to
string[] arrFiles = listSourceFiles.ToArray();
// statistic
int numFiles = 0;
int numPages = 0;
DateTime dateStart = DateTime.Now;
// ----
Document document = new Document();
MemoryStream output = new MemoryStream();
try
{
try
{
// Initialize pdf writer
PdfWriter writer = PdfWriter.GetInstance(document, output);
//writer.PageEvent = new PdfPageEvents();
// set pdf version
//writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_6);
// Open document to write
document.Open();
PdfContentByte content = writer.DirectContent;
// Iterate through all pdf documents
foreach (string strFilename in arrFiles)
{
if (System.IO.File.Exists(strFilename) == false)
{
if (document != null)
{
document.Close();
}
Console.WriteLine("STATUS:ERROR - File not found: " + strFilename);
return 0;
}
numFiles++;
// Create pdf reader
// File.ReadAllBytes(strFilename)
PdfReader reader = new PdfReader(strFilename);
int numberOfPages = reader.NumberOfPages;
// Iterate through all pages
for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
{
numPages++;
// Determine page size for the current page
Rectangle psize = reader.GetPageSizeWithRotation(currentPageIndex);
document.SetPageSize(psize);
// Create page
document.NewPage();
PdfImportedPage importedPage = writer.GetImportedPage(reader, currentPageIndex);
switch (psize.Rotation)
{
case 0:
content.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
break;
case 90:
content.AddTemplate(importedPage, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(1).Height);
break;
case 180:
content.AddTemplate(importedPage, -1f, 0, 0, -1f, 0, 0);
break;
case 270:
content.AddTemplate(importedPage, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(1).Width, 0);
break;
default:
break;
}
// This block is not working correctly
// Determine page orientation
//int pageOrientation = reader.GetPageRotation(currentPageIndex);
//if ((pageOrientation == 90) || (pageOrientation == 270))
//{
//content.AddTemplate(importedPage, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(currentPageIndex).Height);
//}
//else
//{
//content.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
//}
}
}
}
catch (Exception exception)
{
throw new Exception("There has an unexpected exception occured during the pdf merging process.", exception);
}
}
finally
{
if (document != null)
{
document.Close();
}
}
byte[] arrContent = output.ToArray();
// Write out PDF from memory stream.
using (FileStream fs = File.Create(strDestFile))
{
fs.Write(arrContent, 0, (int)arrContent.Length);
}
// ...and start a viewer.
//Process.Start(strDestFile);
// timer
DateTime dateStop = DateTime.Now;
long elapsedTicks = dateStop.Ticks - dateStart.Ticks;
TimeSpan elapsedSpan = new TimeSpan(elapsedTicks);
// write status to console
Console.WriteLine("STATUS:OK");
Console.WriteLine("OUTPUT:{0}", strDestFile);
Console.WriteLine("FILES:{0}", numFiles);
Console.WriteLine("PAGES:{0}", numPages);
Console.WriteLine("TIME:{0}", elapsedSpan.TotalSeconds);
return 1;
}
}
}