Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using System.IO;
- using System.Diagnostics;
- namespace SzyApp
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- string pdfFilePath = "";
- string outputPath = "";
- private void SplitAndSaveInterval(string pdfFilePath, string outputPath, int startPage, int interval, 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();
- for (int pagenumber = startPage; pagenumber < (startPage + interval); pagenumber++)
- {
- if (reader.NumberOfPages >= pagenumber)
- {
- copy.AddPage(copy.GetImportedPage(reader, pagenumber));
- }
- else
- {
- break;
- }
- }
- document.Close();
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if(txtFolder.Text != "" && txtInterval.Text != "" && txtPDF.Text != "")
- {
- int interval = int.Parse(txtInterval.Text);
- int pageNameSuffix = 0;
- PdfReader reader = new PdfReader(pdfFilePath);
- FileInfo file = new FileInfo(pdfFilePath);
- string pdfFileName = file.Name.Substring(0, file.Name.LastIndexOf(".")) + "-";
- for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber += interval)
- {
- pageNameSuffix++;
- string newPdfFileName = string.Format(pdfFileName + "{0}", pageNameSuffix);
- SplitAndSaveInterval(pdfFilePath, outputPath, pageNumber, interval, newPdfFileName);
- }
- MessageBox.Show(pdfFileName + " was successfully split into " + pageNameSuffix +
- " files, which are located at " + outputPath, "Success!");
- } else
- {
- MessageBox.Show("You're missing one of the required fields.", "Error");
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- OpenFileDialog openFileDialog1 = new OpenFileDialog();
- openFileDialog1.Filter = "PDF Files|*.pdf";
- openFileDialog1.Title = "Select a PDF File";
- if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- pdfFilePath = @openFileDialog1.FileName;
- txtPDF.Text = pdfFilePath;
- PdfReader pdfReader = new PdfReader(pdfFilePath);
- int numberOfPages = pdfReader.NumberOfPages;
- lblPages.Text = "This file is " + numberOfPages + " pages long.";
- txtInterval.Text = "" + numberOfPages / 2;
- }
- else
- {
- MessageBox.Show("Please select a PDF file.", "Error");
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
- folderBrowser.Description = "Select a Folder";
- if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- outputPath = @folderBrowser.SelectedPath;
- txtFolder.Text = outputPath;
- }
- else
- {
- MessageBox.Show("Please select an output folder.", "Error");
- }
- }
- private void lblSource_MouseClick(object sender, MouseEventArgs e)
- {
- Process.Start("https://benbcompsci.wordpress.com/2018/11/19/c-split-a-pdf-file/");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement