如何使用pdf生成代码导出eclipse项目(itext)
问题描述:
我在SO上遇到了这个问题。
Java application runs properly in Eclipse, but not as .jar
我没有在我的code.I创建以下列方式运行的JAR文件中的任何图片,
如何使用pdf生成代码导出eclipse项目(itext)
- 右键单击项目,
- 单击导出,
- 选择“ Runnable的JAR文件”,
- 提取所需的库到生成JAR
当我在桌面上运行.jar文件时,正在创建PDF文件。 但它显示了以下错误
ADOBE READER无法打开“结果,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();
}
答
夫妇的指针,以帮助您的问题
- 比较两个PDF文件大小,第一个从eclipse生成,第二个从jar文件生成。是否有大小差异,如果存在,则意味着生成的jar文件缺少eclipse项目所具有的东西。
- 你是通过双击运行生成的jar吗?如果'是',那么即使jar文件中的任何程序抛出任何错误,它也不会在窗口立即关闭时出现(假设这不是Swing/AWT GUI应用程序)。 所以我建议从命令提示符下运行一样它:
java -jar xyz.jar
希望这两个应该解决您的问题。
我纠正了一些错误。然后我添加了图像显示代码。它现在显示java.io.FileNotFoundException C:\ Users \ Pradeep \ Desktop \ resources \ logo.jpg(系统找不到路径指定)' – Pradeep
尝试检查logo.jpg是否存在。如果存在 –
https://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html。 JavaDoc说,甚至当试图打开一个只读文件来编写时,抛出异常 –