Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

zMergePdf: merge PDF locally for free

Friday evening, my wife asked to me an easy way to merge some Pdf Documents into a single pdf file.
I suggested her to use one of the millions of sites which allow you to perform this operation online for free.
But a question came to me spontaneously: pdf documents, in my case, contains sensitive data, signatures and other data that I don’t want to share with other unknow people: what will my data do?

Then I decided to write a simple console Application to be used locally to allow my wife to concatenate (merge) her documents, without having to share her personal data to the entire world.

The behavior of this application is very simple: it receives as input the full path of a file system Folder, gets alphabetically all pdf files stored in this folder and then creates a single file merging all pdf documents, using alphabetic sort to establish the position of the document.

How to install this application:

  • Download from this link http://zsis.it/public/svipullo/zmergepdf.zip the zip file named zmergepdf.zip.
  • Extract all files (3 files) inside a local file system folder
  • Use the application as described below

This application is compiled using Microsoft .Net Framework 4.5.

How to use this application.

You can use two different way to invoke the merge command of the application:

By command line

  • Put all source pdf files inside a single folder, naming that alphabetically: the simple way is to add a numeric prefix to the file name, for example 01, 02, 03… Be carefull: 2 cames after 10!!!
  • open a CMD dos command
  • navigate to the folder containing the zmergepdf.exe file
  • run the following command:

zMergePdf "c:\Users\s.russo\Desktop\pdf"

where c:\Users\s.russo\Desktop\pdf is the folder containing my source pdf documents.



By running console application

If you are not familiar with dos prompt command, you can use this application by double clicking inside the zMergePdf.exefile.
The user interface will require to you the full path of the folder where you stored source pdf documents: pressing then Enter key, the application will merge all the documents found inside this folder.



In each use case, the result will be a single pdf document named merge_2017-06-26 08_57_14Z.pdf and stored inside the same folder that you specified as source folder.

The date is the current system date: you can rename the file once it has been created.

You can download the compiled file version for free from here:
http://zsis.it/public/svipullo/zmergepdf.zip

Here the code if somebody want to create his own application: add the reference to iTextSharp dll using nugget package or adding the dll directly.

using System;
using System.Collections.Generic;
using System.IO;
using its = iTextSharp;

namespace zMergePdf
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string folder = string.Empty;
                if (args.Length == 0)
                {
                    Console.WriteLine("Type the folder full path and press Enter.");
                    Console.WriteLine("For the next times, you can also pass the folder full path as parameter.");
                    Console.WriteLine();
                    folder = Console.ReadLine();
                }
                else
                {
                    folder = args[0];
                }

                var files = System.IO.Directory.GetFiles(folder, "*.pdf");
                var filestreams = new ListStream>();

                foreach (var item in files)
                {
                    var stream = System.IO.File.OpenRead(item);
                    filestreams.Add(stream);

                    Console.WriteLine("Adding: " + item);
                }

                if (files.Length == 0)
                    throw new FileNotFoundException("PDF files not found");

                var res = merge(filestreams);
                string fileName = Path.Combine(folder, "merge_" + DateTime.Now.ToString("u").Replace(":", "_").Replace("/", "_") + ".pdf");

                File.WriteAllBytes(fileName, res.ToArray());
                System.Diagnostics.Process.Start(fileName);

                Console.WriteLine();
                Console.WriteLine("Created file " + fileName);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

        private static MemoryStream merge(ListStream> InFiles)
        {
            MemoryStream res = new MemoryStream();

            using (its.text.Document doc = new its.text.Document())
            using (its.text.pdf.PdfCopy pdf = new its.text.pdf.PdfCopy(doc, res))
            {
                doc.Open();

                its.text.pdf.PdfReader reader = null;
                its.text.pdf.PdfImportedPage page = null;

                //fixed typo
                InFiles.ForEach(file =>
                {
                    reader = new its.text.pdf.PdfReader(file);

                    for (int i = 0; i
                    {
                        page = pdf.GetImportedPage(reader, i + 1);
                        pdf.AddPage(page);
                    }

                    pdf.FreeReader(reader);
                    reader.Close();
                });
            }

            return res;
        }
    }
}

If you like my free application, please visit my band Facebook page, the Folx, have a look at its music and, only if you appreciate it, donate your like!!!

https://www.facebook.com/folxpage



This post first appeared on ZSvipullo, please read the originial post: here

Share the post

zMergePdf: merge PDF locally for free

×

Subscribe to Zsvipullo

Get updates delivered right to your inbox!

Thank you for your subscription

×