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
| public class DocumentPrinter implements Printable, Pageable{
protected int currentPage = -1;
protected JTextPane jTextPane;
protected double pageEndY = 0;
protected double pageStartY = 0;
protected PageFormat pageFormat;
protected PrinterJob printJob;
private int numPages = 13;
public DocumentPrinter() {
pageFormat = new PageFormat();
printJob = PrinterJob.getPrinterJob();
}
public void pageDialog() {
pageFormat = printJob.pageDialog(pageFormat);
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
double scale = 2;
Graphics2D graphics2D;
View rootView;
graphics2D = (Graphics2D) graphics;
jTextPane.setSize((int) pageFormat.getImageableWidth(), Integer.MAX_VALUE);
jTextPane.validate();
rootView = jTextPane.getUI().getRootView(jTextPane);
graphics2D.setClip((int) (pageFormat.getImageableX() / scale),
(int) (pageFormat.getImageableY() / scale),
(int) (pageFormat.getImageableWidth() / scale),
(int) (pageFormat.getImageableHeight() / scale));
if (pageIndex > currentPage) {
currentPage = pageIndex;
pageStartY += pageEndY;
pageEndY = graphics2D.getClipBounds().getHeight();
}
graphics2D.translate(graphics2D.getClipBounds().getX(),
graphics2D.getClipBounds().getY());
Rectangle allocation = new Rectangle(0,
(int) -pageStartY,
(int) (jTextPane.getMinimumSize().getWidth()),
(int) (jTextPane.getPreferredSize().getHeight()));
if (printView(graphics2D, allocation, rootView)) {
return Printable.PAGE_EXISTS;
} else {
pageStartY = 0;
pageEndY = 0;
currentPage = -1;
return Printable.NO_SUCH_PAGE;
}
}
public void print(JTextPane jtextPane) {
setDocument(jtextPane);
printDialog();
}
protected void printDialog() {
printJob.setPrintable(this, pageFormat);
printJob.setPageable(this);
if (printJob.printDialog()) {
try {
printJob.print();
} catch (PrinterException printerException) {
pageStartY = 0;
pageEndY = 0;
currentPage = -1;
System.out.println("Error Printing Document");
printerException.printStackTrace();
}
}
}
protected boolean printView(Graphics2D graphics2D, Shape allocation,
View view) {
boolean pageExists = false;
Rectangle clipRectangle = graphics2D.getClipBounds();
Shape childAllocation;
View childView;
if (view.getViewCount() > 0) {
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 {
if (allocation.getBounds().getMaxY() >= clipRectangle.getY()) {
pageExists = true;
if ((allocation.getBounds().getHeight() > clipRectangle.getHeight()) &&
(allocation.intersects(clipRectangle))) {
view.paint(graphics2D, allocation);
} else {
if (allocation.getBounds().getY() >= clipRectangle.getY()) {
if (allocation.getBounds().getMaxY() <= clipRectangle.getMaxY()) {
view.paint(graphics2D, allocation);
} else {
if (allocation.getBounds().getY() < pageEndY) {
pageEndY = allocation.getBounds().getY();
}
}
}
}
}
}
return pageExists;
}
protected void setContentType(String type) {
jTextPane.setContentType(type);
}
public void setDocument(JTextPane jtextPane) {
jTextPane = new JTextPane();
setDocument(jtextPane.getContentType(), jtextPane.getDocument());
}
protected void setDocument(String type, Document document) {
setContentType(type);
jTextPane.setDocument(document);
}
public int getNumberOfPages() {
return numPages;//TODO
}
public PageFormat getPageFormat(int pageIndex)
throws IndexOutOfBoundsException {
return pageFormat;
}
public Printable getPrintable(int pageIndex)
throws IndexOutOfBoundsException {
return this;
}
} |
Partager