Wednesday, March 6, 2013

C# Code to Split , Merge PDF files

Split the PDF File
---------------------
filepath--- The Path of the Source PDF File need to Split.

dirPath---The Path of the Destination Folder, the splited files need to be saved.



private void SplitPDF(string filepath)
        {
           

            PdfReader reader = null;
            int currentPage = 1;
            int pageCount = 0;
          
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            reader = new iTextSharp.text.pdf.PdfReader(filepath);
            reader.RemoveUnusedObjects();
            pageCount = reader.NumberOfPages;
            string ext = System.IO.Path.GetExtension(filepath);
            for (int i = 1; i <= pageCount; i++)
            {
                iTextSharp.text.pdf.PdfReader reader1 = new iTextSharp.text.pdf.PdfReader(filepath);
                string outfile = filepath.Replace((System.IO.Path.GetFileName(filepath)), (System.IO.Path.GetFileName(filepath).Replace(".pdf", "") + "_" + i.ToString()) + ext);
                outfile = outfile.Substring(0, dirPath.Length).Insert(dirPath.Length, "\\Images\\PDFSplit") + "\\" + Path.GetFileName(filepath).Replace(".pdf", "") + "_" + i.ToString() + ext;
                reader1.RemoveUnusedObjects();
                iTextSharp.text.Document doc = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(currentPage));
                iTextSharp.text.pdf.PdfCopy pdfCpy = new iTextSharp.text.pdf.PdfCopy(doc, new System.IO.FileStream(outfile, System.IO.FileMode.Create));
                doc.Open();
                for (int j = 1; j <= 1; j++)
                {
                    iTextSharp.text.pdf.PdfImportedPage page = pdfCpy.GetImportedPage(reader1, currentPage);
                    pdfCpy.SetFullCompression();
                    pdfCpy.AddPage(page);
                    currentPage += 1;
                }
                doc.Close();
                pdfCpy.Close();
                reader1.Close();
                reader.Close();

            }

        }







Merge the Multiple PDF Files into one File
--------------------------------------------------
SourceFiles--- The Path of the Source PDF Files need to Merge.

DestinationFile---The Path of the Destination Folder, the Merged file need to be saved.



        public void MergePdf(string destinationFile, string[] sourceFiles)
        {

            try
            {
                int f = 0;
                
                PdfReader reader = new PdfReader(sourceFiles[f]);
                int n = reader.NumberOfPages;
                
                Document document = new Document(reader.GetPageSizeWithRotation(1));
               
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
              
                document.Open();
                PdfContentByte cb = writer.DirectContent;
                PdfImportedPage page;
                int rotation;
               
                while (f < sourceFiles.Length)
                {
                    int i = 0;
                    while (i < n)
                    {
                        i++;
                        document.SetPageSize(reader.GetPageSizeWithRotation(i));
                        document.NewPage();
                        page = writer.GetImportedPage(reader, i);
                        rotation = reader.GetPageRotation(i);
                        if (rotation == 90 || rotation == 270)
                        {
                            cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                        }
                        else
                        {
                            cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                        }
                
                    }
                    f++;
                    if (f < sourceFiles.Length)
                    {
                        reader = new PdfReader(sourceFiles[f]);
                       
                        n = reader.NumberOfPages;
                    }
                }
               
                document.Close();
            }
            catch (Exception e)
            {
                string strOb = e.Message;
            }
        }