繁体   English   中英

无法在打包的 Javafx 或 jar 可执行文件中生成 PDF 和 ITEXT

[英]Can't generate PDF with ITEXT in packaged Javafx or jar executable

这个问题已经困扰我很长一段时间了,正好一个多月了。我尝试开发一个小的 Javafx 应用程序,我使用 ITEXT 5.3 版本,一切正常,我使用 icepdf 显示生成的 PDF in.netbean.perfectly在.netbeans 中运行良好。但是一旦我尝试在 outside.netbean 中运行可执行程序 jar,它不会生成 PDF。我尝试使用高级安装程序打包它,而 pdf 也没有生成。我尝试了一些解决方案,这个pdf 不是由来自 java 应用程序的 itext 库,但没有帮助。 非常感谢您的帮助这是我的 PDF 代的源代码:

     @FXML
    private void generatereportforresistance(ActionEvent event) throws IOException {
         XYSeries powerSeries = new XYSeries("Power_Speed relation");

        for (double i=0;i<getValue(13);i=i+0.01){
            double p=getPower(i)*0.51444;


        powerSeries.add(i, p);


        }
        XYDataset xyDataset = new XYSeriesCollection(powerSeries);
        JFreeChart chart = ChartFactory.createXYLineChart(
        "Effective Power V.S Speed", "Speed[knots]", "Power[KW]", xyDataset,
        PlotOrientation.VERTICAL, true, true, true);
     //Render the frame
    ChartFrame chartFrame = new ChartFrame("Power V.S speed Charts", chart);
    chartFrame.setIconImage(Toolkit.getDefaultToolkit().getImage("app_icon.png"));
    //chartFrame.setVisible(true);
    //chartFrame.setSize(800, 800);
    StandardChartTheme st = (StandardChartTheme)org.jfree.chart.StandardChartTheme.createJFreeTheme();
    st.setTitlePaint(Color.decode("#4572a7"));

    st.setRangeGridlinePaint( Color.decode("#C0C0C0"));
    st.setPlotBackgroundPaint( Color.white );
    st.setChartBackgroundPaint( Color.white );
    st.setGridBandPaint( Color.red );
    st.setAxisLabelPaint( Color.decode("#666666")  );
    st.apply( chart );

   double a_aa = r_a(getValue(14),getValue(13),getValue(17),getValue(2),getValue(18),getValue(0),getValue(3),getValue(8),getValue(7),getValue(9),getValue(10));

      double b_bb=r_tr(getValue(13),getValue(14),getValue(11),getValue(2),getValue(10));
      double c_cc=r_b(getValue(13),getValue(14),getValue(7),getValue(3),getValue(10),getValue(2),getValue(8),getValue(16));
      double d_dd=r_w(getValue(13),getValue(14),getValue(7),getValue(5),getValue(11),getValue(3),getValue(18),getValue(10),getValue(6),getValue(17),getValue(0),getValue(9),getValue(2),getValue(8),getValue(16));
      double e_ee=r_app(getValue(14),getValue(13),getValue(0),getValue(15),getValue(12));
      double f_ff=r_f(getValue(14),getValue(13),getValue(0),getValue(15),getValue(2),getValue(10),getValue(17),getValue(7),getValue(18),getValue(9),getValue(6));
      double totoal =f_ff+a_aa+b_bb+c_cc+d_dd+e_ee;
      double p_e =totoal *getValue(13)*0.51444;
      double p_s =p_e/getValue(19);
       //double values of parameters para = new 
       double[] val = new double[9];
       val[0]=a_aa; val[1]=b_bb; val[2]=c_cc; val[3]=d_dd; val[4]=e_ee; val[5]=f_ff; val[6]=totoal; val[7]=p_e; val[8]=p_s;

        //string values of units and parameters namings
        String [] para = new String[9];
        para[0]="Model_Ship Correlation Resistance";para[1]="Additional Pressure Resisitance of immersed Transom";para[2]="Additional pressure resistance of bulbous bow near water surface";
        para[3]="Wave Resistance";para[4]="Resistance due to Appendages";para[5]="Frictional resistance";para[6]="Total resistance";
        para[7]="Effective Power";para[8]="Shaft Power";
        String [] units = new String[9];
        units[0]="KN"; units[1]="KN"; units[1]="KN"; units[2]="KN"; units[3]="KN"; units[4]="KN"; units[5]="KN"; units[6]="KN"; units[7]="KW"; units[8]="KW";

      //generating the report
      PdfWriter wr = null;
       Document doc = new Document();


      try{
          //fos = new FileOutputStream(new File(filename)); 



          PdfPTable table = new PdfPTable(new float[] { 3, 2, 1 });
           table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
         table.addCell("Resistance and Powering parameter");
         table.addCell("Values");
         table.addCell("Units");
         table.setHeaderRows(1);
              PdfPCell[] cells = table.getRow(0).getCells(); 
         for (int j=0;j<cells.length;j++){
             cells[j].setBackgroundColor(BaseColor.GRAY);
         }
          for (int i=0;i<9;i++){
             table.addCell(new PdfPCell(new Paragraph(String.valueOf(para[i]))));
             table.addCell(new PdfPCell(new Paragraph(String.valueOf(val[i]))));
             table.addCell(new PdfPCell(new Paragraph(String.valueOf(units[i]))));
         }
           wr=PdfWriter.getInstance(doc, new FileOutputStream("report.pdf"));
         doc.open();
         Image in = Image.getInstance("app_icon.png");
         in.scaleToFit(100,100);
         //in.setAbsolutePosition(500,500);
         doc.add(in);
         doc.addTitle("Resistance and powering report");
         doc.addCreationDate();

         float w =520;//PageSize.A4.getWidth();
         float h =380;//*PageSize.A4.getHeight();


         //doc.add(new Paragraph("Resistance and powering report",FontFactory.getFont(FontFactory.TIMES_BOLD, 19,Font.BOLD,BaseColor.RED)));
         //doc.add(new Paragraph(new Date().toString()));
         doc.add(new Paragraph("Resistance and powering report",FontFactory.getFont(FontFactory.TIMES_ROMAN,18,Font.BOLD,BaseColor.ORANGE)));
         doc.add(new Paragraph(new Date().toString()));
         doc.add(new Paragraph("**********************************************************************************************"));

          doc.add(table);
          //doc.newPage();
           doc.add(new Paragraph("The effective power graph against the speed is shown below",FontFactory.getFont(FontFactory.TIMES_ITALIC,14,Font.ITALIC)));

          PdfContentByte tem = wr.getDirectContent();
          PdfTemplate tl = tem.createTemplate(w,h);
          Graphics2D gd =new PdfGraphics2D(tl,w,h,new DefaultFontMapper());
          Rectangle2D r = new Rectangle2D.Double(0,0,w,h);
          chart.draw(gd, r);

                  gd.dispose();
          Image im = Image.getInstance(tl);

          doc.add(im);
           /*Image inm = Image.getInstance("app_icon.png");
               in.setAbsolutePosition(490,100);
               wr.getDirectContent().addImage(in);
               doc.add(inm);*/

         doc.close(); }catch(Exception ex){
         }

  try{   

SwingController con= new SwingController();
SwingViewBuilder fac = new SwingViewBuilder(con);
JPanel jp =fac.buildViewerPanel();
con.getDocumentViewController().setAnnotationCallback(

new org.icepdf.ri.common.MyAnnotationCallback(con.getDocumentViewController()));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(jp);
con.openDocument("report.pdf");
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("app_icon.png"));
frame.pack();

frame.setVisible(true);
  }
  catch(Exception ex){
  ex.printStackTrace();
  }

     // Desktop.getDesktop().open(new File("report.pdf"));



    }

好吧,我知道这是一个较老的问题,但对于那些为这个问题而苦苦挣扎的人来说。 使用命令 java -jar YouJarFile.jar 在热敏设备上执行您的 jar 文件,然后查看问题所在。 对我来说,问题是我将文件保存在相对路径上。 当我将位置更改为绝对路径时,我的问题就解决了。

暂无
暂无

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

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