Java Printing Stretch Font Extension

I just got a printer to work in java, as I need it too, but there is one last problem that I need to solve. When it prints, the width of the font is quite stretched, and not clear and transparent, as it should be.

Here is my code, my actual drawing on paper:

FontMetrics metrics = graphics.getFontMetrics(font); int lineHeight = metrics.getHeight(); arrangePage(graphics, pageFormat, lineHeight); if (page > pageBreaks.length){ return NO_SUCH_PAGE; } Graphics2D g = (Graphics2D) graphics; g.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); g.setFont(font); int y = 0; int begin = 0; if (page == 0){ begin = 0; }else begin = pageBreaks[page-1]; int end = 0; if (page == pageBreaks.length){ end = lines.length; }else end = pageBreaks[page]; for (int line = begin; line < end; line++){ y += lineHeight; g.drawString(lines[line], 0, y); } string = deepCopy; return PAGE_EXISTS; 

How do I get rid of stretch marks? It can be noted that this is based on this tutorial: http://docs.oracle.com/javase/tutorial/2d/printing/set.html

Any advice or help is appreciated.

+4
source share
2 answers

By default, DPI is a normal 72 DPI (I believe), which is pretty bad on printed paper. You need to request a print API to try to find a printer with the best DPI.

Basically you need to use the print services API. A.

Try something like ...

 public class PrintTest01 { public static void main(String[] args) { PrinterResolution pr = new PrinterResolution(300, 300, PrinterResolution.DPI); PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(MediaSizeName.ISO_A4); aset.add(pr); aset.add(OrientationRequested.PORTRAIT); PrinterJob pj = PrinterJob.getPrinterJob(); pj.setPrintable(new Page()); try { pj.print(aset); } catch (PrinterException ex) { ex.printStackTrace(); } } public static class Page implements Printable { @Override public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex > 0) { return NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D) g; g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); g.setFont(new Font("Arial", Font.PLAIN, 128)); FontMetrics fm = g.getFontMetrics(); int x = (int)(pageFormat.getWidth() - fm.stringWidth("A")) / 2; int y = (int)((pageFormat.getHeight() - fm.getHeight()) / 2) + fm.getAscent(); g2d.drawString("A", x, y); return PAGE_EXISTS; } } } 

You can find Working with print services and some help attributes ...

I have to warn you, this will print the first printout that it can find, which matches PrintRequestAttributeSet . You can also add in the print dialog to see what it does, but this is another level of difficulty I can live without right now;)

+2
source

Worked Above! To open the print dialog with it, use this:

  PrinterJob job = PrinterJob.getPrinterJob(); TextDocumentPrinter document = new TextDocumentPrinter(); PrinterResolution pr = new PrinterResolution(300, 300, PrinterResolution.DPI); PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(MediaSizeName.ISO_A4); aset.add(pr); aset.add(OrientationRequested.PORTRAIT); job.setPrintable(document); boolean doPrint = false; if (showDialog){ doPrint = job.printDialog(aset); }else doPrint = true; if (doPrint){ try{ job.print(); }catch(PrinterException e){ e.printStackTrace(); } } 

The aset variable contains all of your new default values, and by connecting them to printDialog, they are entered into printJob and therefore displayed on paper! They can also be changed in the dialog box.

0
source

All Articles