当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 2 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : Ubuntu下批量打印出白纸,缺页。
帖子发表于 : 2008-07-24 15:56 

注册: 2008-07-24 15:48
帖子: 2
送出感谢: 0 次
接收感谢: 0 次
谁能告诉下,Ubuntu里面对打印队列是如何处理的,我做了一个批量打印html的java类,打印后出现 缺页,页面打印半个,打印白纸等情况。


页首
 用户资料  
 
2 楼 
 文章标题 : 这个是我的java类。
帖子发表于 : 2008-07-24 16:51 

注册: 2008-07-24 15:48
帖子: 2
送出感谢: 0 次
接收感谢: 0 次
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.IOException;
import java.net.URL;

import javax.swing.JEditorPane;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
import javax.swing.text.View;
import javax.swing.text.html.HTMLDocument;

public class Print1 implements Printable {

protected boolean next;
protected int currentPage = -1; // Used to keep track of when

protected JEditorPane jeditorPane; // Container to hold the


protected double pageEndY = 0; // Location of the current page
// end.

protected double pageStartY = 0; // Location of the current page
// start.

protected boolean scaleWidthToFit = true; // boolean to allow control over
// whether pages too wide to fit
// on a page will be scaled.

protected PageFormat pFormat;
protected PrinterJob pJob;

/*
* The constructor initializes the pFormat and PJob variables.
*/
public Print1() {
pFormat = new PageFormat();
pJob = PrinterJob.getPrinterJob();
}

/*
* Method to get the current Document
*/
public Document getDocument() {
if (jeditorPane != null)
return jeditorPane.getDocument();
else
return null;
}

/*
* Method to get the current choice the width scaling option.
*/
public boolean getScaleWidthToFit() {
return scaleWidthToFit;
}

/*
* pageDialog() displays a page setup dialog.
*/
public void pageDialog() {
pFormat = pJob.pageDialog(pFormat);
}


public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
double scale = 1.0;
Graphics2D graphics2D;
View rootView;
// I
graphics2D = (Graphics2D) graphics;
// II
jeditorPane.setSize((int) pageFormat.getImageableWidth(),
Integer.MAX_VALUE);
jeditorPane.validate();
// III
rootView = jeditorPane.getUI().getRootView(jeditorPane);
// IV
if ((scaleWidthToFit)
&& (jeditorPane.getMinimumSize().getWidth() > pageFormat
.getImageableWidth())) {
scale = pageFormat.getImageableWidth()
/ jeditorPane.getMinimumSize().getWidth();
graphics2D.scale(scale, scale);
}
// V
graphics2D.setClip((int) (pageFormat.getImageableX() / scale),
(int) (pageFormat.getImageableY() / scale), (int) (pageFormat
.getImageableWidth() / scale), (int) (pageFormat
.getImageableHeight() / scale));
// VI
if (pageIndex > currentPage) {
currentPage = pageIndex;
pageStartY += pageEndY;
pageEndY = graphics2D.getClipBounds().getHeight();
}
// VII
graphics2D.translate(graphics2D.getClipBounds().getX(), graphics2D
.getClipBounds().getY());
// VIII
Rectangle allocation = new Rectangle(0, (int) -pageStartY,
(int) (jeditorPane.getMinimumSize().getWidth()),
(int) (jeditorPane.getPreferredSize().getHeight()));
// X
if (printView(graphics2D, allocation, rootView)) {
return Printable.PAGE_EXISTS;
} else {
pageStartY = 0;
pageEndY = 0;
currentPage = -1;
return Printable.NO_SUCH_PAGE;
}
}


public void print(JEditorPane jedPane, boolean flag) {
setDocument(jedPane);
printDialog(flag);
}


protected void printDialog(boolean flag) {
if (flag) {
if (pJob.printDialog()) {
pJob.setPrintable(this, pFormat);
try {
pJob.print();
next = true;
} catch (PrinterException printerException) {
pageStartY = 0;
pageEndY = 0;
currentPage = -1;
System.out.println("Error Printing Document");
next = false;
}
}

} else {
if (next) {
try {
pJob.setPrintable(this, pFormat);
pJob.print();
} catch (PrinterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
pageStartY = 0;
pageEndY = 0;
currentPage = -1;
System.out.println("Error Printing Document");
}
}
}
}


protected boolean printView(Graphics2D graphics2D, Shape allocation,
View view) {
boolean pageExists = false;
Rectangle clipRectangle = graphics2D.getClipBounds();
Shape childAllocation;
View childView;

if (view.getViewCount() > 0
&& !view.getElement().getName().equalsIgnoreCase("td")) {
for (int i = 0; i < view.getViewCount(); i++) {
childAllocation = view.getChildAllocation(i, allocation);
if (childAllocation != null) {
childView = view.getView(i);
if (printView(graphics2D, childAllocation, childView)) {
pageExists = true;
}
}
}
} else {
// I
if (allocation.getBounds().getMaxY() >= clipRectangle.getY()) {
pageExists = true;
// II
if ((allocation.getBounds().getHeight() > clipRectangle
.getHeight())
&& (allocation.intersects(clipRectangle))) {
view.paint(graphics2D, allocation);
} else {
// III
if (allocation.getBounds().getY() >= clipRectangle.getY()) {
if (allocation.getBounds().getMaxY() <= clipRectangle
.getMaxY()) {
view.paint(graphics2D, allocation);
} else {
// IV
if (allocation.getBounds().getY() < pageEndY) {
pageEndY = allocation.getBounds().getY();
}
}
}
}
}
}
return pageExists;
}


protected void setContentType(String type) {
jeditorPane.setContentType(type);
}

/*
* Method to set an HTMLDocument as the Document to print.
*/
public void setDocument(HTMLDocument htmlDocument) {
jeditorPane = new JEditorPane();
setDocument("text/html", htmlDocument);
}

/*
* Method to set the Document to print as the one contained in a
* JEditorPane. This method is useful when Java does not provide direct
* access to a particular Document type, such as a Rich Text Format
* document. With this method such a document can be sent to the
* DocumentRenderer class enclosed in a JEditorPane.
*/
public void setDocument(JEditorPane jedPane) {
jeditorPane = new JEditorPane();
setDocument(jedPane.getContentType(), jedPane.getDocument());
}

/*
* Method to set a PlainDocument as the Document to print.
*/
public void setDocument(PlainDocument plainDocument) {
jeditorPane = new JEditorPane();
setDocument("text/plain", plainDocument);
}

/*
* Method to set the content type and document of the JEditorPane.
*/
protected void setDocument(String type, Document document) {
setContentType(type);
jeditorPane.setDocument(document);
}

/*
* Method to set the current choice of the width scaling option.
*/
public void setScaleWidthToFit(boolean scaleWidth) {
scaleWidthToFit = scaleWidth;
}

public void printPage(String[] path) {
int j = 0;
Print1 docr = new Print1();

for (int i = 0; i < path.length; i++) {
JEditorPane jedPane = new JEditorPane();
try {
System.out.println(path[i]);
URL url = new URL(path[i]);
jedPane.setPage(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
continue;
}
if (j == 0)
docr.print(jedPane, true);
else {
docr.print(jedPane, false);
}
j++;
}
}

public static void main(String args[]) {
Print1 p = new Print1();
String path[] = {"",""};
p.printPage(path);
}
}


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 2 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 4 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译