java swing 显示pdf,带放大缩小及打印功能

本文展示了如何使用Java Swing实现PDF的显示,包括放大、缩小功能,并集成打印功能。主要通过CustomImgPanel类加载转换为image的PDF页面,并在ShowJFrame中进行展示和操作控制。
摘要由CSDN通过智能技术生成

最近项目中要用到java swing 显示pdf,带放大缩小及打印功能,经过一天的网络搜索及api查看终于完成,下公布代码如下

CustomImgPanel.java用来加载pdf转换后的image

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.merchantrunglobal.image.panel;


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;


/**
 *
 * @author admin
 */
public class CustomImgPanel extends javax.swing.JPanel {


    private int width = 0;
    private int height = 0;
    Image image;


    /**
     *
     * @param _width 整型,窗口的宽度
     * @param _height 整型,窗口的高度
     * @param _imgPath 图片的URL,可用相对路径
     */
    public CustomImgPanel(int _width, int _height, Image _image) {
        width = _width;
        height = _height;
        image = _image;
        setSize(width, height);
        setVisible(true);
    }


    /**
     *
     * @param _width 浮点型,窗口的宽度
     * @param _height 浮点型,窗口的高度
     * @param _imgPath 字符串,图片的URL,可用相对
     */
    public CustomImgPanel(double _width, double _height, Image _image) {
        width = (int) _width;
        height = (int) _height;
        image = _image;
        setSize(width, height);
        setVisible(true);
    }


    @Override
    public void paintComponent(Graphics gs) {
        Graphics2D g = (Graphics2D) gs;
        super.paintComponent(g);
        g.drawImage(image, 0, 0, width, height, this);
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>                        
    // Variables declaration - do not modify                     
    // End of variables declaration                   
}



ShowJFrame.java主运行类,用来加载滚动条,放大缩小及打印。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.merchantrunglobal.image.panel;


import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
imp

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值