Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions MergePDF.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.27130.2026
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MergePDF", "MergePDF\MergePDF.csproj", "{8BF6B775-F54A-4590-959B-FD6D5AEC1280}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MergePDFTests", "MergePDFTests\MergePDFTests.csproj", "{116308E4-C514-461A-B222-AEC05F1DFEFC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{8BF6B775-F54A-4590-959B-FD6D5AEC1280}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BF6B775-F54A-4590-959B-FD6D5AEC1280}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BF6B775-F54A-4590-959B-FD6D5AEC1280}.Release|Any CPU.Build.0 = Release|Any CPU
{116308E4-C514-461A-B222-AEC05F1DFEFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{116308E4-C514-461A-B222-AEC05F1DFEFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{116308E4-C514-461A-B222-AEC05F1DFEFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{116308E4-C514-461A-B222-AEC05F1DFEFC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 3 additions & 3 deletions MergePDF/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
</configuration>
</configuration>
80 changes: 61 additions & 19 deletions MergePDF/Form1.Designer.cs

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

159 changes: 44 additions & 115 deletions MergePDF/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,21 @@ public partial class Form1 : Form
private List<TextBox> Textboxes { get; set; }
private List<OpenFileDialog> FileDialogs { get; set; }

private List<string> Documents { get; set; }

// Create a document for the merged result.
private PdfDocument outDocument { get; set; }
private PdfMerger _pdfMerger;

private OpenFileDialog openFileDialog { get; set; }
private SaveFileDialog saveFileDialog { get; set; }

private int NumberOfFiles { get; set; }

public Form1()
{
InitializeComponent();
Labels = new List<Label>();
Buttons = new List<Button>();
Textboxes = new List<TextBox>();
FileDialogs = new List<OpenFileDialog>();
Documents = new List<string>();
_pdfMerger = new PdfMerger();
CreateOpenFileDialog();
CreateSaveFileDialog();
}
Expand All @@ -51,33 +50,15 @@ private void richConsole_Enter(object sender, EventArgs e)
ActiveControl = null;
}

private void CopyPages(PdfDocument from, PdfDocument to)
{
for (int i = 0; i < from.PageCount; i++)
{
to.AddPage(from.Pages[i]);
}
}

private void btnMergePdf_Click(object sender, EventArgs e)
{

outDocument = new PdfDocument();

try
{
using (PdfDocument outPdf = new PdfDocument())
{
foreach (var document in Documents)
{
document.Replace(@"\", "/");
PdfDocument importPdf = PdfReader.Open(document, PdfDocumentOpenMode.Import);
CopyPages(importPdf, outPdf);
}
var outputLocation = saveFileDialog.FileName.Replace(@"\", "/");
outPdf.Save(outputLocation);
}
_pdfMerger.Clear();
foreach (var box in Textboxes)
_pdfMerger.AddFile(box.Text);

_pdfMerger.MergeFiles(saveFileDialog.FileName);
richConsole.AppendText(string.Format("Merged successfully!\n"));
}catch(Exception ex)
{
Expand All @@ -97,36 +78,27 @@ private void clearToolStripMenuItem_Click(object sender, EventArgs e)
richConsole.Clear();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == -1)
return;

btnMergePdf.Enabled = true;
ShowMergeRows(Int32.Parse(comboBox1.Items[comboBox1.SelectedIndex].ToString()));
}

private void RemoveMergeRows()
{
foreach(var button in Buttons)
{
Controls.Remove(button);
flpFiles.Controls.Remove(button);
}

foreach(var label in Labels)
{
Controls.Remove(label);
flpFiles.Controls.Remove(label);
}

foreach(var txtBox in Textboxes)
{
Controls.Remove(txtBox);
flpFiles.Controls.Remove(txtBox);
}

Buttons.Clear();
Labels.Clear();
Textboxes.Clear();
Documents.Clear();
_pdfMerger.Clear();
}

private void CreateOpenFileDialog()
Expand Down Expand Up @@ -162,94 +134,51 @@ private TextBox CreateTextbox(int yOffset)
return textBox;
}

private Button CreateSaveButton(int yOffset)
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Button button = new Button();
button.Location = new Point(10, yOffset);
button.Text = string.Format("Save file at");
button.Visible = true;

button.Click += (sender, e) =>
{
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName != "")
{
Textboxes.Last().Text = saveFileDialog.FileName;
richConsole.AppendText(string.Format("Output location set: {0}\n", saveFileDialog.FileName));
}
};

Buttons.Add(button);

return button;
this.Close();
}

private Button CreateOpenButton(int yOffset, int increment)
private void btnClearAll_Click(object sender, EventArgs e)
{
Button button = new Button();
button.Location = new Point(10, yOffset);
button.Text = string.Format("Browse file {0}", increment + 1);
button.Visible = true;
lblFileCount.Text = (NumberOfFiles = 0).ToString();
RemoveMergeRows();
richConsole.Clear();
txtSaveFile.Text = string.Empty;
btnMergePdf.Enabled = false;
}



button.Click += (sender, e) =>
private void btnAddFile_Click(object sender, EventArgs e)
{
var dialog = openFileDialog;
if (dialog.ShowDialog() == DialogResult.OK)
{
var dialog = openFileDialog;
if (dialog.ShowDialog() == DialogResult.OK)
try
{
try
{
string fileName = dialog.FileName;
Documents.Add(fileName);
Textboxes[increment].Text = fileName;
}
catch(System.IO.IOException)
{
MessageBox.Show("Uh-oh, something went wrong. Please try closing all the PDF files you want to import", "Import error", MessageBoxButtons.OK, MessageBoxIcon.Error);
richConsole.AppendText("Something went wrong, please try closing all the PDF files you want to import first.\n");
}

lblFileCount.Text = (++NumberOfFiles).ToString();
var box = CreateTextbox(45 * NumberOfFiles);
box.Text = dialog.FileName;
flpFiles.Controls.Add(box);
}
};

Buttons.Add(button);

return button;
catch (System.IO.IOException)
{
MessageBox.Show("Uh-oh, something went wrong. Please try closing all the PDF files you want to import", "Import error", MessageBoxButtons.OK, MessageBoxIcon.Error);
richConsole.AppendText("Something went wrong, please try closing all the PDF files you want to import first.\n");
}
}
btnMergePdf.Enabled = NumberOfFiles > 1;
}

private void ShowMergeRows(int totalRows)
private void btnSaveFile_Click(object sender, EventArgs e)
{
RemoveMergeRows();
int startY = 100;

for (int i = 0; i < totalRows; i++)
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName != "")
{
var textBox = CreateTextbox(startY);
Controls.Add(textBox);

var button = CreateOpenButton(startY, i);
Controls.Add(button);

startY += 45;
txtSaveFile.Text = saveFileDialog.FileName;
richConsole.AppendText(string.Format("Output location set: {0}\n", saveFileDialog.FileName));
}

var saveTextBox = CreateTextbox(startY);
Controls.Add(saveTextBox);

var saveButton = CreateSaveButton(startY);
Controls.Add(saveButton);
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}

private void btnClearAll_Click(object sender, EventArgs e)
{
RemoveMergeRows();
richConsole.Clear();
comboBox1.SelectedIndex = -1;
btnMergePdf.Enabled = false;
}
}
}
Loading