Skip to content

Instantly share code, notes, and snippets.

@RaffaeleSgarro
Created October 30, 2013 09:30
Show Gist options
  • Select an option

  • Save RaffaeleSgarro/7229649 to your computer and use it in GitHub Desktop.

Select an option

Save RaffaeleSgarro/7229649 to your computer and use it in GitHub Desktop.
public class ArtifactsWhenUsingDialogFont {
public static void main(String... args) throws Exception {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(PrintServiceLookup.lookupDefaultPrintService());
job.setPrintable(new Printable(){
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
graphics.translate( (int) pageFormat.getImageableX(), (int) pageFormat.getImageableY() );
final String target = "OOOOOOOOOO\u2014MMMMMMMMM\u2014IIIIIIIIII\u2014";
printLine(graphics, target, "Arial");
printLine(graphics, target, "Times New Roman");
printLine(graphics, target, "Dialog");
return pageIndex == 0 ? PAGE_EXISTS : NO_SUCH_PAGE;
}
private int lineCounter = 0;
private void printLine(Graphics graphics, String text, String fontName) {
graphics.setFont(new Font(fontName, Font.PLAIN, 20));
graphics.drawString(text, 20, 20 * ++lineCounter);
}
});
job.print();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment