Skip to content

Commit 7327b51

Browse files
committed
Initial commit
0 parents  commit 7327b51

File tree

47 files changed

+836
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+836
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Object files
5+
*.o
6+
*.ko
7+
*.obj
8+
*.elf
9+
10+
# Linker output
11+
*.ilk
12+
*.map
13+
*.exp
14+
15+
# Precompiled Headers
16+
*.gch
17+
*.pch
18+
19+
# Libraries
20+
*.lib
21+
*.a
22+
*.la
23+
*.lo
24+
25+
# Shared objects (inc. Windows DLLs)
26+
*.dll
27+
*.so
28+
*.so.*
29+
*.dylib
30+
31+
# Executables
32+
*.exe
33+
*.out
34+
*.app
35+
*.i*86
36+
*.x86_64
37+
*.hex
38+
39+
# Debug files
40+
*.dSYM/
41+
*.su
42+
*.idb
43+
*.pdb
44+
45+
# Kernel Module Compile Results
46+
*.mod*
47+
*.cmd
48+
.tmp_versions/
49+
modules.order
50+
Module.symvers
51+
Mkfile.old
52+
dkms.conf

.vscode/launch.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [],
7+
"console" : "integratedTerminal"
8+
}

PDFBookletMaker.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="iTextSharp" Version="5.5.13.3" />
12+
</ItemGroup>
13+
14+
</Project>

PDFBookletMaker.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.002.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PDFBookletMaker", "PDFBookletMaker.csproj", "{A9B32427-D694-4930-8C1F-A4183CB40552}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{A9B32427-D694-4930-8C1F-A4183CB40552}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{A9B32427-D694-4930-8C1F-A4183CB40552}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{A9B32427-D694-4930-8C1F-A4183CB40552}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{A9B32427-D694-4930-8C1F-A4183CB40552}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {7E273685-F569-41C3-A317-ADEC37B07099}
24+
EndGlobalSection
25+
EndGlobal

Program.cs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using System;
2+
using iTextSharp.text;
3+
using iTextSharp.text.pdf;
4+
using Org.BouncyCastle.Asn1.Ocsp;
5+
6+
namespace PDFBookletMaker
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
if(args != null && args.Length == 6) {
13+
if(!(args.Contains("-source") || args.Contains("-s")) || !(args.Contains("-dest") || args.Contains("-d")) || !(args.Contains("-bsize") || args.Contains("-b"))) {
14+
printHelp();
15+
return;
16+
}
17+
string source = "", dest = "";
18+
int bookletSize = 0;
19+
for(int i = 0; i < 5; i+=2) {
20+
if(args[i].Equals("-source") || args[i].Equals("-s")) {
21+
source = args[i+1];
22+
}
23+
if(args[i].Equals("-dest") || args[i].Equals("-d")) {
24+
dest = args[i+1];
25+
}
26+
if(args[i].Equals("-bsize") || args[i].Equals("-b")) {
27+
try {
28+
bookletSize = Convert.ToInt32(args[i+1]);
29+
if(bookletSize < 4 || bookletSize%4 != 0) {
30+
Console.WriteLine("Booklet Size must be a positive integer and multiple of 4");
31+
return;
32+
}
33+
}
34+
catch {
35+
Console.WriteLine("Booklet Size must be a positive integer and multiple of 4");
36+
return;
37+
}
38+
}
39+
}
40+
try {
41+
PdfReader reader = new PdfReader(source);
42+
int booklets = reader.NumberOfPages / bookletSize;
43+
int pagesNeeded = reader.NumberOfPages % bookletSize;
44+
pagesNeeded = bookletSize - pagesNeeded;
45+
if(pagesNeeded > 0)
46+
booklets++;
47+
int addToFront = 0;
48+
int addToBack = 0;
49+
if(pagesNeeded % 2 == 0) {
50+
addToFront = pagesNeeded/2;
51+
addToBack = pagesNeeded/2;
52+
}
53+
else {
54+
addToFront = (pagesNeeded-1)/2;
55+
addToBack = ((pagesNeeded-1)/2)+1;
56+
}
57+
Console.WriteLine($"This will create {booklets} booklets");
58+
Console.WriteLine($"{pagesNeeded} pages must be added for a booklet size of {bookletSize}.");
59+
char response = 't';
60+
do {
61+
Console.WriteLine($"Would you like to add {addToFront} blank pages to the Front and {addToBack} blank pages to the Back? (Y/N)");
62+
response = Console.ReadKey().KeyChar;
63+
}while(response != 'y' && response != 'n' && response != 'Y' && response != 'N');
64+
if(response == 'n' || response == 'N') {
65+
Console.WriteLine("\nGoodbye!");
66+
return;
67+
}
68+
else {
69+
Console.WriteLine($"\nConverting {source} to {dest} with a booklet size of {bookletSize}");
70+
string tempfile = $"temp-{Guid.NewGuid().ToString()}.pdf";
71+
using(var tempStream = new FileStream(tempfile, FileMode.Create)) {
72+
PdfStamper stamper = new PdfStamper(reader, tempStream);
73+
for(int j = 0; j < addToFront; j++) {
74+
stamper.InsertPage(0, PageSize.A4);
75+
}
76+
for(int j = 0; j < addToFront; j++) {
77+
stamper.InsertPage(reader.NumberOfPages+1, PageSize.A4);
78+
}
79+
stamper.Close();
80+
}
81+
List<int> pageOrder = new List<int>();
82+
int count = 0;
83+
int n = (booklets * bookletSize)-1;
84+
int i = 0;
85+
do {
86+
pageOrder.Add(n+1);
87+
pageOrder.Add(i+1);
88+
n--;
89+
i++;
90+
pageOrder.Add(i+1);
91+
pageOrder.Add(n+1);
92+
n--;
93+
i++;
94+
}while(i < n);
95+
PdfReader reader2 = new PdfReader(tempfile);
96+
using(var destDocStream = new FileStream(dest, FileMode.Create)) {
97+
var pdfConcat = new PdfConcatenate(destDocStream);
98+
reader2.SelectPages(pageOrder);
99+
pdfConcat.AddPages(reader2);
100+
reader2.Close();
101+
pdfConcat.Close();
102+
}
103+
File.Delete(tempfile);
104+
Console.WriteLine($"resulting pdf can be found at {dest}.");
105+
Console.WriteLine("When printing result use 2 page per side option and duplex printing on short edge");
106+
}
107+
reader.Close();
108+
}
109+
catch(Exception ex) {
110+
Console.WriteLine(ex.Message);
111+
return;
112+
}
113+
}
114+
else {
115+
printHelp();
116+
}
117+
}
118+
119+
static private void printHelp() {
120+
Console.WriteLine("Invalid Arguments! Must have all 3 parameters");
121+
Console.WriteLine("-source (-s)\t\t\tsource document location");
122+
Console.WriteLine("-dest (-d)\t\t\tdestination document location");
123+
Console.WriteLine("-bsize (-b)\t\t\tbooklet size (multiple of 4)");
124+
}
125+
}
126+
}

