用java实现打印功能 怎么写 求大神求源码PrintService[] services = PrinterJob.lookupPrintServices();//查找系统中所有java打印功能源代码的可用打印服务
PrintService service = ServiceUI.printDialog(null, 500, 500, services, services[0], null, new HashPrintRequestAttributeSet());//创建一个默认java打印功能源代码的打印机选择对话框
DocPrintJob job = service.createPrintJob();//创建一个打印任务
Doc doc = new SimpleDoc(new FileInputStream(new File("D:\\image\\MM.jpg")), DocFlavor.INPUT_STREAM.JPEG, null);//创建一个打印的文档
job.print(doc, null);//打印
这是jdk提供的打印接口java打印功能源代码,你也可以找找其它的第三方类库,jdk提供打印接口功能一般般
谁有java打印的源代码//参考下吧
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.util.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.swing.*;
/**
This program demonstrates how to print 2D graphics
*/
public class PrintTest
{
public static void main(String[] args)
{
JFrame frame = new PrintTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
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(DEFAULT_WIDTH, DEFAULT_HEIGHT);
canvas = new PrintPanel();
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 e)
{
JOptionPane.showMessageDialog(PrintTestFrame.this, e);
}
}
});
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);
}
});
add(buttonPanel, BorderLayout.NORTH);
}
private PrintPanel canvas;
private PrintRequestAttributeSet attributes;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_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();
推荐阅读
- 抖音无人直播违规,抖音无人直播违规了会封号吗
- c语言编程有一个函数分别用if,c语言if函数程序举例
- 微信小程序开发书有用么,微信小程序程序开发
- md中文角色扮演游戏,md经典角色扮演游戏
- php传输数据到html php传参
- 关于jdk和.net库的信息
- 苹果电脑excel怎么用,苹果电脑excel怎么用渐变填充
- 锅巴直播录屏,锅巴直播录屏怎么录
- 如何用Go语言打出 go语言输出hello word