
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
// The iText PDF jar file is available at:
//        http://sourceforge.net/projects/itext/
//    or  http://www.lowagie.com/iText/
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;


/**
 * Demonstrates the incorrect way to use GradientPaint. When the app is run,
 * hit the button in the lower right corner, pick a file, and save it.
 * Then open the saved file in your favorite PDF viewer (Acrobat 7 in my case)
 * and two of the boxes are solid green.
 */
public class p {

    private JFrame frame;
    private Dummy dummy;
    
    p() {
        frame = new JFrame("PDF Test");

        dummy  = new Dummy();
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(new JScrollPane(dummy), BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 3,3));
        JButton button;

        button = new JButton("Make PDF");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                doStuff();
            }
        });
        buttonPanel.add(button);

        panel.add(buttonPanel, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setSize(600,400);
        frame.show();

        
        
    }
        
    private void doStuff() {
        JFileChooser fc = new JFileChooser();
        fc.setDialogType(JFileChooser.SAVE_DIALOG);
        fc.setDialogTitle("Save PDF");
        int response = fc.showSaveDialog(frame);
        if (response != JFileChooser.APPROVE_OPTION) {
            return;
        }
        File file = fc.getSelectedFile();

        FileOutputStream fout = null;
        try {
            fout = new FileOutputStream(file);
            savePDF_scalable(fout);
        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(frame,
                "Unable to create the PDF file. Check Standard-Err for stack trace.",
                "Test failed!",
                JOptionPane.WARNING_MESSAGE);
            
        } finally {
            if (fout != null) {
                try {
                    fout.close();
                } catch (Exception do_not_care) {}
            }
        }
    }

    /**
     * Saves the PDH as a collection of graphics objects, which results
     * in a scalable PDF file, however the jgraph cell renderer doesn't 
     * seem to work at zoom levels greater than 0.1.
     */
    private void savePDF_scalable(FileOutputStream fout) throws Exception {
        int width = dummy.getWidth();
        int height = dummy.getHeight();
        Document document = new Document(new com.lowagie.text.
                Rectangle(width, height));
        PdfWriter writer = PdfWriter.getInstance(document, fout);
        document.addAuthor("Help/Systems test app");
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics2D g2 = tp.createGraphics(width, height,
                new DefaultFontMapper());
        
        // doesn't matter if you use printAll, paintAll, or anything else.
        dummy.paintAll(g2);
        //jgraph.printAll(g2);

        g2.dispose();
        cb.addTemplate(tp, 0, 0);
        // add the creation date at the bottom.
        cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA,
                BaseFont.WINANSI, false), 6.0f);
        cb.moveTo(0, height - 30);
        cb.beginText();
        cb.showText("PDF File created " + (new java.util.Date()));
        cb.endText();
        // close it all up
        document.close();
    }        



    public static void main(String[] args) {
        p app = new p();
    }


    class Dummy extends JComponent {

        Dummy() {
            setPreferredSize(new Dimension(700,700));
        }
        
        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;

            Paint paint = new GradientPaint(0, 0, Color.white,
                100, 60, Color.green, true);
            
            g2.setPaint(paint);
            // this one is fine.
            g2.fillRect(0, 0, 100, 60);

            // this one comes out solid green
            g2.fillRect(300, 300, 100, 60);
            // and so does this one.
            g2.fillRect(600, 600, 100, 70);
            
            g2.setColor(Color.red);
            g2.drawString("Hello there!", 30,30);
            g2.drawString("My background is solid green in the PDF", 250,330);
            g2.drawString("Pick Me!", 650,650);
            
        }
    }        
   
}
