Advertisement
TermSpar

PDF Splitter

Nov 19th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using iTextSharp.text;
  11. using iTextSharp.text.pdf;
  12. using System.IO;
  13. using System.Diagnostics;
  14.  
  15. namespace SzyApp
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         string pdfFilePath = "";
  25.         string outputPath = "";
  26.  
  27.         private void SplitAndSaveInterval(string pdfFilePath, string outputPath, int startPage, int interval, string pdfFileName)
  28.         {
  29.             using (PdfReader reader = new PdfReader(pdfFilePath))
  30.             {
  31.                 Document document = new Document();
  32.                 PdfCopy copy = new PdfCopy(document, new FileStream(outputPath + "\\" + pdfFileName + ".pdf", FileMode.Create));
  33.                 document.Open();
  34.  
  35.                 for (int pagenumber = startPage; pagenumber < (startPage + interval); pagenumber++)
  36.                 {
  37.                     if (reader.NumberOfPages >= pagenumber)
  38.                     {
  39.                         copy.AddPage(copy.GetImportedPage(reader, pagenumber));
  40.                     }
  41.                     else
  42.                     {
  43.                         break;
  44.                     }
  45.                 }
  46.  
  47.                 document.Close();
  48.             }
  49.         }
  50.  
  51.         private void button1_Click(object sender, EventArgs e)
  52.         {
  53.             if(txtFolder.Text != "" && txtInterval.Text != "" && txtPDF.Text != "")
  54.             {
  55.                 int interval = int.Parse(txtInterval.Text);
  56.                 int pageNameSuffix = 0;
  57.  
  58.                 PdfReader reader = new PdfReader(pdfFilePath);
  59.  
  60.                 FileInfo file = new FileInfo(pdfFilePath);
  61.                 string pdfFileName = file.Name.Substring(0, file.Name.LastIndexOf(".")) + "-";
  62.  
  63.                 for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber += interval)
  64.                 {
  65.                     pageNameSuffix++;
  66.                     string newPdfFileName = string.Format(pdfFileName + "{0}", pageNameSuffix);
  67.                     SplitAndSaveInterval(pdfFilePath, outputPath, pageNumber, interval, newPdfFileName);
  68.                 }
  69.                 MessageBox.Show(pdfFileName + " was successfully split into " + pageNameSuffix +
  70.                     " files, which are located at " + outputPath, "Success!");
  71.             } else
  72.             {
  73.                 MessageBox.Show("You're missing one of the required fields.", "Error");
  74.             }
  75.         }
  76.  
  77.         private void button2_Click(object sender, EventArgs e)
  78.         {
  79.             OpenFileDialog openFileDialog1 = new OpenFileDialog();
  80.             openFileDialog1.Filter = "PDF Files|*.pdf";
  81.             openFileDialog1.Title = "Select a PDF File";
  82.  
  83.             if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  84.             {
  85.                 pdfFilePath = @openFileDialog1.FileName;
  86.                 txtPDF.Text = pdfFilePath;
  87.  
  88.                 PdfReader pdfReader = new PdfReader(pdfFilePath);
  89.                 int numberOfPages = pdfReader.NumberOfPages;
  90.                 lblPages.Text = "This file is " + numberOfPages + " pages long.";
  91.                 txtInterval.Text = "" + numberOfPages / 2;
  92.             }
  93.             else
  94.             {
  95.                 MessageBox.Show("Please select a PDF file.", "Error");
  96.             }
  97.         }
  98.  
  99.         private void button3_Click(object sender, EventArgs e)
  100.         {
  101.             FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
  102.             folderBrowser.Description = "Select a Folder";
  103.  
  104.             if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  105.             {
  106.                 outputPath = @folderBrowser.SelectedPath;
  107.                 txtFolder.Text = outputPath;
  108.             }
  109.             else
  110.             {
  111.                 MessageBox.Show("Please select an output folder.", "Error");
  112.             }
  113.         }
  114.  
  115.         private void lblSource_MouseClick(object sender, MouseEventArgs e)
  116.         {
  117.             Process.Start("https://benbcompsci.wordpress.com/2018/11/19/c-split-a-pdf-file/");
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement