//  RFax
//  Copyright (C)
//
//  java4less@confluencia.net
//  All rights reserved
//
// Adquisition , use and distribution of this code is subject to restriction:
//  - You may modify the source code in order to adapt it to your needs.
//  - Redistribution ofret this ( or a modified version) source code is prohibited. You may only redistribute compiled versions.
//  - You may redistribute the compiled version as part of your application, not a new java component with the same purpose as this one.
//  - You may not remove this notice from the source code
//  - This notice disclaim all warranties of all material
//

package com.java4less.rfax;


import com.adobe.acrobat.PDFDocument;
import com.adobe.acrobat.*;
import com.adobe.acrobat.gui.*;
import com.adobe.acrobat.util.*;
import com.adobe.acrobat.sidecar.*;

import java.awt.Image;
import javax.swing.JPanel;
import java.awt.Graphics;

/**
 * creates a fax using PDF as data source. The class uses the PDF JavaBean from Acrobat, please download it from: http://www.adobe.com/products/acrviewer/main.html
 *<BR>
 *<BR>Example code for faxing:
 *<BR>
 *<BR>try {
 *<BR>        AcrobatFaxProducer pdf=new AcrobatFaxProducer();
 *<BR>        pdf.setPDFFile("c:\\report.pdf");
 *<BR>        FaxModem fm= new FaxModem();
 *<BR>        pdf.pageImage=new java.awt.image.BufferedImage(800,1290,java.awt.image.BufferedImage.TYPE_INT_RGB);
 *<BR>
 *<BR>        FaxModem m=new FaxModem();
 *<BR>        m.setPortName("COM1");
 *<BR>        m.flowControl=m.FLOWCONTROL_XONXOFF;
 *<BR>        m.faxClass=1;
 *<BR>        m.AtFBOR=true;
 *<BR>        m.open(p);
 *<BR>  if (m.sendFax("11111111")) System.out.println("Success ***");
 *<BR>  else System.out.println("FAILED");
 *<BR>          m.close();
 *<BR>}
 *<BR>catch ( Exception e )   { e.printStackTrace();}
 *<BR>
 *<BR>
 *<BR>
 *
 */
public class AcrobatFaxProducer implements FaxProducer {


 /**
  * loaded PDFDocument
  */
  protected PDFDocument pdfDocument;

  /**
   * PDF File
   */
  private String pdfFile=null;

  /**
   * image
   */
  public java.awt.Image pageImage=null;

  int pageCount=0;

  /**
   * this is a dummy component required by acrobat reader
   */
   private static java.awt.Frame dummyFrame=new java.awt.Frame();





  public Image getFaxPage(int page) {

    // no file set
    if (pdfDocument==null) return null;
    // no more pages
    if (page>=pageCount) return null;

    return createImage(page+1); // first page is 1, not 0

  }

  /**
   * set PDF File, initialize everything
   */
   public void setPDFFile(String file) throws Exception {

     pdfDocument=new PDFDocument(new java.io.File(file));

     pageCount =pdfDocument.getNumPages();

   }


  /**
   * create page Image
   */
  private Image createImage(int page) {

     try {

         dummyFrame.setVisible(true);
/*         double a = 1;
         double d = 1;
         double b =0;
         double c =0;
         double x =0;
         double y = 0;
         pdfDocument.drawPage(page,dummyFrame.createImage(1000,1000),new com.adobe.acrobat.sidecar.AffineTransform(1,0,0,1,0,0),null,dummyFrame);


         dummyFrame.setVisible(f);
		*/
                FloatPoint cropBoxSize = pdfDocument.getPageSize(page-1);

			// Compute the appropriate current scale
			int width = pageImage.getWidth(null);
			int height =pageImage.getHeight(null);

			float hScale = (float)((width ) / cropBoxSize.x);
			float vScale = (float)((height) / cropBoxSize.y);
			float scale = (float)Math.min(hScale, vScale);

			// Compute the transform -- this is just the identity matrix scaled
			// by the computed scale.
			AffineTransform transform = new AffineTransform(scale, 0, 0, scale, 0, 0);

			// Create the Image object and draw into it
			int w = (int)(cropBoxSize.x * scale);
			int h = (int)(cropBoxSize.y * scale);
			Image osImage = dummyFrame.createImage(w, h);
                        System.out.println("before page "+(page-1));
			pdfDocument.drawPage(page-1, osImage, transform, null, dummyFrame);

                        dummyFrame.setVisible(false);

                        Graphics g=pageImage.getGraphics();
                        g.setColor(java.awt.Color.white);
                        g.fillRect(0,0,width,height);
                        g.drawImage(osImage,0,0,null);
                        System.out.println("page "+(page-1));

/*
  java.awt.image.BufferedImage image = new java.awt.image.BufferedImage( width,height,java.awt.image.BufferedImage.TYPE_INT_RGB );
            java.awt.Graphics imgGraphics = image.createGraphics();

              imgGraphics.drawImage(osImage,0,0,null);

            // open file
            java.io.File f=new java.io.File("c:\\j4l\\fax\\a.jpg");
            f.delete();
            java.io.FileOutputStream of=new java.io.FileOutputStream(f);

            // encode buffered image to a jpeg
            com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(of );
            encoder.encode( image );

            of.close();
*/


                        //pageImage=dummyFrame.createImage((int) cropBoxSize.x,(int) cropBoxSize.y);
                        //pdfDocument.drawPage(page,pageImage,new com.adobe.acrobat.sidecar.AffineTransform(1,0,0,1,0,0),null,dummyFrame);

      } catch (Exception pdfe) {
             pdfe.printStackTrace(System.err);
      }

      return pageImage;

  }


}