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 131 132
|
public class PrinterManager implements Printable
{
final int POINTS_PER_INCH = 72;
private PrinterJob _printerJob;
private PrintService[] _printServices;
private PageFormat _pageFormat;
private Paper _paper;
private PrintRequestAttributeSet _printAttributeSet;
private TextLayout _txtLayout;
public static String textToPrint = new String();
public PrinterManager( String textToPrintParam )
{
if( textToPrintParam.equals("") )
PrinterManager.textToPrint = "Manipulating raw fonts would be too complicated to render paragraphs of "
+"text. Trying to write an algorithm to fully justify text using "
+"proportional fonts is not trivial. Adding support for international "
+"characters adds to the complexity. That's why we will use the "
+"TextLayout and the LineBreakMeasurer class to "
+"render text.";
else
PrinterManager.textToPrint = textToPrintParam;
_printerJob = PrinterJob.getPrinterJob();
_printServices = PrinterJob.lookupPrintServices();
for( int k = 0 ; k < _printServices.length ; k++ )
System.out.println("Found printer : " + _printServices[k].getName() );
_printAttributeSet = new HashPrintRequestAttributeSet();
_printAttributeSet.add( new Copies(1) );
_printAttributeSet.add( new JobName("My job", null ) );
_printAttributeSet.add( OrientationRequested.PORTRAIT );
_pageFormat =_printerJob.defaultPage();
_paper=_pageFormat.getPaper();
_paper.setSize( 204, 13106);
_paper.setImageableArea( 35, 72, 134, 12962);
_pageFormat.setPaper(_paper);
_printerJob.setPrintable(this, _pageFormat);
if ( _printServices.length > 0)
{
System.out.println("------------------- selected printer : ------------------");
System.out.println("selected printer " + _printServices[0].getName() );
try {
_printerJob.setPrintService( _printServices[0] );
_printerJob.print(_printAttributeSet);
}
catch (PrinterException pe) {
System.err.println(pe);
}
}
}
@Override
public int print( Graphics graphik, PageFormat pageFormat, int pageIndex) throws PrinterException
{
if ( pageIndex == 0 )
{
double pageX = pageFormat.getImageableX();
double pageY = pageFormat.getImageableY();
double pageWidth = pageFormat.getWidth();
double pageHeight = pageFormat.getHeight();
Graphics2D g2d = ( Graphics2D ) graphik;
g2d.translate( pageX, pageY );
g2d.setColor(Color.black);
Point2D.Double pen = new Point2D.Double (0, 0);
double width = pageWidth - 70;
AttributedString paragraphText = new AttributedString ( PrinterManager.textToPrint );
paragraphText.addAttribute ( TextAttribute.FONT, new Font ("serif", Font.PLAIN, 10) );
LineBreakMeasurer lineBreaker = new LineBreakMeasurer ( paragraphText.getIterator(),
new FontRenderContext (null, true, true) );
while ( ( _txtLayout = lineBreaker.nextLayout( (float) width ) ) != null )
{
//--- Align the Y pen to the ascend of the font, remember that
//--- the ascend is origin (0, 0) of a font. Refer to Figure 1
pen.y += _txtLayout.getAscent ();
//--- Draw the line of text
_txtLayout.draw ( g2d, (float) pen.x, (float) pen.y );
//--- Move the pen to the next position adding the descent and
//--- the leading of the font
pen.y += _txtLayout.getDescent () + _txtLayout.getLeading ();
}
//Rectangle2D outline = new Rectangle2D.Double( pageX, pageY, pageWidth, 300);
//g2d.setPaint(Color.black);
//g2d.draw(outline);
return Printable.PAGE_EXISTS;
}
else
{
return Printable.NO_SUCH_PAGE;
}
}
} |
Partager