圧縮フォルダを解凍する。画像が複数含まれるので分割する。フォルダごとにpdfに変換
@@ -0,0 +1,34 @@ | ||
1 | +Imports System.IO | |
2 | + | |
3 | + | |
4 | +Public Class AppTempDir | |
5 | + Implements IDisposable | |
6 | + | |
7 | + Public ReadOnly Property Path As String | |
8 | + | |
9 | + Private log As Logger | |
10 | + | |
11 | + Sub New(path As String, logger As Logger) | |
12 | + Me.log = logger | |
13 | + Me.Path = path | |
14 | + Dim unzipSrcFilesPath = path | |
15 | + IfExistDirDelete(unzipSrcFilesPath) | |
16 | + Directory.CreateDirectory(unzipSrcFilesPath) | |
17 | + End Sub | |
18 | + | |
19 | + Private Sub IDisposable_Dispose() Implements IDisposable.Dispose | |
20 | + ' IfExistDirDelete(Path) | |
21 | + End Sub | |
22 | + | |
23 | + ''' <summary> | |
24 | + ''' もしディレクトリがあれば削除 | |
25 | + ''' </summary> | |
26 | + ''' <param name="path"></param> | |
27 | + Private Sub IfExistDirDelete(path As String) | |
28 | + 'tempフォルダを削除する。 | |
29 | + If Directory.Exists(path) Then | |
30 | + Directory.Delete(path, True) | |
31 | + log.Info(path + "を削除") | |
32 | + End If | |
33 | + End Sub | |
34 | +End Class |
@@ -1,19 +1,64 @@ | ||
1 | 1 | Imports System.IO, com |
2 | 2 | Imports iTextSharp.text.pdf |
3 | 3 | Imports iTextSharp.text |
4 | +Imports WindowsApp1 | |
4 | 5 | |
5 | 6 | Public Class Form1 |
7 | + | |
8 | + Private Class Form1Logger | |
9 | + Inherits LoggerTemplate | |
10 | + Private form As Form1 | |
11 | + Public Sub New(form As Form1) | |
12 | + Me.form = form | |
13 | + End Sub | |
14 | + Public Overrides Sub output(message As String) | |
15 | + form.lstLog.Items.Add(message) | |
16 | + End Sub | |
17 | + End Class | |
18 | + Private log As Logger = New Form1Logger(Me) | |
19 | + | |
20 | + | |
6 | 21 | Private Sub btnExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click |
7 | - lstLog.Items.Add("処理開始") | |
22 | + log.Info("処理開始") | |
8 | 23 | Dim allPath = TextBox1.Text '全冊フォルダ |
9 | 24 | '全冊フォルダのzipファイルを列挙するし以下を繰り返す。 |
10 | - 'tempフォルダがあれば削除する。 | |
11 | - 'tempフォルダを作成する。 | |
12 | - 'zipファイルを1つ解凍しtempフォルダに入れる | |
13 | - 'filename = zipファイルの名前 | |
14 | - 'filename_count = 000 | |
25 | + Dim files As String() = System.IO.Directory.GetFiles(allPath, "*.zip", System.IO.SearchOption.AllDirectories) | |
26 | + If 0 < files.Count Then | |
27 | + Dim zipFilePath = files(0) 'todo:test用とりあえず一個だけ試す。 | |
28 | + zipToPdf(zipFilePath) | |
29 | + End If | |
30 | + | |
31 | + log.Info("処理終了") | |
32 | + End Sub | |
33 | + | |
34 | + Private Sub zipToPdf(zipFilePath As String) | |
35 | + '新規に解凍後の画像のフォルダを作成 | |
36 | + Using unzipSrcFiles As New AppTempDir(zipFilePath + ".temp1", log) | |
37 | + '新規に分割後の画像のフォルダを作成 | |
38 | + Using divingFilesPath As New AppTempDir(zipFilePath + ".temp2", log) | |
39 | + zipToPdf(zipFilePath, unzipSrcFiles, divingFilesPath) | |
40 | + End Using | |
41 | + End Using | |
42 | + End Sub | |
43 | + | |
44 | + | |
45 | + | |
46 | + Private Sub zipToPdf(zipFilePath As String, srcFiles As AppTempDir, tempFiles As AppTempDir) | |
47 | + | |
48 | + '解凍処理 | |
49 | + System.IO.Compression.ZipFile.ExtractToDirectory(zipFilePath, srcFiles.Path) | |
50 | + | |
51 | + Dim filename_count = 0 | |
15 | 52 | 'tempフォルダのjpegファイルを列挙し以下を繰り返す |
16 | - '先頭ファイルはそのままとする。000.jpeg | |
53 | + Dim files As String() = System.IO.Directory.GetFiles(srcFiles.Path, "*.jpg", System.IO.SearchOption.AllDirectories) | |
54 | + For Each filepath As String In files | |
55 | + Dim filename = System.IO.Path.GetFileName(filepath) | |
56 | + '先頭ファイルはそのままとする。001.jpeg | |
57 | + If filename.Contains("001") Then | |
58 | + Dim destfilepath = Path.Combine(tempFiles.Path, filename) | |
59 | + File.Copy(filepath, destfilepath) | |
60 | + End If | |
61 | + Next | |
17 | 62 | '2番目以降のファイルを読み込む。 |
18 | 63 | '2つに分割し右側をfilename_count.jpeg,左側をfilename_count+1.jpegとして保存する |
19 | 64 | 'tempフォルダをpdf化する |
@@ -20,7 +65,7 @@ | ||
20 | 65 | 'A4サイズを横向きで |
21 | 66 | Dim pdfDocument = New Document(PageSize.A4.Rotate(), 0, 0, 0, 0) |
22 | 67 | '出力先のファイル名 |
23 | - Dim makePdfFilePath = "d:\test.pdf" | |
68 | + Dim makePdfFilePath = zipFilePath.Replace(".zip", "pdf") | |
24 | 69 | Dim fileStream = New FileStream(makePdfFilePath, FileMode.Create) |
25 | 70 | Dim writer = PdfWriter.GetInstance(pdfDocument, fileStream) |
26 | 71 | 'PDFドキュメントを開く |
@@ -28,6 +73,8 @@ | ||
28 | 73 | pdfDocument.Add(New Paragraph("test")) |
29 | 74 | 'PDFドキュメントを閉じる |
30 | 75 | pdfDocument.Close() |
31 | - lstLog.Items.Add("処理終了") | |
76 | + | |
32 | 77 | End Sub |
78 | + | |
79 | + | |
33 | 80 | End Class |
@@ -0,0 +1,4 @@ | ||
1 | +Public Interface Logger | |
2 | + Sub ErrorLog(ByVal message As String) | |
3 | + Sub Info(ByVal message As String) | |
4 | +End Interface |
@@ -0,0 +1,19 @@ | ||
1 | +Imports WindowsApp1 | |
2 | + | |
3 | +Public MustInherit Class LoggerTemplate | |
4 | + Implements Logger | |
5 | + | |
6 | + Public Sub ErrorLog(message As String) Implements Logger.ErrorLog | |
7 | + output("ERROR", message) | |
8 | + End Sub | |
9 | + | |
10 | + Public Sub Info(message As String) Implements Logger.Info | |
11 | + output("INFO", message) | |
12 | + End Sub | |
13 | + | |
14 | + Public Sub output(level As String, message As String) | |
15 | + output($"{level}:{message}") | |
16 | + End Sub | |
17 | + | |
18 | + Public MustOverride Sub output(message As String) | |
19 | +End Class |