My Free Java Print Preview Class

Ok, for 2 weeks now ive been in agony trying to program a
print preview for a program im writing for work.
Alot, of the free print previews online are way complicated
and crazy and hard to implement.
Either that, or these forums and "how tos" online never
answered the question or were to general or hard
to follow for a beginner.
I even posted a $40 question on google answers on how to
do this and never got an answer.
If ive had this problem others must have also
so to save them the trouble i went through, here is a good
starting point.
I just finished this class.
All you have to do is pass it a JPanel (or i guess any component
with a " .paint(Graphics g) " ).
new PrintPreview( component );all you might have to change is JInternalFrame to JFrame which is
easy. also, the fit to page doesnt work yet but thats easy to code.
please, i encourage comments and polite criticism.
im just starting out so i know this isnt perfect but i posted
it with good intentions.
Hope it helps out some out there.
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.print.*;
import java.lang.Math.*;
import java.awt.image.*;
public class PrintPreview extends JInternalFrame implements ActionListener{
public PrintPreview(Component pc){
     super("Print Preview", true, true, true, true);
     PC = pc;
// buttons
     pf1 = new JRadioButton("Portrait");
        pf1.setActionCommand("1");
        pf1.setSelected(true);
        pf2 = new JRadioButton("Landscape");
        pf2.setActionCommand("2");
        pf.add(pf1);
        pf.add(pf2);
        pf1.setBackground(Pref.tbgc);
        pf2.setBackground(Pref.tbgc);
        cp.setBackground(Pref.tbgc);
     preview.addActionListener(this);
        print.addActionListener(this);
// set
     pp.setPreferredSize(new Dimension(460, 460));
     pp.setBorder(BorderFactory.createLineBorder (Color.black, 2));
     pp.setBackground(Pref.tbgc);
     hold.setPreferredSize(new Dimension(200, 280));
     hold.setBorder(BorderFactory.createLineBorder (Color.black, 2));
     hold.setBackground(Pref.tbgc);
// make main panel
     mp.setBackground(Pref.tbgc);
     GridBagLayout gridbag = new GridBagLayout();
     GridBagConstraints constraints = new GridBagConstraints();
     mp.setLayout(gridbag);
// make hold panel
     GridBagLayout g1 = new GridBagLayout();
     GridBagConstraints c1 = new GridBagConstraints();
     hold.setLayout(g1);
// DELETE
     PF.setOrientation(PF.PORTRAIT);
// GUI
// hold
     c1.insets.top = 5;
     c1.insets.left = 45;
     c1.insets.right = 5;
     buildConstraints(c1, 0, 2, 2, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(pf1, c1);
     hold.add(pf1);
     c1.insets.top = 2;
     buildConstraints(c1, 0, 4, 2, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(pf2, c1);
     hold.add(pf2);
     c1.insets.left = 5;
     c1.insets.right = 0;
     c1.insets.top = 25;
     buildConstraints(c1, 0, 6, 1, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(xsl, c1);
     hold.add(xsl);
     c1.insets.left = 0;
     c1.insets.right = 5;
     buildConstraints(c1, 1, 6, 1, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(xs, c1);
     hold.add(xs);
     c1.insets.left = 5;
     c1.insets.right = 0;
     c1.insets.top = 5;
     buildConstraints(c1, 0, 8, 1, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(ysl, c1);
     hold.add(ysl);
     c1.insets.left = 0;
     c1.insets.right = 5;
     buildConstraints(c1, 1, 8, 1, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(ys, c1);
     hold.add(ys);
     c1.insets.left = 25;
     c1.insets.right = 5;
     buildConstraints(c1, 0, 10, 2, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(cp, c1);
     hold.add(cp);
     c1.insets.left = 35;
     c1.insets.right = 35;
     c1.insets.top = 20;
     buildConstraints(c1, 0, 12, 2, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(ftp, c1);
     hold.add(ftp);
     c1.insets.left = 35;
     c1.insets.right = 35;
     c1.insets.top = 25;
     buildConstraints(c1, 0, 14, 2, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(preview, c1);
     hold.add(preview);
     c1.insets.bottom = 15;
     c1.insets.top = 5;
     buildConstraints(c1, 0, 16, 2, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(print, c1);
     hold.add(print);
     constraints.insets.top = 10;
     constraints.insets.left = 10;
     constraints.insets.bottom = 10;
     buildConstraints(constraints, 0, 2, 1, 1, 1, 1);
     constraints.fill = GridBagConstraints.NONE;
     gridbag.setConstraints(hold, constraints);
     mp.add(hold);
     constraints.insets.right = 10;
     buildConstraints(constraints, 1, 2, 1, 1, 1, 1);
     constraints.fill = GridBagConstraints.NONE;
     gridbag.setConstraints(pp, constraints);
     mp.add(pp);
// display
     this.setContentPane(mp);
     this.setVisible(true);
     this.setResizable(false);
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
     setSize(720, 520);
        Dimension windowSize = getSize();
     setLocation(Math.max(0,(screenSize.width -windowSize.width)/2),
        (Math.max(0,(screenSize.height-windowSize.height)/2)));
        DESKTOP.desktop.add(this);
        try {
     this.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
public void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, double wx, double wy){
     gbc.gridx = gx;
     gbc.gridy = gy;
     gbc.gridwidth = gw;
     gbc.gridheight = gh;
     gbc.weightx = wx;
     gbc.weighty = wy;
public class PreviewPage extends JPanel{
public PreviewPage(){
public void paint(Graphics g){
     super.paint(g);
     Graphics2D g2 = (Graphics2D)g;
// PORTRAIT
     if(PF.getOrientation() == PF.PORTRAIT){
     g.setColor(Color.black);
     g.drawRect(60, 10, 340, 440);
     int x1 = (int)Math.rint( ( (double)PF.getImageableX() / 72 ) * 40 );
     int y1 = (int)Math.rint( ( (double)PF.getImageableY() / 72 ) * 40 );
     int l1 = (int)Math.rint( ( (double)PF.getImageableWidth() / 72 ) * 40 );
     int h1 = (int)Math.rint( ( (double)PF.getImageableHeight() / 72 ) * 40 );
     g.setColor(Color.red);
     g.drawRect(x1+60, y1+10, l1, h1);
     setScales();
     int x2 = (int)Math.rint( 1028 / XSC );
     int y2 = (int)Math.rint( 768 / YSC );
     Image image = new BufferedImage( x2, y2, BufferedImage.TYPE_INT_ARGB);
     PC.paint( image.getGraphics() );
     g.drawImage(image, x1+60, y1+10, l1, h1, this);
// LANDSCAPE
     if(PF.getOrientation() == PF.LANDSCAPE){
     g.setColor(Color.black);
     g.drawRect(10, 60, 440, 340);
     int x1 = (int)Math.rint( ( (double)PF.getImageableX() / 72 ) * 40 );
     int y1 = (int)Math.rint( ( (double)PF.getImageableY() / 72 ) * 40 );
     int l1 = (int)Math.rint( ( (double)PF.getImageableWidth() / 72 ) * 40 );
     int h1 = (int)Math.rint( ( (double)PF.getImageableHeight() / 72 ) * 40 );
     g.setColor(Color.red);
     g.drawRect(x1+10, y1+60, l1, h1);
     setScales();
     int x2 = (int)Math.rint( 1028 / XSC );
     int y2 = (int)Math.rint( 768 / YSC );
     Image image = new BufferedImage( x2, y2, BufferedImage.TYPE_INT_ARGB);
     PC.paint( image.getGraphics() );
     g.drawImage(image, x1+10, y1+60, l1, h1, this);
public void actionPerformed(ActionEvent e){
// fit to page
     if(e.getSource() == ftp){
// preview
     if(e.getSource() == preview){
     setProperties();
// print
     if(e.getSource() == print){
     doPrint();
public void setProperties(){
     if(pf1.isSelected() == true){
     PF.setOrientation(PF.PORTRAIT);
     if(pf2.isSelected() == true){
     PF.setOrientation(PF.LANDSCAPE);
     setScales();
     pp.repaint();
public void setScales(){
     try{
     XSC = Double.parseDouble( xs.getText() );
     } catch (NumberFormatException e) { }
     try{
     YSC = Double.parseDouble( ys.getText() );
     } catch (NumberFormatException e) { }
public void doPrint(){
     PrintThis();
public void PrintThis(){
     PrinterJob printerJob = PrinterJob.getPrinterJob();
     Book book = new Book();
     book.append( new PrintPage(), PF );
     printerJob.setPageable(book);
     boolean doPrint = printerJob.printDialog();
     if (doPrint) {
     try {
     printerJob.print();
     } catch (PrinterException exception) {
     System.err.println("Printing error: " + exception);
public class PrintPage implements Printable{
public int print(Graphics g, PageFormat format, int pageIndex) {
     Graphics2D g2D = (Graphics2D) g;
     g2D.translate(format.getImageableX (), format.getImageableY ());
     disableDoubleBuffering(mp);
     System.out.println("get i x " + format.getImageableX ());
     System.out.println("get i x " + format.getImageableY ());
     System.out.println("getx: " + format.getImageableWidth() );
     System.out.println("getx: " + format.getImageableHeight() );
     // scale to fill the page
     double dw = format.getImageableWidth();
     double dh = format.getImageableHeight();
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
     setScales();
     double xScale = dw / (1028/XSC);
     double yScale = dh / (768/YSC);
     double scale = Math.min(xScale,yScale);
     System.out.println("" + scale);
     g2D.scale( xScale, yScale);
     ((JPanel)PC).paint(g);
     enableDoubleBuffering(mp);
     return Printable.PAGE_EXISTS;
public void disableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
public void enableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);
     Component PC;
     PageFormat PF = new PageFormat();
     double XSC = 1;
     double YSC = 1;
// gui comp
     JPanel mp = new JPanel();
     JPanel hold = new JPanel();
     JPanel pp = new PreviewPage();
     ButtonGroup pf = new ButtonGroup();
     JRadioButton pf1;
     JRadioButton pf2;
     JLabel xsl = new JLabel("X Scale:", JLabel.LEFT);
     JTextField xs = new JTextField("1");
     JLabel ysl = new JLabel("Y Scale:", JLabel.LEFT);
     JTextField ys = new JTextField("1");
     JButton ftp = new JButton("Fit to Page");
     JCheckBox cp = new JCheckBox("Constrain Proportions");
     JButton preview = new JButton("PREVIEW");
     JButton print = new JButton("PRINT");
}

Ok, for 2 weeks now ive been in agony trying to program a
print preview for a program im writing for work.
Alot, of the free print previews online are way complicated
and crazy and hard to implement.
Either that, or these forums and "how tos" online never
answered the question or were to general or hard
to follow for a beginner.
I even posted a $40 question on google answers on how to
do this and never got an answer.
If ive had this problem others must have also
so to save them the trouble i went through, here is a good
starting point.
I just finished this class.
All you have to do is pass it a JPanel (or i guess any component
with a " .paint(Graphics g) " ).
new PrintPreview( component );all you might have to change is JInternalFrame to JFrame which is
easy. also, the fit to page doesnt work yet but thats easy to code.
please, i encourage comments and polite criticism.
im just starting out so i know this isnt perfect but i posted
it with good intentions.
Hope it helps out some out there.
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.print.*;
import java.lang.Math.*;
import java.awt.image.*;
public class PrintPreview extends JInternalFrame implements ActionListener{
public PrintPreview(Component pc){
     super("Print Preview", true, true, true, true);
     PC = pc;
// buttons
     pf1 = new JRadioButton("Portrait");
        pf1.setActionCommand("1");
        pf1.setSelected(true);
        pf2 = new JRadioButton("Landscape");
        pf2.setActionCommand("2");
        pf.add(pf1);
        pf.add(pf2);
        pf1.setBackground(Pref.tbgc);
        pf2.setBackground(Pref.tbgc);
        cp.setBackground(Pref.tbgc);
     preview.addActionListener(this);
        print.addActionListener(this);
// set
     pp.setPreferredSize(new Dimension(460, 460));
     pp.setBorder(BorderFactory.createLineBorder (Color.black, 2));
     pp.setBackground(Pref.tbgc);
     hold.setPreferredSize(new Dimension(200, 280));
     hold.setBorder(BorderFactory.createLineBorder (Color.black, 2));
     hold.setBackground(Pref.tbgc);
// make main panel
     mp.setBackground(Pref.tbgc);
     GridBagLayout gridbag = new GridBagLayout();
     GridBagConstraints constraints = new GridBagConstraints();
     mp.setLayout(gridbag);
// make hold panel
     GridBagLayout g1 = new GridBagLayout();
     GridBagConstraints c1 = new GridBagConstraints();
     hold.setLayout(g1);
// DELETE
     PF.setOrientation(PF.PORTRAIT);
// GUI
// hold
     c1.insets.top = 5;
     c1.insets.left = 45;
     c1.insets.right = 5;
     buildConstraints(c1, 0, 2, 2, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(pf1, c1);
     hold.add(pf1);
     c1.insets.top = 2;
     buildConstraints(c1, 0, 4, 2, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(pf2, c1);
     hold.add(pf2);
     c1.insets.left = 5;
     c1.insets.right = 0;
     c1.insets.top = 25;
     buildConstraints(c1, 0, 6, 1, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(xsl, c1);
     hold.add(xsl);
     c1.insets.left = 0;
     c1.insets.right = 5;
     buildConstraints(c1, 1, 6, 1, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(xs, c1);
     hold.add(xs);
     c1.insets.left = 5;
     c1.insets.right = 0;
     c1.insets.top = 5;
     buildConstraints(c1, 0, 8, 1, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(ysl, c1);
     hold.add(ysl);
     c1.insets.left = 0;
     c1.insets.right = 5;
     buildConstraints(c1, 1, 8, 1, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(ys, c1);
     hold.add(ys);
     c1.insets.left = 25;
     c1.insets.right = 5;
     buildConstraints(c1, 0, 10, 2, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(cp, c1);
     hold.add(cp);
     c1.insets.left = 35;
     c1.insets.right = 35;
     c1.insets.top = 20;
     buildConstraints(c1, 0, 12, 2, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(ftp, c1);
     hold.add(ftp);
     c1.insets.left = 35;
     c1.insets.right = 35;
     c1.insets.top = 25;
     buildConstraints(c1, 0, 14, 2, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(preview, c1);
     hold.add(preview);
     c1.insets.bottom = 15;
     c1.insets.top = 5;
     buildConstraints(c1, 0, 16, 2, 1, 1, 1);
     c1.fill = GridBagConstraints.BOTH;
     g1.setConstraints(print, c1);
     hold.add(print);
     constraints.insets.top = 10;
     constraints.insets.left = 10;
     constraints.insets.bottom = 10;
     buildConstraints(constraints, 0, 2, 1, 1, 1, 1);
     constraints.fill = GridBagConstraints.NONE;
     gridbag.setConstraints(hold, constraints);
     mp.add(hold);
     constraints.insets.right = 10;
     buildConstraints(constraints, 1, 2, 1, 1, 1, 1);
     constraints.fill = GridBagConstraints.NONE;
     gridbag.setConstraints(pp, constraints);
     mp.add(pp);
// display
     this.setContentPane(mp);
     this.setVisible(true);
     this.setResizable(false);
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
     setSize(720, 520);
        Dimension windowSize = getSize();
     setLocation(Math.max(0,(screenSize.width -windowSize.width)/2),
        (Math.max(0,(screenSize.height-windowSize.height)/2)));
        DESKTOP.desktop.add(this);
        try {
     this.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
public void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, double wx, double wy){
     gbc.gridx = gx;
     gbc.gridy = gy;
     gbc.gridwidth = gw;
     gbc.gridheight = gh;
     gbc.weightx = wx;
     gbc.weighty = wy;
public class PreviewPage extends JPanel{
public PreviewPage(){
public void paint(Graphics g){
     super.paint(g);
     Graphics2D g2 = (Graphics2D)g;
// PORTRAIT
     if(PF.getOrientation() == PF.PORTRAIT){
     g.setColor(Color.black);
     g.drawRect(60, 10, 340, 440);
     int x1 = (int)Math.rint( ( (double)PF.getImageableX() / 72 ) * 40 );
     int y1 = (int)Math.rint( ( (double)PF.getImageableY() / 72 ) * 40 );
     int l1 = (int)Math.rint( ( (double)PF.getImageableWidth() / 72 ) * 40 );
     int h1 = (int)Math.rint( ( (double)PF.getImageableHeight() / 72 ) * 40 );
     g.setColor(Color.red);
     g.drawRect(x1+60, y1+10, l1, h1);
     setScales();
     int x2 = (int)Math.rint( 1028 / XSC );
     int y2 = (int)Math.rint( 768 / YSC );
     Image image = new BufferedImage( x2, y2, BufferedImage.TYPE_INT_ARGB);
     PC.paint( image.getGraphics() );
     g.drawImage(image, x1+60, y1+10, l1, h1, this);
// LANDSCAPE
     if(PF.getOrientation() == PF.LANDSCAPE){
     g.setColor(Color.black);
     g.drawRect(10, 60, 440, 340);
     int x1 = (int)Math.rint( ( (double)PF.getImageableX() / 72 ) * 40 );
     int y1 = (int)Math.rint( ( (double)PF.getImageableY() / 72 ) * 40 );
     int l1 = (int)Math.rint( ( (double)PF.getImageableWidth() / 72 ) * 40 );
     int h1 = (int)Math.rint( ( (double)PF.getImageableHeight() / 72 ) * 40 );
     g.setColor(Color.red);
     g.drawRect(x1+10, y1+60, l1, h1);
     setScales();
     int x2 = (int)Math.rint( 1028 / XSC );
     int y2 = (int)Math.rint( 768 / YSC );
     Image image = new BufferedImage( x2, y2, BufferedImage.TYPE_INT_ARGB);
     PC.paint( image.getGraphics() );
     g.drawImage(image, x1+10, y1+60, l1, h1, this);
public void actionPerformed(ActionEvent e){
// fit to page
     if(e.getSource() == ftp){
// preview
     if(e.getSource() == preview){
     setProperties();
// print
     if(e.getSource() == print){
     doPrint();
public void setProperties(){
     if(pf1.isSelected() == true){
     PF.setOrientation(PF.PORTRAIT);
     if(pf2.isSelected() == true){
     PF.setOrientation(PF.LANDSCAPE);
     setScales();
     pp.repaint();
public void setScales(){
     try{
     XSC = Double.parseDouble( xs.getText() );
     } catch (NumberFormatException e) { }
     try{
     YSC = Double.parseDouble( ys.getText() );
     } catch (NumberFormatException e) { }
public void doPrint(){
     PrintThis();
public void PrintThis(){
     PrinterJob printerJob = PrinterJob.getPrinterJob();
     Book book = new Book();
     book.append( new PrintPage(), PF );
     printerJob.setPageable(book);
     boolean doPrint = printerJob.printDialog();
     if (doPrint) {
     try {
     printerJob.print();
     } catch (PrinterException exception) {
     System.err.println("Printing error: " + exception);
public class PrintPage implements Printable{
public int print(Graphics g, PageFormat format, int pageIndex) {
     Graphics2D g2D = (Graphics2D) g;
     g2D.translate(format.getImageableX (), format.getImageableY ());
     disableDoubleBuffering(mp);
     System.out.println("get i x " + format.getImageableX ());
     System.out.println("get i x " + format.getImageableY ());
     System.out.println("getx: " + format.getImageableWidth() );
     System.out.println("getx: " + format.getImageableHeight() );
     // scale to fill the page
     double dw = format.getImageableWidth();
     double dh = format.getImageableHeight();
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
     setScales();
     double xScale = dw / (1028/XSC);
     double yScale = dh / (768/YSC);
     double scale = Math.min(xScale,yScale);
     System.out.println("" + scale);
     g2D.scale( xScale, yScale);
     ((JPanel)PC).paint(g);
     enableDoubleBuffering(mp);
     return Printable.PAGE_EXISTS;
public void disableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
public void enableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);
     Component PC;
     PageFormat PF = new PageFormat();
     double XSC = 1;
     double YSC = 1;
// gui comp
     JPanel mp = new JPanel();
     JPanel hold = new JPanel();
     JPanel pp = new PreviewPage();
     ButtonGroup pf = new ButtonGroup();
     JRadioButton pf1;
     JRadioButton pf2;
     JLabel xsl = new JLabel("X Scale:", JLabel.LEFT);
     JTextField xs = new JTextField("1");
     JLabel ysl = new JLabel("Y Scale:", JLabel.LEFT);
     JTextField ys = new JTextField("1");
     JButton ftp = new JButton("Fit to Page");
     JCheckBox cp = new JCheckBox("Constrain Proportions");
     JButton preview = new JButton("PREVIEW");
     JButton print = new JButton("PRINT");
}

Similar Messages

  • Print Preview class

    I'm trying to build a WordProcessor and i'm stuck on the print preview.
    I've been using the Manning book, Swing. I think they have some typo's
    in the print chapter or I'm missing something.
    Well here the problem:
    Wordwrite.java:103: cannot resolve symbol
    symbol : constructor PrintView (Wordwrite)
    location : class Wordwrite.PrintView
    m_printView = new PrintView(Wordwrite.this);
    My code is slightly off from the books
    Here's what the book had
    new PrintPreview(Wordwrite.this);
    for your info: PrintPreview is only mentioned here and nowhere else.
    Any Clues.

    The error is telling you that either the class PrintView doesn't exist or that it doesn't have a constructor that takes Wordwrite as an argument.
    Is there a PrintView class? Perhaps you've called it PrintPreview? If there's a PrintPreview class then change the line that says
    m_printView = new PrintView(...)
    to
    m_printView = new PrintPreview(...)
    Hope this helps.

  • Where's the Java Print Preview Option?

    I've tested v13 for a few weeks now, and the only apparent problem I'm having surrounds the pdf printer. We have Adobe Pro-9 installed on our clients.
    When selecting Print Preview or Advanced Print, I can't get past the "Contract Manager pdf Printer" error.
    Previous versions provided a configuration option between Java and pdf... where'd that option go? I can't seem to locate it.
    Thanks in advance...

    There is no option anymore for Java printing vs ActiveX vs anything else. PDF printing is the only choice. What is the exact error message? Log in to the CM13 web server as the account used to start the service and verify the CM PDF Printer is available. If not, I believe you can install the PDF printer directly from the amyuni folder on the CD, then you may need to rename the printer to Contract Manager PDF Printer.

  • How i will see print Preview of an excel by clicking a button in java

    hi,
    I have created an excel file from java. Now i want to see the print preview of this file by clicking a button in java? Is it possible in java? I have used JExcelApi for this. Is there anybody can help me?
    This is my code:
    package image_handle;
        import jxl.Workbook;
        import jxl.write.*;
        import java.io.*;
        import javax.print.*;
        import javax.print.attribute.*;
    import javax.print.attribute.standard.*;
        public class Excel {
          public Excel(String name) {
           try{
            Workbook wb= Workbook.getWorkbook(new File("report.xls"));
           WritableWorkbook copy=Workbook.createWorkbook(new File("Workbook.xls"),wb);
          WritableSheet sheet = copy.getSheet(0);
          Label label = new Label(0,1,"Bina");
          sheet.addCell(label);
          Label label1 = new Label(1,1,"187");
          sheet.addCell(label1);
          WritableImage image = new WritableImage(2,3,2,4,new File(name));
          sheet.addImage(image);
          copy.write();
          sheet.p
           copy.close();
          catch(Exception ex){
            ex.printStackTrace();
    }I need solution urgently. PLease help me.
    With regards
    Bina

    Multi-Post
    http://forum.java.sun.com/thread.jspa?threadID=731384&tstart=-1

  • How to show print Preview of an file in java

    Hi,
    Suppose i have a file like "bina.xls". Now i want to see the print preview of this file from java code. How is this possible? I s there anybody can help me? now i am in serious trouble with this? I can generate an excel file. But i can not show this file with JExcelApi. i can know that this is possible with jasper report, how this is possible with Jasper Report ? if any one help me with sample code then i will be very gratefull to u . Please help me.
    With Regards
    Bina

    This forum is exclusively for Sun Java Studio Creator. Please post your queries on appropriate forums to have better discussions :
    e.g.
    Java Technology Forums - Java Programming :
    http://forum.java.sun.com/forum.jspa?forumID=31

  • GUI JAVA 7.20 rev.2 (on Mac OSX 10.6.3): Print preview does not work!

    I've just started using rev 2 of java sapgui and print preview is not working anymore.
    Client: OSX 10.6.3
    SAP: ERP5.0 on WAS 640
    Has anyone experienced the same problem?

    This is the output in console when print-previewing:
    I'm going to open a support message as adviced.
    <code>
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]     Exception in thread "Loader for htmlViewer8_XZ_7" java.lang.NullPointerException
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at com.sap.plaf.frog.FrogListUI.updateColors(FrogListUI.java:63)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at com.sap.plaf.frog.FrogListUI.propertyChange(FrogListUI.java:48)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:339)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:276)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:318)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at java.awt.Component.firePropertyChange(Component.java:8233)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at javax.swing.JComponent.firePropertyChange(JComponent.java:4428)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at javax.swing.JComponent.setEnabled(JComponent.java:2638)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at org.icepdf.ri.common.annotation.ActionsPanel.setEnabled(ActionsPanel.java:418)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at org.icepdf.ri.common.annotation.ActionsPanel.<init>(ActionsPanel.java:106)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at org.icepdf.ri.common.annotation.AnnotationPanel.setGUI(AnnotationPanel.java:128)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at org.icepdf.ri.common.annotation.AnnotationPanel.<init>(AnnotationPanel.java:73)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at org.icepdf.ri.common.SwingViewBuilder.buildAnnotationPanel(SwingViewBuilder.java:1574)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at org.icepdf.ri.common.SwingViewBuilder.buildUtilityTabbedPane(SwingViewBuilder.java:1541)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at org.icepdf.ri.common.SwingViewBuilder.buildUtilityAndDocumentSplitPane(SwingViewBuilder.java:1501)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at org.icepdf.ri.common.SwingViewBuilder.buildContents(SwingViewBuilder.java:499)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at org.icepdf.ri.common.SwingViewBuilder.buildViewerPanel(SwingViewBuilder.java:482)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at ice.pilots.pdf.ThePilot.createComponent(ThePilot.java:149)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at ice.storm.Viewport.setPilot(VCDO)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at ice.storm.StormBase.append(VCDO)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at ice.storm.StormBase.do_clear_content(VCDO)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at ice.storm.StormBase.do_render_content(VCDO)
    04/05/10 10.45.41     [0x0-0x3e63e6].com.sap.platin[6491]          at ice.storm.Viewport.runAsynchronousLoad(VCDO)
    </code>
    Edited by: Lorenzo Nicora on May 4, 2010 10:52 AM

  • How to print the image data stored in object of 'java.io.File' Class in jsp

    I have created a file object for the image file in the system temporary directory.Now I want to display it in my page. please tell,How to print the image data stored in object(in my program it is imgr) of 'java.io.File' Class in jsp

    Create a servlet which gets an InputStream of the file and writes it to the OutputStream of the response and call this servlet in `src` attribute. That's basically all.
    Here's an example which covers the most of the minimum needs: [http://balusc.blogspot.com/2007/04/imageservlet.html].

  • Since installing Firefox 4, I keep getting this message "TypeError: Components.classes[cid] is undefined" when a window should open. Also print and print preview don't work properly anymore.

    Since installing the newest version of Firefox, This message "TypeError: Components.classes[cid] is undefined" appears in a box when a new window should open. Also, what you see in print print preview is not what prints.

    This type of error is usually caused by an add-on you are using that is not compatible. It could be anything from an antivirus link scanner like AVG Safe Search add-on, or McAfee Site Advisor add-on, or something similar. To confirm what add-on is causing this use the procedure in this link:
    https://support.mozilla.com/kb/Troubleshooting+extensions+and+themes
    Try Disabling the last add-on that was installed prior to upgrading to version 5.0
    Check and tell if its working.

  • Java Printing - Print Preview

    Hey, could anybody point me in the direction on how to create a print preview dialog box. I can already print the document that I created. Thanks for the help.

    public static void printPreview(BufferedImage buffer, Pageable job, PageFormat pageFormat, int page) {
       Graphics2D  g = (Graphics2D) buffer.getGraphics();
       //figure out the size of your page and the size of your buffer, and scale appropriately using Graphics2D.scale
       Pageable.getPrintable(g, pageFormat, page);
    }

  • Print & Print Preview in JSP application?

    Hi,
    I moved from swing to JSP. Trying to convert same Swing
    application to JSP application.In the swing application I used
    PRINT method in JPanel classes which implements PRINTABLE
    interface to format and print the data to the printer. Also I
    developed my own class to print preview the data(on a JFrame)
    before sending to the printer. So, each screen had print and
    print preview abilities.
    Now I&#8217;d like to have this functionality in JSP application too.
    Would appreciate to advice me if I could used same print preview
    java class(developed for swing application) in JSP application.
    If yes, please explain how. If No, please advice me how could I
    have print and print preview ability in JSP application.
    Any idea would appreciated.
    Thanks...Ali

    Hi,
    I am asking the JSP team to look at your question to see if
    they can help you.
    As you probably know, many java client functionalities will
    not map to JSP client functionality. That is why some people
    still prefer a java client if they want a richer UI.

  • How to do Print, Print Preview & Page Setup?

    do you have any sample to do Print, Print Preview & Page Setup.
    thanks very much!

    I have a JTable on a dialog and wanted to print it. I discovered the following method (may or may not work for you).
    1) Simply extend your class to implement Prinatable....
    public class EbertPanelProductivityHistory extends JPanel implements Printable {
    2) As pare of your class in #1 make a print() method as follows (i.e. implement print() for Printable)
    public int print( Graphics g, PageFormat pageFormat, int page ) {
    int i,j,k,x,y;
    Graphics2D g2d;
    // We're done! (page is 0 based)
    if( page >= ReportDataPrintPageCnt ) {
    return (NO_SUCH_PAGE);
    // Create a graphic2D object and set the default parameters
    g2d = (Graphics2D)g;
    g2d.setColor( Color.black );
    etc. etc. etc....
    // PRINT IT!!!
    return( PAGE_EXISTS );
    3) And then you can print yourself...
    private void PrintActionPerformed(java.awt.event.ActionEvent evt) {
    PrinterJob PrintJob = PrinterJob.getPrinterJob(); // printer access - instance of PrinterJob
    /**** THIS IS OURSELF - i.e. our class with print() implemented that implements Printable **********/
    PrintJob.setPrintable(this); // this class implements Printable - see print()
    PrintJob.setJobName( Title ); // job name
    if( PrintJob.printDialog() ) {                    // print dialog!
    try { PrintJob.print(); } catch (Exception e) {
    javax.swing.JOptionPane.showMessageDialog(null, MSG_ID + MSG_ERR + e.getMessage(), "Alert", javax.swing.JOptionPane.ERROR_MESSAGE);

  • Create a Print Preview

    Hi, how can i create a print preview for a Text Component and/or a JTable (or any other component)?
    I know that i must write the print method but i need some intresting examples of all the classes that i have to develop.
    Thanks

    If you only want to print content that can be
    displayed by a JEditorPane then you may want to check
    out my DocumentPrinter, which you can get from
    http://www.dcs.shef.ac.uk/~mark/software/swing/
    As well as printing Documents that can be loaded by a
    JEditorPane it also allows you to specify headers and
    footers and provides a print preview dialog box.
    At the moment it works but it could do with some
    improvements including memory usage which I'm
    currently working on. Any feedback if you do use it
    or feature requests would be appreciated,
    MarkHi Mark,
    I must commend you for developing such a wondeful java printing solution (Document Printer). I have a question, is it posible for me to be able to call a new page from my code. I have generated some pay slips from my application with HTML and i want only 2 pay slips per page. Is it possible for me to call a new page from my code using document printer....please i need it badly...
    Thanks
    Jaji Lukman

  • How do i use java printing api 1.4

    How can i print documents using jdk1.4 api.
    I have used the following program for printing.
    import java.io.*;
    import java.awt.*;
    import java.awt.print.*;
    import javax.print.*;
    import javax.print.attribute.*;
    import javax.print.attribute.standard.*;
    public class Print2DtoStream implements Printable{
    public Print2DtoStream() {
    /* Use the pre-defined flavor for a Printable from an InputStream */
    DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    /* Specify the type of the output stream */
    String psMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();
    /* Locate factory which can export a GIF image stream as Postscript */
    StreamPrintServiceFactory[] factories =
    StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, psMimeType);
    if (factories.length == 0) {
    System.err.println("No suitable factories");
    System.exit(0);
    try {
    /* Create a file for the exported postscript */
    FileOutputStream fos = new FileOutputStream("out.ps");
    /* Create a Stream printer for Postscript */
    StreamPrintService sps = factories[0].getPrintService(fos);
    /* Create and call a Print Job */
    DocPrintJob pj = sps.createPrintJob();
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    Doc doc = new SimpleDoc(this, flavor, null);
    pj.print(doc, aset);
    fos.close();
    } catch (PrintException pe) {
    System.err.println(pe);
    } catch (IOException ie) {
    System.err.println(ie);
    public int print(Graphics g,PageFormat pf,int pageIndex) {
    if (pageIndex == 0) {
    Graphics2D g2d= (Graphics2D)g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());
    g2d.setColor(Color.black);
    g2d.drawString("example string", 250, 250);
    g2d.fillRect(0, 0, 200, 200);
    return Printable.PAGE_EXISTS;
    } else {
    return Printable.NO_SUCH_PAGE;
    public static void main(String args[]) {
    Print2DtoStream sp = new Print2DtoStream();
    However when i run this program,it prints "example string" as used in g2d.drawString("example string",250,250) method above.
    What if i want to print content from any file.
    So anybody konwing this plz reply

    This is covered pretty well in the Java Print Service API Guide.
    You could start here http://java.sun.com/j2se/1.4.1/docs/guide/jps/spec/printing.fm1.html
    Basically, you have two choices - either you're going to throw a document at the print service, or you're going to print using Graphics2D commands. You also have the choice between printing to a printer, or to a stream. Mix and match for four combinations.
    Whether you can just take a raw document and send it to a printer via the document printing stuff depends on what print services you have installed. For the standard J2SE, it's pretty limited.
    If you're doing it via the Graphics2D approach, however, you can print anything you can draw. That's nice - it means you can use the same code to display stuff on the screen and print it.

  • JasperReports - how to save as PDF from Print Preview page?

    Hi all!
    How can I save reports created with JasperReperts (with iReport tool) as PDF?
    On print preview, when I select it is as PDF, and click on "save", nothing is saved. Also, I get NoClassDefFoundError despite I included all nessesry classes (did I?). Here are list of included classes
    jasperreports-1.2.7.jar;
    jasperreports-1.2.7-javaflow.jar;
    commons-beanutils-1.5.jar;
    commons-collections-2.1.jar;
    commons-logging-1.0.2.jar;
    commons-digester-1.7.jar;Here is my code:
    void printWithPreview() {
        try {
          JasperPrint print = JasperFillManager.fillReport(report, reportParam, conn);
          JasperViewer.viewReport(print, false);
          //byte[] pdfasbytes = JasperExportManager.exportReportToPdf(print); // can I use it somehow?
          JasperExportManager.exportReportToPdf(print);
        } catch (JRException e) {
          e.printStackTrace();
      }Can you help me with this?
    Also, I need to know, how to limit options "save as" only to PFD (on same Print Preview page)?
    Thank you in advance!

    Right!
    I was missing iText.jar.
    So, you need only this code to get report preview and feature to export to pdf, html and other formats:
    void printWithPreview() {
        try {
          JasperPrint print = JasperFillManager.fillReport(report, reportParam, conn);
          JasperViewer.viewReport(print, false);
          JasperExportManager.exportReportToPdf(print);
        } catch (JRException e) {
          e.printStackTrace();
      }You do not need to save it specificly to c: or other "hard coded" directory.
    Thanx 1000 times!!!

  • Report Admin - Print Preview Error

    In reports admin, I get the following error when I select print preview:error marshalling arguments; nested exception is; java.net.SocketException; Socket write failed.Any ideas?

    Hi
    Are you using <b>crystal wave</b>  a third party add  or the crystal report add on provided by SAP.
    1.Are all your user facing same issue ?
    2.Did it happen to only item or it is happening to all ?
    So far looks to me ,some fields have been deleted from template. It might be the cause .
    Hard to troubleshoot if it is coming from addon ...
    Well try restoring a default template (if you have ) or some other layout to test the issue.
    Thank you
    Bishal

Maybe you are looking for

  • How to type a Copyright Symbol

    does the new elements 11 have the uk copyright symbol in the text options. Message title was edited by: Brett N

  • Itunes backup error 4480

    I am trying to backup my itunes library so I can have it on my new computer - the backup doesnt work and I get error message 4480 and the disc is blank. Can anyone help?

  • Different Payement terms & Incoterms

    Hi friends I have 2 orders with respective deliveries. Payment terms and Incoterms are different from 1st order to 2nd order. Now can we get SAME INVOICE NUMBER for both the deliveries?? Urgent pleasee. Regards Ramesh

  • Default Category Dispositions Workflow in URM11g!!

    Hi, In URM11g set up guide I could see the 'Category Dispositions Workflow' in configuration options chapter. I would like to know whether this workflow will help me to fulfill the below scenario. "I have to approve the contents for disposition in UR

  • Why do we need to need Desktop Experience to get Disk Cleanup?

    Why do we need to need Desktop Experience to get Disk Cleanup? What other options do we have?