-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrintSimpleText.java
More file actions
executable file
·89 lines (79 loc) · 3.35 KB
/
PrintSimpleText.java
File metadata and controls
executable file
·89 lines (79 loc) · 3.35 KB
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
/*
* PrintSimpleText.java
*
* Created on August 1, 2006, 2:27 PM
*/
/**
* @author Chen-Fu Liao
* Sr. Systems Engineer
* ITS Institute, ITS Laboratory
* Center For Transportation Studies
* University of Minnesota
* 200 Transportation and Safety Building
* 511 Washington Ave. SE
* Minneapolis, MN 55455
*/
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.awt.Graphics2D ;
import java.text.*;
public class PrintSimpleText implements Printable {
private AttributedString mStyledText ;
/** Creates a new instance of PrintSimpleText */
public PrintSimpleText(String myText) {
mStyledText = new AttributedString(myText);
/* Get the representation of the current printer and
* the current print job.
*/
PrinterJob printerJob = PrinterJob.getPrinterJob();
/* Build a book containing pairs of page painters (Printables)
* and PageFormats. This example has a single page containing
* text.
*/
Book book = new Book();
book.append(this, new PageFormat());
/* Set the object to be printed (the Book) into the PrinterJob.
* Doing this before bringing up the print dialog allows the
* print dialog to correctly display the page range to be printed
* and to dissallow any print settings not appropriate for the
* pages to be printed.
*/
printerJob.setPageable(book);
/* Show the print dialog to the user. This is an optional step
* and need not be done if the application wants to perform
* 'quiet' printing. If the user cancels the print dialog then false
* is returned. If true is returned we go ahead and print.
*/
boolean doPrint = printerJob.printDialog();
if (doPrint) {
try {
printerJob.print();
} catch (PrinterException exception) {
System.err.println("Printing error: " + exception);
}
} // if
}
public int print(java.awt.Graphics g, java.awt.print.PageFormat format, int pageIndex) throws java.awt.print.PrinterException {
Graphics2D g2d = (Graphics2D) g;
// Move the origin from the corner of the Paper to the corner of the imageable area.
g2d.translate(format.getImageableX(), format.getImageableY());
// Set the text color.
g2d.setPaint(Color.black);
//g2d.drawString(mStyledText.toString(), 0,0) ;
// Use a LineBreakMeasurer instance to break our text into lines that fit the imageable area of the page.
Point2D.Float pen = new Point2D.Float();
AttributedCharacterIterator charIterator = mStyledText.getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, g2d.getFontRenderContext());
float wrappingWidth = (float) format.getImageableWidth();
while (measurer.getPosition() < charIterator.getEndIndex()) {
TextLayout layout = measurer.nextLayout(wrappingWidth);
pen.y += layout.getAscent();
float dx = layout.isLeftToRight()? 0 : (wrappingWidth - layout.getAdvance());
layout.draw(g2d, pen.x + dx, pen.y);
pen.y += layout.getDescent() + layout.getLeading() +1;
}
return Printable.PAGE_EXISTS;
}
}