bin/Debug/net6.0/PDFBookletMaker

70.3 KB
Binary file not shown.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"runtimeTarget": {
3+
"name": ".NETCoreApp,Version=v6.0",
4+
"signature": ""
5+
},
6+
"compilationOptions": {},
7+
"targets": {
8+
".NETCoreApp,Version=v6.0": {
9+
"PDFBookletMaker/1.0.0": {
10+
"dependencies": {
11+
"iTextSharp": "5.5.13.3"
12+
},
13+
"runtime": {
14+
"PDFBookletMaker.dll": {}
15+
}
16+
},
17+
"BouncyCastle/1.8.9": {
18+
"runtime": {
19+
"lib/BouncyCastle.Crypto.dll": {
20+
"assemblyVersion": "1.8.9.0",
21+
"fileVersion": "1.8.20343.1"
22+
}
23+
}
24+
},
25+
"iTextSharp/5.5.13.3": {
26+
"dependencies": {
27+
"BouncyCastle": "1.8.9"
28+
},
29+
"runtime": {
30+
"lib/itextsharp.dll": {
31+
"assemblyVersion": "5.5.13.3",
32+
"fileVersion": "0.0.0.0"
33+
}
34+
}
35+
}
36+
}
37+
},
38+
"libraries": {
39+
"PDFBookletMaker/1.0.0": {
40+
"type": "project",
41+
"serviceable": false,
42+
"sha512": ""
43+
},
44+
"BouncyCastle/1.8.9": {
45+
"type": "package",
46+
"serviceable": true,
47+
"sha512": "sha512-axnBgvdD5n+FnEG6efk/tfKuMFru7R/EoISH9zjh319yb3HD24TEHSAbNN/lTRT2ulOGRxDgOsCjkuk08iwWPg==",
48+
"path": "bouncycastle/1.8.9",
49+
"hashPath": "bouncycastle.1.8.9.nupkg.sha512"
50+
},
51+
"iTextSharp/5.5.13.3": {
52+
"type": "package",
53+
"serviceable": true,
54+
"sha512": "sha512-vtnMhTEJdZFCkLqaQLjD8aqTBIVKHPrSFuSXnxbLEJlvE/j/l88fvG9wtsejXTmhtErMo0lA9T2LdfdfbwplYw==",
55+
"path": "itextsharp/5.5.13.3",
56+
"hashPath": "itextsharp.5.5.13.3.nupkg.sha512"
57+
}
58+
}
59+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"runtimeOptions": {
3+
"tfm": "net6.0",
4+
"framework": {
5+
"name": "Microsoft.NETCore.App",
6+
"version": "6.0.0"
7+
}
8+
}
9+
}

bin/Release/net6.0/PDFBookletMaker

70.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)