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

Documents Java Discussion :

impression PDF d'un Jpanel


Sujet :

Documents Java

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    142
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Mai 2005
    Messages : 142
    Points : 93
    Points
    93
    Par défaut impression PDF d'un Jpanel
    Bonjour tout le monde.

    J'ai une question qui a deja été abordée de diverses manières sur ce forum mais je n'ai pas trouvé ma réponse.
    J'ai un dessin fait dans un JPanel et j'aimerais en faire un PDF. Mon idée était de le créer avec une imprimante.
    J'ai trouvé ce bout de code :

    Code : 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
     
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Shape;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.font.FontRenderContext;
    import java.awt.font.TextLayout;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.GeneralPath;
    import java.awt.geom.Line2D;
    import java.awt.geom.Point2D;
    import java.awt.geom.Rectangle2D;
    import java.awt.print.PageFormat;
    import java.awt.print.Printable;
    import java.awt.print.PrinterException;
    import java.awt.print.PrinterJob;
     
    import javax.print.attribute.HashPrintRequestAttributeSet;
    import javax.print.attribute.PrintRequestAttributeSet;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
     
    /**
    * This program demonstrates how to print 2D graphics
    */
    public class EssaiImprimer {
    public static void main(String[] args) {
    JFrame frame = new PrintTestFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
    }
    }
     
    /**
    * This frame shows a panel with 2D graphics and buttons to print the graphics
    * and to set up the page format.
    */
     
    class PrintTestFrame extends JFrame {
    public PrintTestFrame() {
    setTitle("PrintTest");
    setSize(WIDTH, HEIGHT);
     
    Container contentPane = getContentPane();
    canvas = new PrintPanel();
    contentPane.add(canvas, BorderLayout.CENTER);
     
    attributes = new HashPrintRequestAttributeSet();
     
    JPanel buttonPanel = new JPanel();
    JButton printButton = new JButton("Print");
    buttonPanel.add(printButton);
     
    printButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    try {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(canvas);
    if (job.printDialog(attributes)) {
    job.print(attributes);
    }
    } catch (PrinterException exception) {
    JOptionPane.showMessageDialog(PrintTestFrame.this,
    exception);
    }
    }
    });
     
    JButton pageSetupButton = new JButton("Page setup");
    buttonPanel.add(pageSetupButton);
    pageSetupButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.pageDialog(attributes);
    }
    });
     
    contentPane.add(buttonPanel, BorderLayout.NORTH);
    }
     
    private PrintPanel canvas;
     
    private PrintRequestAttributeSet attributes;
     
    private static final int WIDTH = 300;
     
    private static final int HEIGHT = 300;
    }
     
    /**
    * This panel generates a 2D graphics image for screen display and printing.
    */
     
    class PrintPanel extends JPanel implements Printable {
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    drawPage(g2);
    }
     
    public int print(Graphics g, PageFormat pf, int page)
    throws PrinterException {
    if (page >= 1)
    return Printable.NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    g2.translate(pf.getImageableX(), pf.getImageableY());
    g2.draw(new Rectangle2D.Double(0, 0, pf.getImageableWidth(), pf
    .getImageableHeight()));
     
    drawPage(g2);
    return Printable.PAGE_EXISTS;
    }
     
    /**
    * This method draws the page both on the screen and the printer graphics
    * context.
    * 
    * @param g2
    * the graphics context
    */
    public void drawPage(Graphics2D g2) {
    FontRenderContext context = g2.getFontRenderContext();
    Font f = new Font("Serif", Font.PLAIN, 72);
    GeneralPath clipShape = new GeneralPath();
     
    TextLayout layout = new TextLayout("Hello", f, context);
    AffineTransform transform = AffineTransform.getTranslateInstance(0, 72);
    Shape outline = layout.getOutline(transform);
    clipShape.append(outline, false);
     
    layout = new TextLayout("World", f, context);
    transform = AffineTransform.getTranslateInstance(0, 144);
    outline = layout.getOutline(transform);
    clipShape.append(outline, false);
     
    g2.draw(clipShape);
    g2.clip(clipShape);
     
    final int NLINES = 50;
    Point2D p = new Point2D.Double(0, 0);
    for (int i = 0; i < NLINES; i++) {
    double x = (2 * getWidth() * i) / NLINES;
    double y = (2 * getHeight() * (NLINES - 1 - i)) / NLINES;
    Point2D q = new Point2D.Double(x, y);
    g2.draw(new Line2D.Double(p, q));
    }
    }
    }
    Ca fonctionne très bien mais je ne voudrais pas avoir de boite de dialogue pour choisir l'imprimante et les autres propriétés.
    D'après ce que j'ai compris, c'est la variable attributes qui gére les propriétés.
    Quelqu'un sait il si je peux les modifier directement sans passer par une boite de dialogue. Le but est que l'utilisateur n'ai pas à intervenir.
    Merci

  2. #2
    Membre averti Avatar de Razgriz
    Profil pro
    Professeur / chercheur en informatique / mathématiques
    Inscrit en
    Avril 2006
    Messages
    391
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : Professeur / chercheur en informatique / mathématiques

    Informations forums :
    Inscription : Avril 2006
    Messages : 391
    Points : 306
    Points
    306
    Par défaut
    Pour ton problème je connais une api très bien faite, personnelement j'ai juste fait quelques tests basiques, tu devrais trouver ton bonheur sur ce lien :
    http://www.jpedal.org/.
    On a toujours besoin d'un plus bourrin que soi

    Oui il y a quelques bugs dans ma librairie de Sécurité, mais les classes postées ne sont pas celles de la dernière version, et j'ai la flemme de tout modifier. Je vous donnerai avec plaisir la dernière version du jar par mp.

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    142
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Mai 2005
    Messages : 142
    Points : 93
    Points
    93
    Par défaut
    Désolée de ne pas avoir repondu avant, j'étais sur un autre projet. J'ai résolu mon problème en utilisant la bibliothèque itext et ca marche bien.
    Merci pour ton aide

  4. #4
    Membre averti Avatar de Razgriz
    Profil pro
    Professeur / chercheur en informatique / mathématiques
    Inscrit en
    Avril 2006
    Messages
    391
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : Professeur / chercheur en informatique / mathématiques

    Informations forums :
    Inscription : Avril 2006
    Messages : 391
    Points : 306
    Points
    306
    Par défaut
    De rien, perso j'ai fait ça une fois et cette librairie et son api m'ont donné la solution, mais c'était y a longtemps alors je me souvenais plus bien, j'avais juste le lien
    On a toujours besoin d'un plus bourrin que soi

    Oui il y a quelques bugs dans ma librairie de Sécurité, mais les classes postées ne sont pas celles de la dernière version, et j'ai la flemme de tout modifier. Je vous donnerai avec plaisir la dernière version du jar par mp.

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

Discussions similaires

  1. [Pdf] Impression pdf
    Par Vrylx dans le forum Documents
    Réponses: 6
    Dernier message: 20/04/2007, 19h59
  2. Impression PDF
    Par the java lover dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 28/04/2006, 15h40
  3. impression pdf via asp
    Par flo456 dans le forum ASP
    Réponses: 2
    Dernier message: 16/01/2006, 14h58
  4. Impression PDF sans soft
    Par gnoc dans le forum XML/XSL et SOAP
    Réponses: 2
    Dernier message: 31/05/2005, 14h33
  5. Impression PDF et imprimante par défaut
    Par bobbafet dans le forum C++Builder
    Réponses: 2
    Dernier message: 03/11/2004, 21h33

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