第14回 ドラッグアンドドロップで処理する

これまではソースコード内で直接ファイル名を指定した形で処理していました。
今回はウインドウにドラッグアンドドロップしたPDFを処理するように変更します。

「小説家になろう」の縦書きPDFは最初のページの最初のテキストがその小説のタイトルになっています。
そこから小説のタイトルを抜き出し、ファイル名として使えない文字がある場合はそれを置換し、
小説のタイトルを変換後のファイル名としてPDFを書き出すようにします。

また、今までウインドウに表示していた小説表示機能は削除します。

これにより複数のファイルを一度に変換可能になりました。

■Form1.cs
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace Pdf2Pdf
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();

			//PDFTextReader pr = new PDFTextReader();
			//bool ret = pr.Read(@"c:\N9442CW.pdf");


			//PDFTextWriter pw = new PDFTextWriter();
			//pw.ConvertPDFFile("test.pdf", pr);


			Label label1 = new Label();
			label1.Parent = this;
			label1.Dock = DockStyle.Fill;
			label1.TextAlign = ContentAlignment.MiddleCenter;
			label1.Text = "ここに\"小説家になろう\"縦書きPDFをドラッグアンドドロップしてください";

			label1.AllowDrop = true;

			label1.DragEnter += (sender, e) =>
			{
				if (e.Data.GetDataPresent(DataFormats.FileDrop))
				{
					e.Effect = DragDropEffects.Copy;
				}
			};

			label1.DragDrop += (sender, e) =>
			{
				if (e.Data.GetDataPresent(DataFormats.FileDrop))
				{
					string[] items = (string[])e.Data.GetData(DataFormats.FileDrop);

					foreach (string file in items)
					{
						if (File.Exists(file) == false)
							continue;

						//「小説家になろう」縦書きPDFを読み込む
						PDFTextReader pr = new PDFTextReader();
						bool ret = pr.Read(file);

						string strTitle = "";

						//読み込んだ縦書きPDFに含まれる小説のタイトルを取得
						//(タイトルは最初のページの最初のstrText)
						foreach (PDFPage page in pr.Pages)
						{
							foreach (object obj in page.Items)
							{
								if (obj.GetType() != typeof(PDFText))
									continue;

								strTitle = (obj as PDFText).strText;
								if (strTitle != "")
									break;
							}
							if (strTitle != "")
								break;
						}

						//小説タイトルをファイル名にする
						// →ファイル名に使えない文字を除去
						{
							//ありがちな文字は決め打ちで全角に
							strTitle = strTitle.Replace('*', '*');
							strTitle = strTitle.Replace('\\', '¥');
							strTitle = strTitle.Replace(':', ':');
							strTitle = strTitle.Replace('<', '<');
							strTitle = strTitle.Replace('>', '>');
							strTitle = strTitle.Replace('?', '?');
							strTitle = strTitle.Replace('|', '|');

							char[] pcbInvalid = Path.GetInvalidFileNameChars();

							//使えない文字除去
							foreach (char c in pcbInvalid)
							{
								strTitle = strTitle.Replace(c.ToString(), "");
							}

							//タイトルがなくなったら、元々のPDFのファイル名に"_cnv"を付加
							if (strTitle == "")
								strTitle = Path.GetFileNameWithoutExtension(file + "_cnv");
						}

						string strNewFile = "";

						//書き出したいファイル名がすでに存在していたら、上書きしないように存在しないファイル名を生成
						while (true)
						{
							strNewFile = Path.GetDirectoryName(file) + "\\" + strTitle + ".pdf";
							if (File.Exists(strNewFile) == false)
								break;

							strTitle += "_" + DateTime.Now.Ticks;
						}

						//横書きPDFを書き出す
						PDFTextWriter pw = new PDFTextWriter();
						pw.ConvertPDFFile(strNewFile, pr);
					}
				}
			};


			//PictureBox pictureBox1 = new PictureBox();
			//pictureBox1.Parent = this;
			//pictureBox1.Dock = DockStyle.Fill;


			//int nPage = 0;

			//Bitmap bmp = new Bitmap(Width, Height);
			//DrawPage(pr, nPage, bmp);
			//pictureBox1.Image = bmp;


			//pictureBox1.MouseUp += (sender, e) =>
			//{
			//	if (e.Button == MouseButtons.Left)
			//		nPage++;
			//	else if (e.Button == MouseButtons.Right)
			//		nPage--;
			//	else
			//		return;

			//	bmp = new Bitmap(Width, Height);

			//	while (true)
			//	{
			//		ret = DrawPage(pr, nPage, bmp);
			//		if (ret)
			//			break;

			//		nPage++;
			//		if (nPage >= pr.Pages.Count)
			//			break;
			//	}

			//	pictureBox1.Image.Dispose();
			//	pictureBox1.Image = bmp;
			//};
		}

		////横書きに変換表示
		//bool DrawPage(PDFTextReader pr, int nPage, Bitmap bmp)
		//{
		//	if (nPage < 0 || nPage >= pr.Pages.Count)
		//		return false;

		//	PDFPage page = pr.Pages[nPage];

		//	using (Graphics g = Graphics.FromImage(bmp))
		//	using (Font font = new Font("MS ゴシック", 10))
		//	{
		//		foreach (object obj in page.Items)
		//		{
		//			if (obj.GetType() != typeof(PDFText))
		//				continue;

		//			//(0,0)はページ左下
		//			//A4縦は(595,842)がページ右上
		//			//A4横は(842,595)がページ右上

		//			float x = (obj as PDFText).fX;
		//			float y = (obj as PDFText).fY;

		//			//ページ番号は描画しない
		//			//ページ番号のy座標は56.7?
		//			if ((int)(y) == 56)
		//				continue;


		//			x = 842 - x;
		//			y = 595 - y;
		//			g.DrawString((obj as PDFText).strText, font, System.Drawing.Brushes.Black, y, x);
		//		}
		//	}
		//	return true;
		//}
	}
}

プロジェクトファイルをダウンロード


カテゴリー「PDFを処理する(C#)」 のエントリー