• R/O
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

圧縮フォルダを解凍する。画像が複数含まれるので分割する。フォルダごとにpdfに変換


Commit MetaInfo

修訂3 (tree)
時間2017-09-10 08:24:57
作者bellyoshi

Log Message

Change Summary

差異

--- WindowsApp1/WindowsApp1/AppTempDir.vb (nonexistent)
+++ WindowsApp1/WindowsApp1/AppTempDir.vb (revision 3)
@@ -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
--- WindowsApp1/WindowsApp1/Form1.vb (revision 2)
+++ WindowsApp1/WindowsApp1/Form1.vb (revision 3)
@@ -1,19 +1,64 @@
11 Imports System.IO, com
22 Imports iTextSharp.text.pdf
33 Imports iTextSharp.text
4+Imports WindowsApp1
45
56 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+
621 Private Sub btnExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click
7- lstLog.Items.Add("処理開始")
22+ log.Info("処理開始")
823 Dim allPath = TextBox1.Text '全冊フォルダ
924 '全冊フォルダの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
1552 '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
1762 '2番目以降のファイルを読み込む。
1863 '2つに分割し右側をfilename_count.jpeg,左側をfilename_count+1.jpegとして保存する
1964 'tempフォルダをpdf化する
@@ -20,7 +65,7 @@
2065 'A4サイズを横向きで
2166 Dim pdfDocument = New Document(PageSize.A4.Rotate(), 0, 0, 0, 0)
2267 '出力先のファイル名
23- Dim makePdfFilePath = "d:\test.pdf"
68+ Dim makePdfFilePath = zipFilePath.Replace(".zip", "pdf")
2469 Dim fileStream = New FileStream(makePdfFilePath, FileMode.Create)
2570 Dim writer = PdfWriter.GetInstance(pdfDocument, fileStream)
2671 'PDFドキュメントを開く
@@ -28,6 +73,8 @@
2873 pdfDocument.Add(New Paragraph("test"))
2974 'PDFドキュメントを閉じる
3075 pdfDocument.Close()
31- lstLog.Items.Add("処理終了")
76+
3277 End Sub
78+
79+
3380 End Class
--- WindowsApp1/WindowsApp1/Logger.vb (nonexistent)
+++ WindowsApp1/WindowsApp1/Logger.vb (revision 3)
@@ -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
--- WindowsApp1/WindowsApp1/LoggerTemplate.vb (nonexistent)
+++ WindowsApp1/WindowsApp1/LoggerTemplate.vb (revision 3)
@@ -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