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

360 Views Asked by At

This problem has been choking me for quite some time now precisely more than a month now.i tried developing a small Javafx application and I used ITEXT 5.3 version and everything works just fine where i used icepdf for displaying the generated PDF in netbean.perfectly works fine in netbeans .But once I tried running the executable jar program outside netbean it does not generate the PDF.i tried packaging it using advanced installer and the pdf did not generate too.I tried some solutions with this pdf not generated by itext library from java application but it didn't help. Every assistance would be truly appreciated Here is my source code for the PDF generation:

     @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"));



    }
1

There are 1 best solutions below

1
KADIR Ipek On

Well, i know it's an older question but for those who struggle with this problem. Execute your jar file on the therminal with the command java -jar YouJarFile.jar and look what the problem is. For me the problem was that i saved the file on an relativ path. When i changed the location to an absolute path the problem for me was solved.