Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

broke out merge logic and added UnitTests #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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>
35 changes: 5 additions & 30 deletions MergePDF/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ 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; }
Expand All @@ -35,7 +32,7 @@ public Form1()
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 +48,11 @@ 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.MergeFiles(saveFileDialog.FileName);
richConsole.AppendText(string.Format("Merged successfully!\n"));
}catch(Exception ex)
{
Expand Down Expand Up @@ -126,7 +101,7 @@ private void RemoveMergeRows()
Buttons.Clear();
Labels.Clear();
Textboxes.Clear();
Documents.Clear();
_pdfMerger.Clear();
}

private void CreateOpenFileDialog()
Expand Down Expand Up @@ -199,7 +174,7 @@ private Button CreateOpenButton(int yOffset, int increment)
try
{
string fileName = dialog.FileName;
Documents.Add(fileName);
_pdfMerger.AddFile(fileName);
Textboxes[increment].Text = fileName;
}
catch(System.IO.IOException)
Expand Down
5 changes: 4 additions & 1 deletion MergePDF/MergePDF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<OutputType>WinExe</OutputType>
<RootNamespace>MergePDF</RootNamespace>
<AssemblyName>MergePDF</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<IsWebBootstrapper>false</IsWebBootstrapper>
Expand All @@ -29,6 +29,7 @@
<BootstrapperEnabled>true</BootstrapperEnabled>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -81,6 +82,7 @@
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="PdfMerger.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
Expand All @@ -94,6 +96,7 @@
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="packages.config">
<SubType>Designer</SubType>
Expand Down
78 changes: 78 additions & 0 deletions MergePDF/PdfMerger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

namespace MergePDF
{
public class PdfMerger
{
public List<string> Documents { get; set; }
public string OutputFile { get; set; }

public PdfMerger()
{
Init();
}

private void Init()
{
Documents = new List<string>();
OutputFile = string.Empty;
}

public void Clear()
{
Init();
}

/// <summary>
/// Adds a pdf filepath/filename
/// </summary>
/// <param name="filename"></param>
public void AddFile(string filename)
{
Documents.Add(filename);
}

/// <summary>
/// Merges pdf files
/// </summary>
/// <param name="outputFile">optionally set outputfile here</param>
public void MergeFiles(string outputFile = null)
{
if (outputFile != null)
OutputFile = outputFile;
if (Documents?.Count < 2)
throw new ArgumentException("Two or more files are required to merge!");
if (OutputFile == null)
throw new ArgumentNullException("Output file not set!");
if ((OutputFile ?? string.Empty) == string.Empty)
throw new ArgumentException("Output file not set!");

var outDocument = new PdfDocument();

using (PdfDocument outPdf = new PdfDocument())
{
foreach (var document in Documents)
{
PdfDocument importPdf = PdfReader.Open(document.Replace(@"\", "/"), PdfDocumentOpenMode.Import);
CopyPages(importPdf, outPdf);
}
var outputLocation = OutputFile.Replace(@"\", "/");
outPdf.Save(outputLocation);
}
}

private void CopyPages(PdfDocument from, PdfDocument to)
{
for (int i = 0; i < from.PageCount; i++)
{
to.AddPage(from.Pages[i]);
}
}
}
}
44 changes: 18 additions & 26 deletions MergePDF/Properties/Resources.Designer.cs

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

22 changes: 9 additions & 13 deletions MergePDF/Properties/Settings.Designer.cs

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

Loading