2016-07-26 2 views
0

프린터에 PNG 이미지를 인쇄 중입니다. 이미지는 페이지 중앙에 인쇄되며 전체 페이지를 채우지 않습니다. 나는 이미지의 크기를 늘리려고했지만 항상 페이지의 중앙에 위치시켰다. 어떤 아이디어라도 페이지에 맞게 만드는 방법은 무엇입니까?이미지를 인쇄 할 때 페이지에 맞추기

psStream = new URL(url).openStream(); 
       if (psStream == null) { 
        return "Unable to fetch image"; 
       } 
       DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG; 
       Doc myDoc = new SimpleDoc(psStream, flavor, null); 
       PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 
       PrintServiceAttributeSet attributes = new HashPrintServiceAttributeSet(); 
       attributes.add(new PrinterName(printData.printer, Locale.getDefault())); 
       final PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor, attributes); 
       if (printServices.length == 0) { 
        return "Could not find printer " + printData.printer; 
       } else { 
        myPrinter = printServices[0]; 
        DocPrintJob job = myPrinter.createPrintJob(); 

        try { 
         job.print(myDoc, aset); 
         return null; 
        } catch (Exception e) { 
         e.printStackTrace(); 
         return "Could not print : " + e.getMessage(); 
        } 

       } 
+0

중복 된 http://stackoverflow.com/questions/27029166/java-printerjob-not-printing-to-fit-paper – SomeDude

답변

0

기본적으로 페이지 크기에 맞게 이미지의 크기를 조정해야합니다. 또한 프린터 드라이버는 기본적으로 페이지 테두리 영역을 정의합니다. 따라서 전체 페이지를 사용하려면 해당 테두리를 제거해야합니다. 그러나 때로 프린터 드라이버에 의해 제어되므로 테두리를 제거하는 것이 불가능하므로 제어하는 ​​드라이버 만 제어 할 수 있습니다.

public void print(String imageFileName) throws IOException, PrinterException { 
    final PrintService printService = PrintServiceLookup.lookupDefaultPrintService(); 
    final BufferedImage image = ImageIO.read(new File(imageFileName)); 
    final PrinterJob printJob = PrinterJob.getPrinterJob(); 
    printJob.setJobName("MyApp: " + imageFileName); 
    printJob.setPrintService(printService); 
    printJob.setPrintable(new Printable() { 
     @Override 
     public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { 
      if (pageIndex == 0) { 
       final Paper paper = pageFormat.getPaper(); 
       paper.setImageableArea(0.0, 0.0, pageFormat.getPaper().getWidth(), pageFormat.getPaper().getHeight()); 
       pageFormat.setPaper(paper); 
       graphics.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY()); 
       graphics.drawImage(image, 0, 0, (int) pageFormat.getPaper().getWidth(), (int) pageFormat.getPaper().getHeight(), null); 
       return PAGE_EXISTS; 
      } else { 
       return NO_SUCH_PAGE; 
      } 
     } 
    }); 
    printJob.print(); 
} 

이미지를 확대 인쇄하지 않으려면 이미지 크기를 올바르게 조정해야합니다. 동일하게 보이고 이미지의 크기를 조정하는 방법을 보여주는 또 다른 게시물이 있습니다. Fit image into the printing area

0

다음은 Java Swing JPanel을 인쇄하는 데 사용하는 코드입니다. PNG에서 BufferedImage를 인쇄하도록 수정할 수 있습니다.

필자는 이미지 비례를 유지하므로 페이지를 수평 또는 수직으로 만 채 웁니다.

package com.ggl.sudoku.solver.controller; 

import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.awt.print.PageFormat; 
import java.awt.print.Printable; 
import java.awt.print.PrinterException; 
import java.awt.print.PrinterJob; 

import javax.swing.JPanel; 

import com.ggl.sudoku.solver.view.SudokuFrame; 

public class PrintActionListener implements Runnable { 

    private SudokuFrame frame; 

    public PrintActionListener(SudokuFrame frame) { 
     this.frame = frame; 
    } 

    @Override 
    public void run() { 
     final BufferedImage image = createPanelImage(); 

     PrinterJob printJob = PrinterJob.getPrinterJob(); 
     printJob.setPrintable(new ImagePrintable(printJob, image)); 

     if (printJob.printDialog()) { 
      try { 
       printJob.print(); 
      } catch (PrinterException prt) { 
       prt.printStackTrace(); 
      } 
     } 
    } 

    private BufferedImage createPanelImage() { 
     JPanel panel = frame.getSudokuPanel(); 
     BufferedImage image = new BufferedImage(panel.getWidth(), 
       panel.getHeight(), BufferedImage.TYPE_INT_RGB); 
     Graphics2D g = image.createGraphics(); 
     panel.paint(g); 
     g.dispose(); 
     return image; 
    } 

    public class ImagePrintable implements Printable { 

     private double   x, y, width; 

     private int    orientation; 

     private BufferedImage image; 

     public ImagePrintable(PrinterJob printJob, BufferedImage image) { 
      PageFormat pageFormat = printJob.defaultPage(); 
      this.x = pageFormat.getImageableX(); 
      this.y = pageFormat.getImageableY(); 
      this.width = pageFormat.getImageableWidth(); 
      this.orientation = pageFormat.getOrientation(); 
      this.image = image; 
     } 

     @Override 
     public int print(Graphics g, PageFormat pageFormat, int pageIndex) 
       throws PrinterException { 
      if (pageIndex == 0) { 
       int pWidth = 0; 
       int pHeight = 0; 
       if (orientation == PageFormat.PORTRAIT) { 
        pWidth = (int) Math.min(width, (double) image.getWidth()); 
        pHeight = pWidth * image.getHeight()/image.getWidth(); 
       } else { 
        pHeight = (int) Math.min(width, (double) image.getHeight()); 
        pWidth = pHeight * image.getWidth()/image.getHeight(); 
       } 
       g.drawImage(image, (int) x, (int) y, pWidth, pHeight, null); 
       return PAGE_EXISTS; 
      } else { 
       return NO_SUCH_PAGE; 
      } 
     } 

    } 

} 
관련 문제