【发布时间】:2016-12-29 20:57:43
【问题描述】:
我在 SO 上遇到了这个问题。
Java application runs properly in Eclipse, but not as .jar
我的代码中没有任何图像。我通过以下方式创建了一个可运行的 jar 文件,
- 右键项目,
- 点击导出,
- 选择“可运行的 JAR 文件”,
- 将所需的库提取到生成的 JAR 中
当我在桌面上运行 .jar 文件时,正在创建 PDF 文件。 但它显示以下错误
Adobe Reader 无法打开“Result-itext.pdf”,因为它是 不是受支持的文件类型或文件已损坏
我的代码:
try {
PdfWriter w = new PdfWriter("Result-itext.pdf");
PdfDocument d = new PdfDocument(w);
Document doc = new Document(d);
/** Added **/
Image img = new Image(ImageDataFactory.create(logo));
img.setHorizontalAlignment(HorizontalAlignment.CENTER);
doc.add(img);
/** Added **/
doc.add(new Paragraph("Test Name : Hello World").setTextAlignment(TextAlignment.CENTER));
doc.add(new Paragraph("Maximum Marks : 20").setTextAlignment(TextAlignment.CENTER));
doc.add(new Paragraph("RESULTS").setBold().setTextAlignment(TextAlignment.CENTER));
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA_OBLIQUE);
Table t = new Table(3);
t.setWidthPercent(70);
t.setHorizontalAlignment(HorizontalAlignment.CENTER);
t.setFont(font);
Cell cell = new Cell().add("User-ID").setTextAlignment(TextAlignment.CENTER).setFont(font);
t.addCell(cell);
cell = new Cell().add("User-Name").setTextAlignment(TextAlignment.CENTER).setFont(font);
t.addCell(cell);
cell = new Cell().add("Marks").setTextAlignment(TextAlignment.CENTER).setFont(font);
t.addCell(cell);
PdfFont font1 = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
t.setFont(font1);
ArrayList<String> a = new ArrayList<String>();
for(int i=0;i<3;i++){
a.add(String.valueOf(i));a.add("jack");a.add(String.valueOf(i+10));
}
for(int i=0;i<9;i++){
cell = new Cell().add(a.get(i)).setTextAlignment(TextAlignment.CENTER);
t.addCell(cell);
}
doc.add(t);
doc.close();
JOptionPane.showMessageDialog(null, "Created file");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
【问题讨论】:
标签: java eclipse pdf jar itext