Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sliicy committed Apr 30, 2020
1 parent 65e145b commit 4d6cddd
Show file tree
Hide file tree
Showing 14 changed files with 3,167 additions and 0 deletions.
25 changes: 25 additions & 0 deletions PDF Duplexer.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30011.22
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PDF Duplexer", "PDF Duplexer\PDF Duplexer.csproj", "{D565CC0B-48A0-4D43-B0E3-CA87E83A6267}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D565CC0B-48A0-4D43-B0E3-CA87E83A6267}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D565CC0B-48A0-4D43-B0E3-CA87E83A6267}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D565CC0B-48A0-4D43-B0E3-CA87E83A6267}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D565CC0B-48A0-4D43-B0E3-CA87E83A6267}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8DE1403B-7C89-41D4-9567-9BDB30EDB9E3}
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions PDF Duplexer/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="BouncyCastle.Crypto" publicKeyToken="0e99375e54769942" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.8.6.0" newVersion="1.8.6.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
81 changes: 81 additions & 0 deletions PDF Duplexer/Main Form.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

132 changes: 132 additions & 0 deletions PDF Duplexer/Main Form.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iText.Kernel.Pdf;

namespace PDF_Duplexer {
public partial class FrmMain : Form {
public FrmMain() {
InitializeComponent();
}

public void SendToPrinter(string filePath, int pageCount) {
Cursor.Current = Cursors.WaitCursor;
ProcessStartInfo info = new ProcessStartInfo() {
Verb = "print",
FileName = filePath,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
Process p = new Process() {
StartInfo = info
};
p.Start();
p.WaitForInputIdle();

// Wait minimum 3 seconds plus however many pages are being printed:
if (pageCount < 3) {
pageCount = 3;
}
System.Threading.Thread.Sleep(1000 * pageCount);

if (false == p.CloseMainWindow())
p.Kill();
Cursor.Current = Cursors.Default;
}

private void CleanUp(string path1, string path2) {
File.Delete(path1);
File.Delete(path2);
}
private void Print() {
// Protects accidentally deleting an existing file in Downloads:
Random random = new Random();
int randomNumber = random.Next(0, 10000);

string destinationOdd = @"C:\Users\" + Environment.UserName + @"\Downloads\odd temp" + randomNumber.ToString() + ".pdf";
string destinationEven = @"C:\Users\" + Environment.UserName + @"\Downloads\even temp" + randomNumber.ToString() + ".pdf";

var dir = new DirectoryInfo(@"C:\Users\" + Environment.UserName + @"\Downloads");
var pdfFile = (from f in dir.GetFiles()
orderby f.LastWriteTime
select f).Last();
if (pdfFile.Extension.ToLower() != ".pdf") {
MessageBox.Show("Please download a PDF first to print it double sided!");
return;
}
DialogResult dialog1 = MessageBox.Show("Would you like to print " + pdfFile.Name + " double sided?", "Print " + pdfFile.Name + " double sided?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialog1 == DialogResult.Yes) {
PdfDocument doc = new PdfDocument(new PdfReader(pdfFile.FullName));

int numberOfPages = doc.GetNumberOfPages();

List<int> odds = new List<int>();
List<int> evens = new List<int>();
for (var i = 1; i <= numberOfPages; i++) {
if (i % 2 != 0)
odds.Add(i);
}
for (int i = 1; i <= numberOfPages; i++) {
if (i % 2 == 0)
evens.Add(i);
}
evens.Reverse();

PdfDocument oddPDFWriter = new PdfDocument(new PdfWriter(destinationOdd));
PdfDocument evenPDFWriter = new PdfDocument(new PdfWriter(destinationEven));

//If odd number of pages, append blank page to beginning of even pages:
if (numberOfPages % 2 != 0) {
evenPDFWriter.AddNewPage();
}

foreach (var oddPage in odds)
doc.CopyPagesTo(oddPage, oddPage, oddPDFWriter);
foreach (var evenPage in evens)
doc.CopyPagesTo(evenPage, evenPage, evenPDFWriter);

int oddPageCount = oddPDFWriter.GetNumberOfPages();
int evenPageCount = evenPDFWriter.GetNumberOfPages();

oddPDFWriter.Close();
evenPDFWriter.Close();

// Print odd side:
SendToPrinter(destinationOdd, oddPageCount);

DialogResult dialog2 = MessageBox.Show("Please put the papers back into the printer face down. Then press OK to continue.", "Put back into printer", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dialog2 != DialogResult.OK) {
CleanUp(destinationOdd, destinationEven);
return;
}

// Print even side:
SendToPrinter(destinationEven, evenPageCount);

// Clean up:
CleanUp(destinationOdd, destinationEven);
return;
} else
return;
}
private void FrmMain_Load(object sender, EventArgs e) {

}

private void BtnPrint_Click(object sender, EventArgs e) {
Print();
}

private void BtnExit_Click(object sender, EventArgs e) {
Application.Exit();
}
}
}
Loading

0 comments on commit 4d6cddd

Please sign in to comment.