实现了一个简单的签字功能,同时支持将签字图像保存为PNG格式和将签字添加到PDF文档中。在点击“签字”按钮后,会弹出一个窗口,用户可以在其中绘制签名,绘制完成后可选择保存为PNG图片或将签名添加到指定的PDF文档中。
完整代码
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 | 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(); } } |
方法补充
除了上文的方法,我们还可以使用JavaFX或者Swing来实现签字功能,下面是实现方法,希望对大家有所帮助
JavaFX中可以通过使用Canvas实现签字功能。
步骤如下:
1. 创建一个JavaFX应用程序。
2. 创建一个Canvas对象。
3. 重写鼠标事件的方法。
4. 在鼠标移动时,通过Canvas的GraphicsContext对象绘制线条。
代码示例:
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 | public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ // 创建画布 Canvas canvas = new Canvas( 500 , 500 ); GraphicsContext gc = canvas.getGraphicsContext2D(); // 设置画笔的属性 gc.setLineWidth( 2 ); gc.setStroke(Color.BLACK); // 重写鼠标事件的方法 canvas.setOnMouseDragged(e -> { double x = e.getX(); double y = e.getY(); gc.lineTo(x, y); // 绘制线条 gc.stroke(); // 描边 }); // 创建主场景 Scene scene = new Scene( new Group(canvas), 500 , 500 ); // 显示窗口 primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } |
Swing中可以通过使用JPanel实现签字功能。
步骤如下:
1. 创建一个JFrame窗口。
2. 创建一个JPanel对象。
3. 重写鼠标事件的方法。
4. 在鼠标移动时,通过JPanel的Graphics对象绘制线条。
代码示例:
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 | public class Main extends JFrame { public Main() { // 创建面板 JPanel panel = new JPanel(); // 设置面板的大小和背景色 panel.setPreferredSize( new Dimension( 500 , 500 )); panel.setBackground(Color.WHITE); // 重写鼠标事件的方法 panel.addMouseMotionListener( new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { Graphics g = panel.getGraphics(); // 设置画笔的属性 ((Graphics2D)g).setStroke( new BasicStroke( 2 )); g.setColor(Color.BLACK); // 绘制线条 g.drawLine(e.getX(), e.getY(), e.getX(), e.getY()); } }); // 添加面板到窗口中 getContentPane().add(panel); // 设置窗口的属性 setTitle( "签字功能" ); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize( 500 , 500 ); setLocationRelativeTo( null ); setResizable( false ); setVisible( true ); } public static void main(String[] args) { new Main(); } } |
到此这篇关于使用Java实现签字功能的示例代码的文章就介绍到这了,更多相关Java签字内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!