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

时间:2019-02-14 15:20:25

标签: java pdf javafx itext icepdf

这个问题已经使我窒息了一段时间,现在已经超过一个月了。我尝试开发一个小型Javafx应用程序,并使用了ITEXT 5.3版本,并且在使用icepdf在netbean中显示生成的PDF时一切正常.perfect在netbeans中正常工作。但是,一旦我尝试在netbean外部运行可执行jar程序,它就不会生成PDF。我尝试使用高级安装程序将其打包,而pdf也没有生成。我尝试了此{{3} },但没有帮助。 每一个协助将不胜感激 这是我生成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"));



    }

0 个答案:

没有答案