[英]How to Export an eclipse project with pdf generation code(itext)
I came across this question on SO. 我在SO上遇到了这个问题。
Java application runs properly in Eclipse, but not as .jar Java应用程序可以在Eclipse中正常运行,但不能以.jar格式运行
I don't have any images in my code.I created a runnable jar file in the following way, 我的代码中没有任何图像。我通过以下方式创建了可运行的jar文件,
When I run the .jar file on my desktop, the PDF file is being created. 当我在桌面上运行.jar文件时,正在创建PDF文件。 But it shows the following error
但它显示以下错误
Adobe Reader could not open 'Result-itext.pdf' because it is either not a supported file type or because the file has been damaged
Adobe Reader无法打开“ Result-itext.pdf”,因为它不是受支持的文件类型,或者因为文件已损坏
My Code: 我的代码:
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();
}
Couple pointers to help with your problem 几个指针,以帮助您解决问题
java -jar xyz.jar
java -jar xyz.jar
Hopefully these two should resolve your issue. 希望这两点可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.