Monday, April 22, 2019

C# - Pdf split using iTextSharp

In C#, with opensource iTextSharp can be find in nuget package, we can easily split pdf into mulitple files per page. use below code

using iTextSharp.text;
using iTextSharp.text.pdf;

using System.IO;

namespace PdfSplit
{
    class Program
    {

    
    static void Main(string[] args)
       {
            string pdfFilePath = @"C:\Files\sample.pdf";

            string outputPath = @"C:\Files\";

            SplitPages(pdfFilePath, outputPath);
             }

    private static void SplitPages(string pdfFilePath, string outputPath)
        {
           // Intialize a new PdfReader instance with the contents of the source Pdf file:
            PdfReader reader = new PdfReader(pdfFilePath);

            FileInfo file = new FileInfo(pdfFilePath);
            string pdfFileName = file.Name.Substring(0, file.Name.LastIndexOf(".")) + "-";

            Program obj = new Program();

            int pageNameSuffix = 0;
            for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
            {
                pageNameSuffix++;
                string newPdfFileName = string.Format(pdfFileName + "{0}", pageNameSuffix);
                obj.SplitAndSaveInterval(pdfFilePath, outputPath, pageNumber, newPdfFileName);
            }
        }

   private void SplitAndSaveInterval(string pdfFilePath, string outputPath, int pagenumber, string pdfFileName)
        {
            using (PdfReader reader = new PdfReader(pdfFilePath))
            {
                Document document = new Document();
                PdfCopy copy = new PdfCopy(document, new FileStream(outputPath + "\\" + pdfFileName + ".pdf", FileMode.Create));
                document.Open();

                copy.AddPage(copy.GetImportedPage(reader, pagenumber));

                document.Close();
            }

        }
    }
 }