Print Text as Centered Graphic
In this example, the printer is in Graphic mode and prints drawn text in the center of the page with. The program performs the following steps:
...
1. Opens the connection to the printer.
2. Calculates the center of the page using the page and font metrics.
3. Draws the text on the center of the page.
4. Closes the connection to the printer.
package test;
import com.sap.ip.me.api.pios.connection.*;
import com.sap.ip.me.api.pios.printer.*;
public class TextCenterGraphicMode {
public static void main(String[] args) {
float xCenter=0, yCenter=0;
String sText="This text is on the center.";
GraphicPrinter gP = null;
try{
Connector conn = Connector.getInstance();
DriverInfo[] driverInfo = conn.listDrivers(ConnectionType.PRINTER);
PrinterParameters params = new PrinterParameters(driverInfo[0]);
params.setPrinterMode(PrinterParameters.GRAPHIC_MODE);
//----------------------(1)--------------------
gP = (GraphicPrinter) conn.open(params);
String[] sFonts = gP.getFontConfigurationManager().listFontNames();
//----------------------(2)--------------------
PrinterFont pF = gP.getFont(sFonts[0]);
yCenter = (gP.getPageMetrics().getHeight()/2) - (pF.getMetrics(sText).getHeight()/2);
Metrics metrics = pF.getMetrics(sText);
float pageWidth = gP.getPageMetrics().getWidth();
xCenter = (pageWidth/2) - (metrics.getWidth()/2);
//----------------------(3)--------------------
gP.drawText(pF, xCenter, yCenter, sText, GraphicPrinter.NO_ROTATION);
gP.doPrint(1);
}
catch (Throwable error){
error.printStackTrace();
} finally {
//----------------------(4)--------------------
try {
gP.close();
} catch (Exception ex) {}
}
return;
}
}