简体   繁体   English

如何使用pdf生成代码导出日食项目(itext)

[英]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文件,

  • Right click on project, 右键点击项目,
  • Click Export, 点击导出,
  • select "Runnable JAR File", 选择“可运行的JAR文件”,
  • Extract required libraries into generated JAR 将所需的库提取到生成的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 几个指针,以帮助您解决问题

  1. Compare the two PDF file size, first one generated from eclipse, second generated from the jar file. 比较两个PDF文件的大小,第一个是通过eclipse生成的,第二个是从jar文件生成的。 Is there a size difference, if there is then it means that the generated jar is missing something that the eclipse project has. 是否存在大小差异,如果存在大小差异,则意味着生成的jar缺少Eclipse项目具有的内容。
  2. Are you running the generated jar by double-clicking it? 您是否通过双击运行生成的jar? If 'yes' then even if there is any error thrown by any program in jar file, it won't appear as the window closes immediately (assuming that this is no Swing/AWT GUI application). 如果为“是”,那么即使jar文件中的任何程序抛出任何错误,窗口也不会立即关闭(假设这不是Swing / AWT GUI应用程序),它将不会出现。 So I suggest to run it from command prompt like: java -jar xyz.jar 因此,我建议从命令提示符运行它,例如: java -jar xyz.jar

Hopefully these two should resolve your issue. 希望这两点可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM