/// <summary> /// MainListに新たなpdfファイルを加える為の関数。 /// </summary> // pdfファイルのフルパスはTagを使って管理 private void AddItemToList(string filename, string password) { // 既に同じパスのファイルが存在するなら何もしない foreach (ListViewItem item in MainList.Items) { FileProperty obj = item.Tag as FileProperty; if (obj.Path == filename) { return; } } // パスワードが入力されていない場合 if (password == null) { try { var reader = new iTextSharp.text.pdf.PdfReader(filename); var ListData = new ListViewItem(); double numberofpages = reader.NumberOfPages; double size = Math.Round(reader.FileLength / Math.Pow(2, 10), 0); var property = new FileProperty(); ListData.Text = Path.GetFileName(filename); ListData.SubItems.Add(String.Format("{0:#,0}", numberofpages)); ListData.SubItems.Add(String.Format("{0:#,0} KB", size)); ListData.SubItems.Add(File.GetLastWriteTime(filename).ToString()); property.Path = filename; property.OwnerPassword = password; property.IgnoreExistence = false; ListData.Tag = property; MainList.Items.Add(ListData); } // 例外を検出したとき、ファイルがパスワード管理されていたら解除を求める。 catch (Exception) { if (Configure.IsProtected(filename)) { LoadSetting(filename); } else { OriginalMessageBox_OK(String.Format(Properties.Resources.UnSupportedFile, Path.GetFileName(filename)), MessageBoxIcon.Error); } } } // パスワードが入力された場合 else { try { var configure = new Configure(); var detached = configure.DetachPassword(filename, password); var reader = new iTextSharp.text.pdf.PdfReader(detached); var ListData = new ListViewItem(); double numberofpages = reader.NumberOfPages; var fileinfo = new FileInfo(filename); double size = Math.Round(fileinfo.Length / Math.Pow(2, 10), 0); var property = new FileProperty(); ListData.Text = Path.GetFileName(filename); ListData.SubItems.Add(String.Format("{0:#,0}", numberofpages)); ListData.SubItems.Add(String.Format("{0:#,0} KB", size)); ListData.SubItems.Add(File.GetLastWriteTime(filename).ToString()); property.Path = filename; property.OwnerPassword = password; property.IgnoreExistence = false; ListData.Tag = property; MainList.Items.Add(ListData); } catch (Exception) { OriginalMessageBox_OK(String.Format(Properties.Resources.UnSupportedFile, Path.GetFileName(filename)), MessageBoxIcon.Error); } // 一時ファイルを消去 finally { var configure = new Configure(); File.Delete(configure.DetachPassword(filename, password)); } } }
// 結合ボタン // mergePath(string[]) : 結合するpdfファイルのフルパス // mergeResult(string) : 結合するpdfファイルの名前(出力用) private void MergeButton_Click(object sender, EventArgs e) { // MainListにアイテムが存在するか? if (MainList.Items.Count == 0) { OriginalMessageBox_OK(Properties.Resources.EmptyList, MessageBoxIcon.Warning); return; } var result = new SaveFileDialog(); result.Filter = Properties.Resources.FileFilter; result.Title = Properties.Resources.SaveDialogTitle; if (result.ShowDialog() != DialogResult.OK) { return; } var merger = new Merger(); var configure = new Configure(); System.Collections.Generic.List<string> mergePath = new List<string>(); System.Collections.Generic.List<string> detachedPath = new List<string>(); bool success = true; foreach (ListViewItem item in MainList.Items) { FileProperty obj = item.Tag as FileProperty; if (obj == null) continue; if (obj.OwnerPassword == null) mergePath.Add(obj.Path); else { try { string detached = configure.DetachPassword(obj.Path, obj.OwnerPassword); mergePath.Add(detached); detachedPath.Add(detached); } catch (FileNotFoundException err) { OriginalMessageBox_OK(err.Message, MessageBoxIcon.Error); success = false; break; } } } if (success) { try { merger.Run(mergePath.ToArray(), result.FileName); var listdelete = OriginalMessageBox_YesNo(String.Format(Properties.Resources.MergeDone, mergePath.Count), MessageBoxIcon.Information); MainList.Items.Clear(); if (listdelete == DialogResult.Yes) AddItemToList(result.FileName, null); } catch (Exception err) { OriginalMessageBox_OK(err.Message, MessageBoxIcon.Error); } finally { foreach (string item in detachedPath) File.Delete(item); } } }
// 分割ボタン // splitPath(string) : 分割するファイルのフルパス // splitFolder(string) : 分割したファイルを保存するフォルダ // splitFileName(string) : 分割したファイルの名前(出力用) private void SplitButton_Click(object sender, EventArgs e) { // MainListにアイテムが存在するか? if (MainList.Items.Count == 0) { OriginalMessageBox_OK(Properties.Resources.EmptyList, MessageBoxIcon.Warning); return; } var dialog = new FolderBrowserDialog(); dialog.Description = Properties.Resources.SaveFolder; var result = dialog.ShowDialog(); if (result != DialogResult.OK) { return; } var splitter = new Splitter(); var configure = new Configure(); var successfile = new List<string>(); var failedfile = new List<string>(); int success = 0; int failed = 0; foreach (ListViewItem item in MainList.Items) { FileProperty obj = item.Tag as FileProperty; if (obj == null) continue; string splitFolder = dialog.SelectedPath; string splitFileName = item.SubItems[0].Text; string splitPath; if (obj.OwnerPassword == null) splitPath = obj.Path; else { try { splitPath = configure.DetachPassword(obj.Path, obj.OwnerPassword); } catch (FileNotFoundException err) { OriginalMessageBox_OK(err.Message, MessageBoxIcon.Error); continue; } } try { var files = splitter.Run(splitPath, splitFolder); if (files.Count > 0) successfile.AddRange(files); success++; } catch (OperationCanceledException) { continue; } // ファイルが既に消滅している場合、リストに追加してスルーする catch (FileNotFoundException) { failedfile.Add(item.Text); failed++; continue; } catch (Exception err) { OriginalMessageBox_OK(err.Message, MessageBoxIcon.Error); continue; } finally { if (obj.OwnerPassword != null) File.Delete(configure.DetachPassword(obj.Path, obj.OwnerPassword)); } } // 存在しないファイル(失敗)が一個でもあった if (failed > 0) { string faileditems = ""; foreach (var item in failedfile) { faileditems = faileditems + "\n" + Path.GetFileName(item); } OriginalMessageBox_OK(String.Format(Properties.Resources.SplitFailedFiles) + faileditems, MessageBoxIcon.Warning); } if (success > 0) { var listdelete = OriginalMessageBox_YesNo(String.Format(Properties.Resources.SplitDone, success), MessageBoxIcon.Information); // 分割に成功したファイルのみをリストから削除する foreach (ListViewItem item in MainList.Items) { bool flag = false; foreach (string fItem in failedfile) { if (fItem == item.Text) { flag = true; break; } } if (!flag) { MainList.Items.Remove(item); } } if (listdelete == DialogResult.Yes) { foreach (string item in successfile.ToArray()) { AddItemToList(item, null); } } } }
// ダブルクリックでファイルを開く private void MainList_DoubleClick(object sender, EventArgs e) { if (MainList.SelectedIndices.Count == 0) return; foreach (ListViewItem item in MainList.SelectedItems) { FileProperty obj = item.Tag as FileProperty; if (obj == null) continue; // ファイルが存在するか if (!File.Exists(obj.Path)) { OriginalMessageBox_OK(String.Format(Properties.Resources.FileNotFound, obj.Path), MessageBoxIcon.Error); continue; } var open = new System.Diagnostics.Process(); var configure = new Configure(); try { open.StartInfo.FileName = obj.Path; open.Start(); } catch (Exception err) { OriginalMessageBox_OK(err.Message, MessageBoxIcon.Error); continue; } } }