IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Impression d'une Facture


Sujet :

AWT/Swing Java

  1. #1
    Membre confirmé Avatar de javaNavCha
    Homme Profil pro
    EKG Group
    Inscrit en
    Juillet 2009
    Messages
    311
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : Tunisie

    Informations professionnelles :
    Activité : EKG Group
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2009
    Messages : 311
    Points : 631
    Points
    631
    Par défaut Impression d'une Facture
    Bonjour a tous

    Je suis entrain de développer un projet de GPAO, et je me suis arrivé à l'impression des rapports. Les rapports sont visualisés dans des JFrame.
    Voici le code de la classe d'impression
    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.print.*;
     
    public class PrintUIWindow implements Printable, ActionListener {
     
        JFrame frameToPrint;
     
        public int print(Graphics g, PageFormat pf, int page) throws
                                                            PrinterException {
     
            if (page > 0) { /* We have only one page, and 'page' is zero-based */
                return NO_SUCH_PAGE;
            }
     
            /* User (0,0) is typically outside the imageable area, so we must
             * translate by the X and Y values in the PageFormat to avoid clipping
             */
            Graphics2D g2d = (Graphics2D)g;
            g2d.translate(pf.getImageableX(), pf.getImageableY());
     
            /* Now print the window and its visible contents */
            frameToPrint.printAll(g);
     
            /* tell the caller that this page is part of the printed document */
            return PAGE_EXISTS;
        }
     
        public void actionPerformed(ActionEvent e) {
             PrinterJob job = PrinterJob.getPrinterJob();
             job.setPrintable(this);
             boolean ok = job.printDialog();
             if (ok) {
                 try {
                      job.print();
                 } catch (PrinterException ex) {
                  /* The job did not successfully complete */
                 }
             }
        }
     
        public PrintUIWindow(JFrame f) {
            frameToPrint = f;
        }
     
     /*  public static void main(String args[]) {
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            JFrame f = new JFrame("Print UI Example");
            f.addWindowListener(new WindowAdapter() {
               public void windowClosing(WindowEvent e) {System.exit(0);}
            });
            JTextArea text = new JTextArea(50, 20);
            for (int i=1;i<=50;i++) {
                text.append("Line " + i + "\n");
            }
            JScrollPane pane = new JScrollPane(text);
            pane.setPreferredSize(new Dimension(250,200));
            f.add("Center", pane);
            JButton printButton = new JButton("Print This Window");
            printButton.addActionListener(new PrintUIWindow(f));
            f.add("South", printButton);
            f.pack();
            f.setVisible(true);
        
    }*/
    }
    J'ai rencontré deux soucis
    1-Si j'imprime le JFrame, j'ai des quatre marges de 5 cm... et je veux les éviter
    2- Je veux bien imprimer les JFrames sans l'entête (le title + réduire + restaurer +fermer)
    Merci
    On essaie
    et ça marchera

    Mon site
    Ma page

  2. #2
    Membre confirmé Avatar de javaNavCha
    Homme Profil pro
    EKG Group
    Inscrit en
    Juillet 2009
    Messages
    311
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : Tunisie

    Informations professionnelles :
    Activité : EKG Group
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2009
    Messages : 311
    Points : 631
    Points
    631
    Par défaut
    Pour annuler la barre en haut
    j'ai changer de stratégie:
    je vais imprimer le panneau
    je l'ai essayé et ça marche super bien!!!

    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
     
     
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridBagLayout;
    import java.awt.RenderingHints;
    import java.awt.print.PageFormat;
    import java.awt.print.Paper;
    import java.awt.print.Printable;
    import java.awt.print.PrinterException;
    import java.awt.print.PrinterJob;
    import java.util.Vector;
     
    import javax.swing.JPanel;
     
     
    /**
     * @author mep
     *
     */
    public class MPanelPrinter
        implements Printable
    {
     
        public MPanelPrinter(JPanel panel)
        {
            documentTitle = "";
            this.panel = panel;
            initPrintablePanel();
        }
     
        public void initPrintablePanel()
        {
            showPrintZone = false;
            fitIntoPage = false;
            wrapComponent = false;
            printJob = PrinterJob.getPrinterJob();
            pageFormat = printJob.defaultPage();
            pageFormat.setOrientation(1);
        }
     
        public void setOrientation(int orientation)
        {
            pageFormat.setOrientation(orientation);
        }
     
        public void setPrintZoneVisible(boolean status)
        {
            showPrintZone = status;
        }
     
        public void setWrapComponent(boolean status)
        {
            wrapComponent = status;
        }
     
        public void setFitIntoPage(boolean status)
        {
            fitIntoPage = status;
        }
     
        public int getPageWidth()
        {
            return (int)pageFormat.getImageableWidth();
        }
     
        public double getMarginTop ()
        {
            return pageFormat.getImageableY();
        }
     
        public double getMarginLeft ()
        {
            return pageFormat.getImageableX();
        }
     
        public void setLRMargins(int margin)
        {
            Paper paper = pageFormat.getPaper();
            paper.setImageableArea(paper.getImageableX() - (double)(margin / 2), paper.getImageableY(), paper.getImageableWidth() + (double)(margin / 2), paper.getImageableHeight());
            pageFormat.setPaper(paper);
        }
     
        public void setTBMargins(int margin)
        {
            Paper paper = pageFormat.getPaper();
            paper.setImageableArea(paper.getImageableX(), paper.getImageableY() - (double)(margin / 2), paper.getImageableWidth(), paper.getImageableHeight() + (double)(margin / 2));
            pageFormat.setPaper(paper);
        }
     
        public void setDocumentTitle(String title)
        {
            documentTitle = title;
        }
     
        public int print(Graphics g, PageFormat pf, int pageIndex)
            throws PrinterException
        {
            Dimension tailleDoc = panel.getSize();
            double hauteurDocu = tailleDoc.getHeight();
            double hauteurPage = pf.getImageableHeight();
            double largeurDocu = tailleDoc.getWidth();
            double largeurPage = pf.getImageableWidth();
            int totalNumPages = (int)Math.ceil(hauteurDocu / hauteurPage);
            if(wrapComponent)
                totalNumPages = taillePages.size();
            else
            if(fitIntoPage)
                totalNumPages = 1;
            double scaleX = largeurPage / largeurDocu;
            double scaleY = hauteurPage / hauteurDocu;
            if(pageIndex >= totalNumPages)
                return 1;
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g2d.translate(pf.getImageableX(), pf.getImageableY());
            if(fitIntoPage)
            {
                double ratio = Math.min(scaleX, scaleY);
                g2d.scale(ratio, ratio);
            } else
            if(wrapComponent)
            {
                if(pageIndex > 0)
                    g2d.translate(0.0D, -((Double)taillePages.get(pageIndex - 1)).doubleValue());
            } else
            {
                g2d.translate(0.0D, (double)(-pageIndex) * hauteurPage);
            }
            panel.paint(g2d);
            if(wrapComponent)
            {
                double hauteurBlanc = ((Double)taillePages.get(pageIndex)).doubleValue();
                g2d.setColor(Color.WHITE);
                g2d.fillRect(0, (int)hauteurBlanc, (int)largeurPage, (int)hauteurBlanc + (int)hauteurPage);
            }
            if(wrapComponent)
            {
                if(pageIndex > 0)
                    g2d.translate(0.0D, ((Double)taillePages.get(pageIndex - 1)).doubleValue());
            } else
            {
                g2d.translate(0.0D, (double)pageIndex * hauteurPage);
            }
            g2d.setColor(Color.BLACK);
            g2d.setFont(new Font("Verdanna", 2, 10));
            g2d.drawString(documentTitle + " - [" + (pageIndex + 1) + "/" + totalNumPages + "]", 0, (int)pf.getImageableHeight() - 20);
            return 0;
        }
     
        public void print()
        {
            printJob.setPrintable(this, pageFormat);
            try
            {
                if(printJob.printDialog())
                {
                    if(wrapComponent)
                        calculatePages();
                    Paper paper = pageFormat.getPaper();
                    Paper save = pageFormat.getPaper();
                    paper.setImageableArea(paper.getImageableX(), paper.getImageableY(), paper.getWidth() - paper.getImageableX(), paper.getHeight() - paper.getImageableY());
                    pageFormat.setPaper(paper);
                    printJob.setPrintable(this, pageFormat);
                    printJob.print();
                    pageFormat.setPaper(save);
                }
            }
            catch(PrinterException pe)
            {
                System.out.println("Erreur lors de l'impression du document: " + toString());
            }
        }
     
        private void calculatePages()
        {
            taillePages = new Vector();
            double hauteurPage = pageFormat.getImageableHeight();
            double hauteurTotal = 0.0D;
            double hauteurCumul = 0.0D;
            for(int i = 0; i < panel.getComponentCount(); i++)
            {
                int gridBagInsets = 0;
                if(panel.getLayout() instanceof GridBagLayout)
                    gridBagInsets = ((GridBagLayout)panel.getLayout()).getConstraints(panel.getComponent(i)).insets.bottom + ((GridBagLayout)panel.getLayout()).getConstraints(panel.getComponent(i)).insets.top;
                double hauteurComponent = panel.getComponent(i).getSize().getHeight() + (double)gridBagInsets;
                if(hauteurComponent > hauteurPage)
                {
                    wrapComponent = false;
                    return;
                }
                hauteurTotal += hauteurComponent;
                if(hauteurTotal > hauteurPage)
                {
                    hauteurTotal -= hauteurComponent;
                    hauteurCumul += hauteurTotal;
                    taillePages.add(new Double(hauteurCumul));
                    hauteurTotal = hauteurComponent;
                }
            }
     
            hauteurCumul += hauteurTotal;
            taillePages.add(new Double(hauteurCumul));
        }
     
        private JPanel panel;
        private boolean showPrintZone;
        private boolean fitIntoPage;
        private boolean wrapComponent;
        private PageFormat pageFormat;
        private PrinterJob printJob;
        private Vector taillePages;
        private String documentTitle;
        public static final int PORTRAIT = 1;
        public static final int LANDSCAPE = 0;
    }
    On essaie
    et ça marchera

    Mon site
    Ma page

  3. #3
    Membre du Club Avatar de arafat877
    Inscrit en
    Septembre 2010
    Messages
    46
    Détails du profil
    Informations forums :
    Inscription : Septembre 2010
    Messages : 46
    Points : 46
    Points
    46
    Par défaut Solution
    Salut !

    Je te propose d'utiliser un superbe logiciel qui s'appel iReport, il est basé sur netbeans, il te permet de dessiner ta facture/rapport puis de le faire appeler par java, afin d'y injecter tes données, il te permet même d'utiliser des Charts !

    Cordialement !

  4. #4
    Membre confirmé Avatar de javaNavCha
    Homme Profil pro
    EKG Group
    Inscrit en
    Juillet 2009
    Messages
    311
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : Tunisie

    Informations professionnelles :
    Activité : EKG Group
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2009
    Messages : 311
    Points : 631
    Points
    631
    Par défaut
    Merci arafat877
    je dois me former sur le ireport...
    On essaie
    et ça marchera

    Mon site
    Ma page

  5. #5
    Membre du Club Avatar de arafat877
    Inscrit en
    Septembre 2010
    Messages
    46
    Détails du profil
    Informations forums :
    Inscription : Septembre 2010
    Messages : 46
    Points : 46
    Points
    46
    Par défaut Solution
    Alors, si t'as besoin d'un eBook sur iReport, j'en ai un en Aglais, je peux te le donner en le publiant sur mon Blog : http://attractive-java.blogspot.com/

    Cordialement !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [CR10 ]Demande d'idée pour impression d'une facture
    Par aimer_Delphi dans le forum SAP Crystal Reports
    Réponses: 3
    Dernier message: 25/07/2015, 10h29
  2. Impression d'une facture
    Par Décibel dans le forum Mise en page CSS
    Réponses: 3
    Dernier message: 15/01/2015, 17h46
  3. Impression d'une facture avec Java EE
    Par marouanenet dans le forum Développement Web en Java
    Réponses: 3
    Dernier message: 03/01/2013, 15h25
  4. [VB 2008 Express] Impression d'une facture
    Par Cyborg289 dans le forum VB.NET
    Réponses: 3
    Dernier message: 19/02/2008, 13h28
  5. impression d'une facture à partir de TDBGrid
    Par blond1888 dans le forum Bases de données
    Réponses: 11
    Dernier message: 23/01/2007, 16h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo