質問編集履歴

3

文章を修正しました。

2021/08/30 05:11

投稿

退会済みユーザー
test CHANGED
File without changes
test 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
 

2

ソースコードを追加

2021/08/30 05:11

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -29,3 +29,303 @@
29
29
 
30
30
 
31
31
  ```
32
+
33
+
34
+
35
+
36
+
37
+ ```java
38
+
39
+ package sample;
40
+
41
+
42
+
43
+ import java.io.IOException;
44
+
45
+ import java.io.File;
46
+
47
+ import javax.imageio.ImageIO;
48
+
49
+ import java.awt.image.BufferedImage;
50
+
51
+
52
+
53
+ import java.util.ArrayList;
54
+
55
+ import java.util.Scanner;
56
+
57
+ import java.util.List;
58
+
59
+
60
+
61
+
62
+
63
+ import org.apache.pdfbox.pdmodel.PDDocument;
64
+
65
+ import org.apache.pdfbox.pdmodel.PDPage;
66
+
67
+ import org.apache.pdfbox.pdmodel.PDPageContentStream;
68
+
69
+ import org.apache.pdfbox.pdmodel.common.PDRectangle;
70
+
71
+ import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
72
+
73
+
74
+
75
+
76
+
77
+ public class Main
78
+
79
+ {
80
+
81
+ //画像クラス
82
+
83
+ static class Image
84
+
85
+ {
86
+
87
+ //コンストラクタ
88
+
89
+ public Image(int w,int h,String n)
90
+
91
+ {
92
+
93
+ width = w;
94
+
95
+ height = h;
96
+
97
+ path = n;
98
+
99
+ }
100
+
101
+
102
+
103
+
104
+
105
+ public int width; //横
106
+
107
+ public int height; //縦
108
+
109
+ public String path; //パス
110
+
111
+ }
112
+
113
+
114
+
115
+ //ファイルの画像パスを取得
116
+
117
+ static public void GetDirectory(File dir,List<Image> list)
118
+
119
+ {
120
+
121
+ for(int i = 0; i < dir.listFiles().length; i++)
122
+
123
+ {
124
+
125
+
126
+
127
+ try
128
+
129
+ {
130
+
131
+ BufferedImage b = ImageIO.read(dir.listFiles()[i]);
132
+
133
+ Image img = new Image(b.getWidth(),b.getHeight(),dir.listFiles()[i].getPath());
134
+
135
+
136
+
137
+ list.add(img);
138
+
139
+
140
+
141
+ b = null;
142
+
143
+ }
144
+
145
+ catch (IOException e)
146
+
147
+ {
148
+
149
+ e.printStackTrace();
150
+
151
+ }
152
+
153
+
154
+
155
+
156
+
157
+ System.out.println("###################################");
158
+
159
+ System.out.println("Width: " + list.get(list.size() -1).width);
160
+
161
+ System.out.println("Height: " + list.get(list.size() -1).height);
162
+
163
+ System.out.println("Path: " + list.get(list.size() -1).path);
164
+
165
+ System.out.println("###################################\n");
166
+
167
+ }
168
+
169
+ }
170
+
171
+
172
+
173
+ //PDFファイルを生成
174
+
175
+ static void GeneratePDF(List<Image> list,List<String> pathList,File fileName)
176
+
177
+ {
178
+
179
+ try
180
+
181
+ {
182
+
183
+ PDDocument document = new PDDocument(); //ドキュメント
184
+
185
+ List<PDPage> page = new ArrayList<>(); //ページ
186
+
187
+
188
+
189
+ for(Image image : list)
190
+
191
+ {
192
+
193
+
194
+
195
+
196
+
197
+ PDRectangle rec = new PDRectangle();
198
+
199
+ rec.setUpperRightX(0);
200
+
201
+ rec.setUpperRightY(0);
202
+
203
+ rec.setLowerLeftX(image.width);
204
+
205
+ rec.setLowerLeftY(image.height);
206
+
207
+
208
+
209
+ page.add(new PDPage(rec));
210
+
211
+ document.addPage(page.get(page.size() -1));
212
+
213
+ }
214
+
215
+
216
+
217
+
218
+
219
+
220
+
221
+ for(int i = 0; i < list.size(); i++)
222
+
223
+ {
224
+
225
+
226
+
227
+ PDImageXObject xImage = PDImageXObject.createFromFile(list.get(i).path,document);
228
+
229
+ PDPageContentStream stream = new PDPageContentStream(document,page.get(i));
230
+
231
+ stream.drawImage(xImage, 0,0);
232
+
233
+
234
+
235
+
236
+
237
+ stream.close();
238
+
239
+ }
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+ document.save(fileName.getName() +".pdf");
252
+
253
+
254
+
255
+ document.close();
256
+
257
+
258
+
259
+
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+
268
+
269
+ }catch(IOException e)
270
+
271
+ {
272
+
273
+ e.printStackTrace();
274
+
275
+ }
276
+
277
+ }
278
+
279
+
280
+
281
+
282
+
283
+ public static void main(String args[])
284
+
285
+ {
286
+
287
+ Scanner scanner = new Scanner(System.in);
288
+
289
+ List<Image> imageList = new ArrayList<>();
290
+
291
+
292
+
293
+ System.out.print("Directory: ");
294
+
295
+ String fileName = scanner.next();
296
+
297
+ File f = new File(fileName);
298
+
299
+
300
+
301
+ GetDirectory(f,imageList); //画像パス読み込み
302
+
303
+
304
+
305
+
306
+
307
+ List<String> fileList = new ArrayList<>();
308
+
309
+ GeneratePDF(imageList,fileList,f); //PDFファイルを生成
310
+
311
+
312
+
313
+
314
+
315
+
316
+
317
+
318
+
319
+ scanner.close();
320
+
321
+
322
+
323
+ }
324
+
325
+ }
326
+
327
+
328
+
329
+
330
+
331
+ ```

1

文章を修正

2021/08/30 05:05

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -12,11 +12,15 @@
12
12
 
13
13
  ![イメージ説明](3721464506715b5e8c8dd4b3646c738d.png)
14
14
 
15
+
16
+
17
+ .batファイルです。
18
+
15
19
  ```.bat
16
20
 
17
21
 
18
22
 
19
- java -classpath pdfbox.jar; commons.jar Main
23
+ java -classpath pdfbox.jar; commons.jar; Main
20
24
 
21
25
 
22
26