实现了一个简单的签字功能,同时支持将签字图像保存为PNG格式和将签字添加到PDF文档中。在点击“签字”按钮后,会弹出一个窗口,用户可以在其中绘制签名,绘制完成后可选择保存为PNG图片或将签名添加到指定的PDF文档中。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
public class SignatureDemo {
public static void main(String[] args) {
SignatureFrame frame = new SignatureFrame();
frame.setVisible(true);
}
}
class SignatureFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPanel;
public SignatureFrame() {
setTitle("签字");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
contentPanel = new JPanel();
setContentPane(contentPanel);
// 添加按钮,用于触发签名事件
JButton button = new JButton("签字");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 显示签名面板
SignaturePanel panel = new SignaturePanel();
int result = JOptionPane.showConfirmDialog(contentPanel, panel, "签字", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
// 获取签名图像并保存到本地
saveSignatureImage(panel.getSignatureImage());
}
}
});
contentPanel.add(button);
}
/**
* 保存签名图像到本地
*/
private void saveSignatureImage(BufferedImage image) {
// 打开文件选择器
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showSaveDialog(contentPanel);
if (result == JFileChooser.APPROVE_OPTION) {
// 获取用户选择的文件路径
File file = fileChooser.getSelectedFile();
// 将图像保存为PNG格式
try (OutputStream out = new FileOutputStream(file)) {
ImageIO.write(image, "PNG", out);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class SignaturePanel extends JPanel {
private static final long serialVersionUID = 1L;
private BufferedImage signatureImage;
public SignaturePanel() {
setPreferredSize(new Dimension(400, 300));
setBackground(Color.WHITE);
setBorder(BorderFactory.createLineBorder(Color.BLACK));
// 添加鼠标监听器,用于绘制签名
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
// 创建新的图像,用于绘制签名
signatureImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = signatureImage.createGraphics();
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(2));
g.drawLine(e.getX(), e.getY(), e.getX(), e.getY());
g.dispose();
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
// 绘制签名
Graphics2D g = signatureImage.createGraphics();
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(2));
g.drawLine(e.getX(), e.getY(), e.getX(), e.getY());
g.dispose();
repaint();
}
});
}
public BufferedImage getSignatureImage() {
return signatureImage;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (signatureImage != null) {
g.drawImage(signatureImage, 0, 0, null);
}
}
}
class PDFUtils {
/**
* 在PDF文档中添加签名
*/
public static void addSignatureToPDF(File pdfFile, BufferedImage signatureImage) throws IOException, DocumentException {
Rectangle pageSize = new Rectangle(PageSize.A4);
Document document = new Document(pageSize);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFile.getAbsolutePath() + ".signed.pdf"));
document.open();
PdfContentByte canvas = writer.getDirectContent();
// 将签名图像添加到PDF文档中
Image image = Image.getInstance(signatureImage, null);
image.scaleToFit(100, 50);
float x = (document.right() - image.getScaledWidth()) / 2;
float y = (document.top() - image.getScaledHeight()) / 2;
canvas.addImage(image, image.getScaledWidth(), 0, 0, image.getScaledHeight(), x, y);
document.close();
writer.close();
}
}