Hussein
Hussein

Reputation: 681

Print JScrollPane content to pdf

I have found this code which transfer a JPanel to an image and then print it to PDF file but since I have that my JPanel inside a JScrollPane and does not contain any layout or dimensions because it depends on the JScrollPane layout so I can't print it. the error says can't print a picture with height 0 and width 0. When I try to pass the JScrollPane itself I get just the frame of the JScrollPane printed only without the content JPanel.

public void printToPDF(java.awt.Image awtImage, String fileName) {
    try {
        Document d = new Document();
        PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream(
                fileName));
        d.open();


        Image iTextImage = Image.getInstance(writer, awtImage, 1);
        //iTextImage.setAbsolutePosition(100,300);
        iTextImage.scalePercent(100);
        d.add(iTextImage);

        d.close();

    } catch (Exception e) {
        e.printStackTrace();
    }   
}

public static java.awt.Image getImageFromPanel(Component component) {

    BufferedImage image = new BufferedImage(component.getWidth(),
            component.getHeight(), BufferedImage.TYPE_INT_RGB);
    component.paint(image.getGraphics());
    return image;
}

and Here is the main method:

         JScrollPane scrollPane = new JScrollPane();
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setBounds(0, 0, 600, 510);
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    getContentPane().add(scrollPane);

    GridBagConstraints c = new GridBagConstraints();
    JPanel borderpanel = new JPanel();
    scrollPane.setViewportView(borderpanel);

    Color veryLight = new Color(239,230,230);
    JPanel container = new JPanel();
    borderpanel.add(container, BorderLayout.CENTER);
    container.setLayout(new GridLayout(0,1,0,1));
    container.setBackground(Color.WHITE);

    JPanel test = new JPanel();
    test.setLayout(new GridLayout(0,1,0,1));
    container.add(test, BorderLayout.CENTER);

        JPanel rowPanel = new JPanel();
        rowPanel.setPreferredSize(new Dimension(580,50));
        test.add(rowPanel, BorderLayout.WEST);
        rowPanel.setLayout(null);

        JLabel auctionTitle = new JLabel("Auction Title");
        auctionTitle.setForeground(Color.WHITE);
        auctionTitle.setFont(new Font("Tahoma", Font.PLAIN, 14));
        auctionTitle.setBounds(102, 0, 82, 20);
        rowPanel.add(auctionTitle); 
        rowPanel.setBackground(SystemColor.GRAY);

        JTextArea textArea = new JTextArea();
        textArea.setBackground(veryLight);
        textArea.setBounds(191, 0, 386, 50);
        rowPanel.add(textArea);


        final java.awt.Image image = getImageFromPanel(borderpanel);

        String fileName = "C:\\Users\\Test\\Desktop\\newfile.pdf";
        printToPDF(image, fileName);

I want to print borderpanel

Upvotes: 0

Views: 815

Answers (1)

rdonuk
rdonuk

Reputation: 3956

Use java.awt.Robot to get the image of the panel:

public static java.awt.Image getImageFromPanel(Component component) {

    Point p = new Point(0, 0);
    SwingUtilities.convertPointToScreen(p, component);

    int width = component.getSize().width;
    int height = component.getSize().height;
    Rectangle screenBounds = new Rectangle(p.x, p.y, width, height);

    Robot robot = new Robot();
    BufferedImage image = robot.createScreenCapture(screenBounds);

    component.getGraphics().drawImage(image, 0, 0, component);

    return image;
} 

Upvotes: 1

Related Questions