Print Text as Right-Justified
Graphic
In this example, the printer is in Graphic mode and prints the drawn text on the right side of the page. The program performs the following steps:
...
1. Opens the connection to the printer.
2. Calculates the right side of the page using the page and font metrics.
3. Draws the text from the right side 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 TextRightGraphicMode {
public static void main(String[] args) {
float xRight=0, y=15;
String sText="This text is on the right.";
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();
PrinterFont pF = gP.getFont(sFonts[0]);
//----------------------(2)--------------------
xRight = gP.getPageMetrics().getWidth() - pF.getMetrics(sText).getWidth() - 10;
//----------------------(3)--------------------
gP.drawText(pF, xRight, y, sText, GraphicPrinter.NO_ROTATION);
gP.doPrint(1);
}
catch (Throwable error){
error.printStackTrace();
} finally {
//----------------------(4)--------------------
try {
gP.close();
} catch (Exception ex) {}
}
return;
}
}