teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

文章を修正しました。

2021/08/30 05:11

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- 提示画像ですが画像にあるpdfbox.jar,commons.jarを参照して.classを実行したいのですが上手く出来ません。複数の.jarファイルがある場合どうやって指定すればいいのでしょうか?
1
+ 提示画像ですが画像にあるpdfbox.jar,commons.jarを参照して.classを実行したいのですが上手く出来ません。複数の.jarファイルがある場合どうやって指定すればいいのでしょうか?ecelipse上で完成したいのでそれをコマンドラインで実行できるか検証したいのですが
2
2
 
3
3
 
4
4
  参考サイト: https://nompor.com/2018/01/25/post-2397/

2

ソースコードを追加

2021/08/30 05:11

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -13,4 +13,154 @@
13
13
 
14
14
  pause
15
15
 
16
+ ```
17
+
18
+
19
+ ```java
20
+ package sample;
21
+
22
+ import java.io.IOException;
23
+ import java.io.File;
24
+ import javax.imageio.ImageIO;
25
+ import java.awt.image.BufferedImage;
26
+
27
+ import java.util.ArrayList;
28
+ import java.util.Scanner;
29
+ import java.util.List;
30
+
31
+
32
+ import org.apache.pdfbox.pdmodel.PDDocument;
33
+ import org.apache.pdfbox.pdmodel.PDPage;
34
+ import org.apache.pdfbox.pdmodel.PDPageContentStream;
35
+ import org.apache.pdfbox.pdmodel.common.PDRectangle;
36
+ import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
37
+
38
+
39
+ public class Main
40
+ {
41
+ //画像クラス
42
+ static class Image
43
+ {
44
+ //コンストラクタ
45
+ public Image(int w,int h,String n)
46
+ {
47
+ width = w;
48
+ height = h;
49
+ path = n;
50
+ }
51
+
52
+
53
+ public int width; //横
54
+ public int height; //縦
55
+ public String path; //パス
56
+ }
57
+
58
+ //ファイルの画像パスを取得
59
+ static public void GetDirectory(File dir,List<Image> list)
60
+ {
61
+ for(int i = 0; i < dir.listFiles().length; i++)
62
+ {
63
+
64
+ try
65
+ {
66
+ BufferedImage b = ImageIO.read(dir.listFiles()[i]);
67
+ Image img = new Image(b.getWidth(),b.getHeight(),dir.listFiles()[i].getPath());
68
+
69
+ list.add(img);
70
+
71
+ b = null;
72
+ }
73
+ catch (IOException e)
74
+ {
75
+ e.printStackTrace();
76
+ }
77
+
78
+
79
+ System.out.println("###################################");
80
+ System.out.println("Width: " + list.get(list.size() -1).width);
81
+ System.out.println("Height: " + list.get(list.size() -1).height);
82
+ System.out.println("Path: " + list.get(list.size() -1).path);
83
+ System.out.println("###################################\n");
84
+ }
85
+ }
86
+
87
+ //PDFファイルを生成
88
+ static void GeneratePDF(List<Image> list,List<String> pathList,File fileName)
89
+ {
90
+ try
91
+ {
92
+ PDDocument document = new PDDocument(); //ドキュメント
93
+ List<PDPage> page = new ArrayList<>(); //ページ
94
+
95
+ for(Image image : list)
96
+ {
97
+
98
+
99
+ PDRectangle rec = new PDRectangle();
100
+ rec.setUpperRightX(0);
101
+ rec.setUpperRightY(0);
102
+ rec.setLowerLeftX(image.width);
103
+ rec.setLowerLeftY(image.height);
104
+
105
+ page.add(new PDPage(rec));
106
+ document.addPage(page.get(page.size() -1));
107
+ }
108
+
109
+
110
+
111
+ for(int i = 0; i < list.size(); i++)
112
+ {
113
+
114
+ PDImageXObject xImage = PDImageXObject.createFromFile(list.get(i).path,document);
115
+ PDPageContentStream stream = new PDPageContentStream(document,page.get(i));
116
+ stream.drawImage(xImage, 0,0);
117
+
118
+
119
+ stream.close();
120
+ }
121
+
122
+
123
+
124
+
125
+
126
+ document.save(fileName.getName() +".pdf");
127
+
128
+ document.close();
129
+
130
+
131
+
132
+
133
+
134
+
135
+ }catch(IOException e)
136
+ {
137
+ e.printStackTrace();
138
+ }
139
+ }
140
+
141
+
142
+ public static void main(String args[])
143
+ {
144
+ Scanner scanner = new Scanner(System.in);
145
+ List<Image> imageList = new ArrayList<>();
146
+
147
+ System.out.print("Directory: ");
148
+ String fileName = scanner.next();
149
+ File f = new File(fileName);
150
+
151
+ GetDirectory(f,imageList); //画像パス読み込み
152
+
153
+
154
+ List<String> fileList = new ArrayList<>();
155
+ GeneratePDF(imageList,fileList,f); //PDFファイルを生成
156
+
157
+
158
+
159
+
160
+ scanner.close();
161
+
162
+ }
163
+ }
164
+
165
+
16
166
  ```

1

文章を修正

2021/08/30 05:05

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -5,9 +5,11 @@
5
5
 
6
6
  ![イメージ説明](2dccc340f3b4a3a8a4277feac3d78cda.png)
7
7
  ![イメージ説明](3721464506715b5e8c8dd4b3646c738d.png)
8
+
9
+ .batファイルです。
8
10
  ```.bat
9
11
 
10
- java -classpath pdfbox.jar; commons.jar Main
12
+ java -classpath pdfbox.jar; commons.jar; Main
11
13
 
12
14
  pause
13
15