I want split PDF documents into multiple PDF documents but unable rename or save as the individual PDF with content within the PDF, Is it possible to rename the Splitted PDFs based on content available in particular in those pdfs ?
What I have tried:
I have created windows form and tried below code.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace Gios.Pdf.SplitMerge
{
public class ProjectForm : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
private IContainer components;
private System.Windows.Forms.Button buttonUp;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.Button buttonExecute;
private ProgressBar progressBar1;
private Label labelStatus;
private Button buttonRemove;
private Button buttonSplit;
private FolderBrowserDialog folderBrowserDialog1;
private OpenFileDialog openFileDialog1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItemAddFiles;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.LinkLabel linkLabel1;
private Project Project;
public ProjectForm(Project Project)
{
InitializeComponent();
this.Project=Project;
this.Text=this.Project.Name;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ProjectForm));
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.buttonUp = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.buttonRemove = new System.Windows.Forms.Button();
this.buttonExecute = new System.Windows.Forms.Button();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.labelStatus = new System.Windows.Forms.Label();
this.buttonSplit = new System.Windows.Forms.Button();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItemAddFiles = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
this.listView1.AllowDrop = true;
this.listView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.listView1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader3,
this.columnHeader2,
this.columnHeader1});
this.listView1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
this.listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.listView1.LabelEdit = true;
this.listView1.Location = new System.Drawing.Point(8, 19);
this.listView1.MultiSelect = false;
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(599, 357);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.AfterLabelEdit += new System.Windows.Forms.LabelEditEventHandler(this.listView1_AfterLabelEdit);
this.listView1.SelectedIndexChanged += new System.EventHandler(this.ListView1_SelectedIndexChanged);
this.listView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.listView1_DragDrop);
this.listView1.DragOver += new System.Windows.Forms.DragEventHandler(this.listView1_DragOver);
this.listView1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.listView1_KeyDown);
this.columnHeader3.Text = "Name";
this.columnHeader3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.columnHeader3.Width = 195;
this.columnHeader2.Text = "Page Count";
this.columnHeader2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.columnHeader2.Width = 85;
this.columnHeader1.Text = "Path";
this.columnHeader1.Width = 286;
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonUp.BackgroundImage")));
this.buttonUp.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonUp.Location = new System.Drawing.Point(615, 16);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(40, 40);
this.buttonUp.TabIndex = 1;
this.buttonUp.Click += new System.EventHandler(this.buttonUp_Click);
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button1.BackgroundImage")));
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button1.Location = new System.Drawing.Point(615, 64);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(40, 40);
this.button1.TabIndex = 1;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox1.Controls.Add(this.listView1);
this.groupBox1.Controls.Add(this.buttonUp);
this.groupBox1.Controls.Add(this.buttonRemove);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Location = new System.Drawing.Point(8, 32);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(663, 384);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "PDF File List - You Can Use Drag and Drop to Add the Documents";
this.buttonRemove.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRemove.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonRemove.BackgroundImage")));
this.buttonRemove.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonRemove.Font = new System.Drawing.Font("Microsoft Sans Serif", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.buttonRemove.Location = new System.Drawing.Point(615, 139);
this.buttonRemove.Name = "buttonRemove";
this.buttonRemove.Size = new System.Drawing.Size(40, 40);
this.buttonRemove.TabIndex = 1;
this.buttonRemove.Click += new System.EventHandler(this.buttonRemove_Click);
this.buttonExecute.Enabled = false;
this.buttonExecute.Location = new System.Drawing.Point(346, 0);
this.buttonExecute.Name = "buttonExecute";
this.buttonExecute.Size = new System.Drawing.Size(145, 24);
this.buttonExecute.TabIndex = 2;
this.buttonExecute.Text = "Merge Documents";
this.buttonExecute.Visible = false;
this.buttonExecute.Click += new System.EventHandler(this.buttonExecute_Click);
this.saveFileDialog1.DefaultExt = "pdf";
this.saveFileDialog1.Filter = "PDF Documents (*.pdf)|*.pdf";
this.saveFileDialog1.Title = "Choose the PDF Destination File";
this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.progressBar1.Location = new System.Drawing.Point(88, 425);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(576, 21);
this.progressBar1.Step = 1;
this.progressBar1.TabIndex = 3;
this.labelStatus.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.labelStatus.AutoSize = true;
this.labelStatus.Location = new System.Drawing.Point(5, 427);
this.labelStatus.Name = "labelStatus";
this.labelStatus.Size = new System.Drawing.Size(38, 13);
this.labelStatus.TabIndex = 4;
this.labelStatus.Text = "Ready";
this.buttonSplit.Enabled = false;
this.buttonSplit.Location = new System.Drawing.Point(8, 2);
this.buttonSplit.Name = "buttonSplit";
this.buttonSplit.Size = new System.Drawing.Size(172, 24);
this.buttonSplit.TabIndex = 2;
this.buttonSplit.Text = "Split into Single Page Files";
this.buttonSplit.Click += new System.EventHandler(this.buttonSplit_Click);
this.folderBrowserDialog1.Description = "Choose the Location for Splitting the Documents";
this.openFileDialog1.DefaultExt = "pdf";
this.openFileDialog1.Filter = "PDF Documents (*.pdf)|*.pdf";
this.openFileDialog1.Multiselect = true;
this.openFileDialog1.ShowReadOnly = true;
this.openFileDialog1.Title = "Add PDF Files ...";
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2});
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemAddFiles});
this.menuItem1.Text = "File";
this.menuItemAddFiles.Index = 0;
this.menuItemAddFiles.Text = "Add Files";
this.menuItemAddFiles.Click += new System.EventHandler(this.menuItemAddFiles_Click);
this.menuItem2.Index = 1;
this.menuItem2.Text = "About";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
this.linkLabel1.Location = new System.Drawing.Point(528, 0);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(144, 24);
this.linkLabel1.TabIndex = 5;
this.linkLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(679, 449);
this.Controls.Add(this.linkLabel1);
this.Controls.Add(this.labelStatus);
this.Controls.Add(this.buttonSplit);
this.Controls.Add(this.buttonExecute);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.groupBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Menu = this.mainMenu1;
this.Name = "ProjectForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "ProjectForm";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
this.listView1.Focus();
if (this.listView1.SelectedItems.Count > 0)
{
int i = this.listView1.SelectedIndices[0];
if (i < this.listView1.Items.Count - 1)
{
ListViewItem lvi = this.listView1.Items[i + 1];
this.listView1.Items.RemoveAt(i + 1);
this.listView1.Items.Insert(i, lvi);
}
}
}
private void buttonUp_Click(object sender, EventArgs e)
{
this.listView1.Focus();
if (this.listView1.SelectedItems.Count > 0)
{
int i = this.listView1.SelectedIndices[0];
if (i > 0)
{
ListViewItem lvi = this.listView1.Items[i - 1];
this.listView1.Items.RemoveAt(i - 1);
this.listView1.Items.Insert(i, lvi);
}
}
}
private void listView1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetData(DataFormats.FileDrop)!=null)
{
e.Effect=DragDropEffects.Copy;
}
else
{
e.Effect=DragDropEffects.None;
}
}
private void listView1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
this.Add(e.Data.GetData(DataFormats.FileDrop) as string[]);
}
private void Add(string[] files)
{
this.labelStatus.Text="Loading Files ...";
foreach (string file in files)
{
if (file.ToLower().EndsWith(".pdf"))
{
Application.DoEvents();
ProjectPart pp = new ProjectPart();
pp.ProgressEvent += new ProgressDelegate(psm_SmPageProcessedEvent);
pp.Load(file);
Application.DoEvents();
this.Project.Parts.Add(pp);
this.listView1.Items.Add(pp.ListViewItem);
Application.DoEvents();
}
}
this.UpdateButtons();
this.Ready();
}
private void Ready()
{
this.labelStatus.Text = "Ready";
this.progressBar1.Value=0;
}
private void buttonExecute_Click(object sender, System.EventArgs e)
{
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Application.DoEvents();
Stream output=new FileStream(this.saveFileDialog1.FileName,FileMode.Create,FileAccess.Write);
PdfSplitterMerger psm = new PdfSplitterMerger(output);
psm.ProgressEvent+=new ProgressDelegate(psm_SmPageProcessedEvent);
this.progressBar1.Maximum = this.Project.TotalPages;
this.progressBar1.Value = 0;
this.labelStatus.Text = "Processing ...";
Application.DoEvents();
foreach (ListViewItem lvi in this.listView1.Items)
{
this.listView1.Focus();
lvi.Selected = true;
Application.DoEvents();
ProjectPart pp = lvi.Tag as ProjectPart;
Application.DoEvents();
psm.Add(File.OpenRead(pp.path), pp.Pages);
}
Application.DoEvents();
psm.Finish();
this.Ready();
Application.DoEvents();
}
}
void psm_SmPageProcessedEvent(int part, int total)
{
Application.DoEvents();
this.progressBar1.Maximum = total;
this.progressBar1.Value = part;
}
private void listView1_AfterLabelEdit(object sender, LabelEditEventArgs e)
{
if (e.Label!=null)
{
(this.listView1.Items[e.Item].Tag as ProjectPart).pages = e.Label;
}
}
private void UpdateButtons()
{
this.buttonExecute.Enabled = this.listView1.Items.Count > 0;
this.buttonSplit.Enabled = this.listView1.Items.Count > 0;
}
private void buttonRemove_Click(object sender, EventArgs e)
{
this.Remove();
}
private void Remove()
{
if (this.listView1.SelectedIndices.Count > 0)
{
this.listView1.Focus();
int i = this.listView1.SelectedIndices[0];
this.listView1.Items.RemoveAt(i);
if (this.listView1.Items.Count > 0)
{
if (i < this.listView1.Items.Count)
{
this.listView1.Items[i].Selected = true;
}
else
{
this.listView1.Items[this.listView1.Items.Count - 1].Selected = true;
}
}
this.UpdateButtons();
this.listView1.Focus();
}
}
private void buttonSplit_Click(object sender, EventArgs e)
{
if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
string path = this.folderBrowserDialog1.SelectedPath;
Application.DoEvents();
int part=0;
this.progressBar1.Maximum = this.Project.TotalPages;
foreach (ListViewItem lvi in this.listView1.Items)
{
this.listView1.Focus();
lvi.Selected = true;
Application.DoEvents();
ProjectPart pp = lvi.Tag as ProjectPart;
int[] Pages=pp.Pages;
foreach (int page in Pages)
{
this.progressBar1.Value = part;
Application.DoEvents();
string filename = Path.GetFileName(pp.path);
filename = Regex.Replace(filename, @".pdf"
, " - " + (page+1).ToString("00000") + ".pdf", RegexOptions.IgnoreCase);
PdfSplitterMerger psm = new PdfSplitterMerger(File.OpenWrite(path+"\\"+filename));
Application.DoEvents();
psm.Add(File.OpenRead(pp.path),new int[]{page});
Application.DoEvents();
psm.Finish();
Application.DoEvents();
part++;
}
}
Application.DoEvents();
this.Ready();
}
}
[STAThread]
static void Main()
{
Application.Run(new ProjectForm());
}
private void menuItemAddFiles_Click(object sender, System.EventArgs e)
{
if (this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
this.Add(this.openFileDialog1.FileNames);
}
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
string s="Gios PDF Splitter And Merger v1.12";
s+="\nThe First Open Source PDF Splitter and Merger Tool!";
s+="\nCopyright 2005-2006 Paolo Gios";
s+="\nhttp://www.paologios.com";
MessageBox.Show(this,s,"About Gios PDF Splitter And Merger",MessageBoxButtons.OK
,MessageBoxIcon.Information,MessageBoxDefaultButton.Button1);
}
private void listView1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode==Keys.Delete)
{
this.Remove();
}
}
private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(this.linkLabel1.Text);
}
private void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}