请问JAVA如何实现打印及打印预览功能?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请问JAVA如何实现打印及打印预览功能?相关的知识,希望对你有一定的参考价值。
内容如题(此为桌面应用程序,非JSP)开发环境 JDK1.5 JBulider2006请给出详细源码,以及注释谢谢
package com.szallcom.tools;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import wf.common.SystemProperties;
public class PrintPreviewDialog extends JDialog implements ActionListener
private JButton nextButton = new JButton("Next");
private JButton previousButton = new JButton("Previous");
private JButton closeButton = new JButton("Close");
private JPanel buttonPanel = new JPanel();
private PreviewCanvas canvas;
public PrintPreviewDialog(Frame parent, String title, boolean modal,
PrintTest pt, String str)
super(parent, title, modal);
canvas = new PreviewCanvas(pt, str);
setLayout();
private void setLayout()
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(canvas, BorderLayout.CENTER);
nextButton.setMnemonic('N');
nextButton.addActionListener(this);
buttonPanel.add(nextButton);
previousButton.setMnemonic('N');
previousButton.addActionListener(this);
buttonPanel.add(previousButton);
closeButton.setMnemonic('N');
closeButton.addActionListener(this);
buttonPanel.add(closeButton);
this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
this.setBounds((int) ((SystemProperties.SCREEN_WIDTH - 400) / 2),
(int) ((SystemProperties.SCREEN_HEIGHT - 400) / 2), 400, 400);
public void actionPerformed(ActionEvent evt)
Object src = evt.getSource();
if (src == nextButton)
nextAction();
else if (src == previousButton)
previousAction();
else if (src == closeButton)
closeAction();
private void closeAction()
this.setVisible(false);
this.dispose();
private void nextAction()
canvas.viewPage(1);
private void previousAction()
canvas.viewPage(-1);
class PreviewCanvas extends JPanel
private String printStr;
private int currentPage = 0;
private PrintTest preview;
public PreviewCanvas(PrintTest pt, String str)
printStr = str;
preview = pt;
public void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
double xoff;
double yoff;
double scale;
double px = pf.getWidth();
double py = pf.getHeight();
double sx = getWidth() - 1;
double sy = getHeight() - 1;
if (px / py < sx / sy)
scale = sy / py;
xoff = 0.5 * (sx - scale * px);
yoff = 0;
else
scale = sx / px;
xoff = 0;
yoff = 0.5 * (sy - scale * py);
g2.translate((float) xoff, (float) yoff);
g2.scale((float) scale, (float) scale);
Rectangle2D page = new Rectangle2D.Double(0, 0, px, py);
g2.setPaint(Color.white);
g2.fill(page);
g2.setPaint(Color.black);
g2.draw(page);
try
preview.print(g2, pf, currentPage);
catch (PrinterException pe)
g2.draw(new Line2D.Double(0, 0, px, py));
g2.draw(new Line2D.Double(0, px, 0, py));
public void viewPage(int pos)
int newPage = currentPage + pos;
if (0 <= newPage && newPage < preview.getPagesCount(printStr))
currentPage = newPage;
repaint();
package wf.common;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
public final class SystemProperties
public static final double SCREEN_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
public static final double SCREEN_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
public static final String USER_DIR = System.getProperty("user.dir");
public static final String USER_HOME = System.getProperty("user.home");
public static final String USER_NAME = System.getProperty("user.name");
public static final String FILE_SEPARATOR = System.getProperty("file.separator");
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static final String PATH_SEPARATOR = System.getProperty("path.separator");
public static final String JAVA_HOME = System.getProperty("java.home");
public static final String JAVA_VENDOR = System.getProperty("java.vendor");
public static final String JAVA_VENDOR_URL = System.getProperty("java.vendor.url");
public static final String JAVA_VERSION = System.getProperty("java.version");
public static final String JAVA_CLASS_PATH = System.getProperty("java.class.path");
public static final String JAVA_CLASS_VERSION = System.getProperty("java.class.version");
public static final String OS_NAME = System.getProperty("os.name");
public static final String OS_ARCH = System.getProperty("os.arch");
public static final String OS_VERSION = System.getProperty("os.version");
public static final String[] FONT_NAME_LIST = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
public static final Font[] FONT_LIST = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
参考技术A 猪哥解答:
我这里有以前收藏的代码,两个类实现了简易的文本打印机的功能,包括预览。简单跟你说一下。
1、PrinterDemo.java主体类,也是入口类,里面有main方法可以直接在Eclipse中调试运行,他实现了从本地磁盘读取文本类文件打印以及打印预览的功能,其中File动作按钮中的PrintPreviw就是打印预览功能,你可以运行看看。
2、PrintPreview.java打印预览类,这是专门为预览打印设计的类,通过他的构造方法可以构造出一个预览类,PrinterDemo中的预览功能就是调用了这个类。
两个类放在同一个包下。
提交不上去,我把源码贴上来提交不了,字数也没有超标。你留一个邮箱,或者等我的文库上传的源码吧,我放到文库里,还在审批。
问题补充:档案已经上传到文库里了,地址是
DOC格式的:http://wenku.baidu.com/view/048ae0e8856a561252d36fab.html
PDF格式的:http://wenku.baidu.com/view/67c57a69011ca300a6c390fd.html
你下来看看吧。
Qt实现Qchart的打印和打印预览的几种方法
实现打印预览和打印,是挺常用的功能。把其他一些内容如QTextBrowser或者QEditText打印和打印预览是容易的,因为它们都自带了print方法,可以直接输出到printer。这里介绍下Qt实现Qchart的打印和打印预览的几种方法。
首选介绍下Qt如何实现打印预览功能。
打印预览的实现
使用Qt自带的QPrintPreviewDialog和QPrinter。
void MainWindow::PrintPreview()
QPrinter printer(QPrinter::HighResolution);
//自定义纸张大小,特别重要,不然预览效果极差
printer.setPageSize(QPrinter::Custom);
printer.setPaperSize(QSizeF(600, 800),QPrinter::Point);
QPrintPreviewDialog preview(&printer, this);// 创建打印预览对话框
preview.setMinimumSize(1000,600);
connect(&preview,SIGNAL(paintRequested(QPrinter*)),this,SLOT(Preview(QPrinter*) ) );
preview.exec(); //打印对话框显示,paintRequest触发
void MainWindow::Preview(QPrinter *printer)
ui->textBrowser->print(printer);
Qchart的打印
方法一,使用QTextDocument
打印html文件模板。html文件中带上图片路径:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>特立独行的猫哥</title>
</head>
<body>
<h1>我的第一个标题</h1>
<p>一个来自文件中的图像:</p>
<img src="./chart1.png" width="600" height="400">
</body>
</html>
void MainWindow::Preview(QPrinter *printer)
//ui->tb->print(printer);
QString fileContent = "";
QFile file1("./test.html"); //htmlfileName:本地的制作好的html文件
if (file1.open(QIODevice::ReadOnly))
QTextStream fs(&file1);
fs.setCodec("UTF-8"); //Qt 使用 UTF-8 编码读取文件,解决打印的文件中文乱码
fileContent = fs.readAll();
file1.close();
QTextDocument textDocument;
textDocument.setHtml(fileContent);
textDocument.print(printer);
方法二,使用QPainter
void MainWindow::Preview(QPrinter *printer)
//ui->tb->print(printer);
QPainter painter(printer);
QPixmap pix;
pix.load("./chart1.png");
// painter.drawPixmap(0,0,850,850,pix); //在(0,0)点起始的宽高均为50的句型中显示图片
qreal wid = pix.width(); //获取图像的宽高
qreal hei = pix.height();
pix = pix.scaled(wid*5,hei*5,Qt::KeepAspectRatio);//将图片宽高扩大两倍,且在矩形内保持宽高比值
painter.drawPixmap(0,0,pix);
保存Qchart为图片
//抓取qchart图片
QPixmap pix = ui->qchart2->grab();
QImage image = pix.toImage();
QString imgname = "_chart2.png";
imgname = PIC_PATH+imgname;
image.save(imgname);
引用
QT打印,打印预览_尔容又夏的博客-CSDN博客_qt打印预览
QT实现打印预览及生成Pdf功能_小MarkK的博客-CSDN博客_qt打印pdf
【QT5】QPixmap的使用_&Mr.Gong的博客-CSDN博客_qpixmap用法
C/C++ Qt QChart 绘图组件应用_LyShark 孤风洗剑的博客-CSDN博客
1.关于QT中的Graphics绘图,定时器,动画,将窗口中的内容打印到图片上,打印机,打印预览_to.to的博客-CSDN博客
以上是关于请问JAVA如何实现打印及打印预览功能?的主要内容,如果未能解决你的问题,请参考以下文章