Skip to content

Instantly share code, notes, and snippets.

@lite1979
Created December 18, 2019 17:41
Show Gist options
  • Save lite1979/d2a8a15c7bb7bd96598223349e023d51 to your computer and use it in GitHub Desktop.
Save lite1979/d2a8a15c7bb7bd96598223349e023d51 to your computer and use it in GitHub Desktop.
public class ThermalTest {
public static void main(String[] args) throws Exception {
//print directly
new VectorPrintable();
//print via pdfbox
new PdfboxPrintable();
}
private static Map<RenderingHints.Key,Object> buildRenderHints() {
Map<RenderingHints.Key,Object> rhMap = new HashMap<>();
rhMap.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
rhMap.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
rhMap.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
rhMap.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
rhMap.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
rhMap.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
rhMap.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
rhMap.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
return rhMap;
}
static class VectorPrintable implements Printable {
VectorPrintable() throws PrinterException {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(PrintServiceLookup.lookupDefaultPrintService());
job.setPrintable(this);
job.print();
}
private void paint(Graphics2D g2d) {
g2d.setFont(new Font("Times New Roman", Font.PLAIN, 24));
g2d.setColor(Color.BLACK);
g2d.drawString("Thermal test [GRAPHICS]", 100f, 128f);
g2d.drawRect(72, 72, 128, 64);
}
@Override
public int print(Graphics graphics, PageFormat pageFormat, int index) {
if (index == 0) {
Graphics2D g2d = (Graphics2D)graphics;
g2d.addRenderingHints(buildRenderHints());
paint(g2d);
return PAGE_EXISTS;
} else {
return NO_SUCH_PAGE;
}
}
}
static class PdfboxPrintable {
PdfboxPrintable() throws PrinterException, IOException {
PDDocument document = new PDDocument();
paint(document);
PDFPrintable printable = new PDFPrintable(document, Scaling.ACTUAL_SIZE, false, 0, false);
printable.setRenderingHints(new RenderingHints(buildRenderHints()));
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(PrintServiceLookup.lookupDefaultPrintService());
job.setPrintable(printable);
job.print();
document.close();
}
private void paint(PDDocument document) throws IOException {
//I guess pdfbox content dimensions are upside-down? use this to flip easier
int height = 792; //72dpi * 11in
PDPage page = new PDPage();
PDPageContentStream content = new PDPageContentStream(document, page);
content.setFont(PDType1Font.TIMES_ROMAN, 24);
content.setStrokingColor(Color.BLACK);
content.beginText();
content.newLineAtOffset(28f, height - 56); //offset does not include margins
content.showText("Thermal test [PDFBOX]");
content.endText();
content.addRect(0, height - 72, 128, 64);
content.stroke();
content.close();
document.addPage(page);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment