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

jv2fixgn  于 2023-01-16  发布在  Java
关注(0)|答案(1)|浏览(104)

这个问题已经让我窒息了相当长的一段时间,现在正好超过一个月了。我试着开发一个小的Javafx应用程序,我使用ITEXT 5.3版本,一切都很好,我使用icepdf显示生成的PDF在netbean中。完美地工作在netbean中很好。但一旦我尝试运行可执行的jar程序在netbean之外,它不生成PDF。我尝试使用高级安装程序打包它,PDF文件没有生成太多。我尝试了一些解决方案与此pdf not generated by itext library from java application,但它没有帮助。每一个援助将是真正感谢这里是我的源代码的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"));


    }
nimxete2

nimxete21#

我知道这是一个老问题,但对于那些谁斗争与这个问题。执行您的jar文件在终端上与命令java -jar YouJarFile.jar,看看问题是什么。对我来说,问题是我保存的文件在一个相对路径。当我改变位置到一个绝对路径,对我来说,问题就解决了。

相关问题