java上的graphics2d:打印时如何删除或减少页眉中的空格?

wnrlj8wa  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(294)

我正在使用graphics2d来设计我将要打印的内容。在我遇到这个问题之前一切都很好。
我想在开始打印时去掉空白。我试图更改代码中变量的一些值,但仍然找不到。也许我错过了这个节目。请帮帮我。

public PageFormat getPageFormat(PrinterJob pj)
{

PageFormat pf = pj.defaultPage();
Paper paper = pf.getPaper();    

double bodyHeight = 10;  
double headerHeight = 5.0;                  
double footerHeight = 5.0;        
double width = cm_to_pp(8); 
double height = cm_to_pp(headerHeight+bodyHeight+footerHeight); 
paper.setSize(width, height);
paper.setImageableArea(0,10,width,height - cm_to_pp(1));  

pf.setOrientation(PageFormat.PORTRAIT);  
pf.setPaper(paper);    

return pf;
}

protected static double cm_to_pp(double cm)
{            
        return toPPI(cm * 0.393600787);            
}

protected static double toPPI(double inch)
{            
        return inch * 72d;            
}

public class BillPrintable implements Printable {

 public int print(Graphics graphics, PageFormat pageFormat,int pageIndex) 
 throws PrinterException 
 {    

  int r= patientName.size();
  ImageIcon icon=new ImageIcon("logo.png"); 
  int result = NO_SUCH_PAGE;    
    if (pageIndex == 0) {                    

        Graphics2D g2d = (Graphics2D) graphics;                    
        double width = pageFormat.getImageableWidth();                               
        g2d.translate((int) pageFormat.getImageableX(),(int) pageFormat.getImageableY()); 
        Graphics2D g = (Graphics2D) graphics;   
        g.translate((int) pageFormat.getImageableX(),(int) pageFormat.getImageableY()); 
        Graphics2D g1 = (Graphics2D) graphics; 
        g1.translate((int) pageFormat.getImageableX(),(int) pageFormat.getImageableY()); 
        Graphics2D g2 = (Graphics2D) graphics; 
        g2.translate((int) pageFormat.getImageableX(),(int) pageFormat.getImageableY()); 
        Graphics2D g3 = (Graphics2D) graphics; 
        g3.translate((int) pageFormat.getImageableX(),(int) pageFormat.getImageableY()); 
        Graphics2D g4 = (Graphics2D) graphics; 
        g4.translate((int) pageFormat.getImageableX(),(int) pageFormat.getImageableY()); 

    try{
        int y=20;
        int yShift = 10;
        int headerRectHeight=8;
        int spaceClass= 12;
        int space = 20;

        g3.setFont(new Font("Monospaced",Font.PLAIN,9));
        g3.drawImage(icon.getImage(), 5, 20, 120, 80, rootPane);y+=yShift+65; //57 height between image and text
        //drawImage(Image img, int x, int y, int width, int height, ImageObserver observer);
        g3.drawString("----------------------------",0,y);y+=yShift;
        g3.drawString("     OSPITAL NG IMUS            ",0,y);y+=yShift;
        g2.setFont(new Font("Monospaced",Font.PLAIN,7));
        g2.drawString("   Pedro Reyes St. Malagasang    ",0,y);y+=yShift;
        g2.drawString("      I-G 4103 Imus, Cavite          ",0,y);y+=yShift;
        //g2d.drawString("www.facebook.com/ospitalngimus    ",0,y);y+=yShift;
        g2.drawString("         (046) 419 8300           ",0,y);y+=yShift;
        g2d.setFont(new Font("Monospaced",Font.PLAIN,9));
        g2d.drawString("------------------------------",0,y);y+=headerRectHeight;
        g2d.drawString("Patient Name:         ",0,y);y+=headerRectHeight;
        g2d.drawString(" "+txtName.getText(),0,y);y+=headerRectHeight;
        g2d.drawString("------------------------------",0,y);y+=yShift; 
        g2d.drawString("Patient ID: "+txtID.getText()+"            ",0,y);y+=headerRectHeight;
        g2d.drawString("-------------------------------",0,y);y+=yShift; 
        g2d.drawString("Symptoms:                ",0,y);y+=headerRectHeight; 
        g2d.drawString(" "+txtSymp.getText(),0,y);y+=headerRectHeight;
        g2d.drawString("-------------------------------",0,y);y+=yShift; 
        g2d.drawString("Classification: ",0,y);y+=spaceClass; 
        g.setFont(new Font("Monospaced",Font.PLAIN,14));
        g.drawString  (" "+txtClass.getText(),0,y);y+=yShift;y+=space;
        g4.setFont(new Font("Monospaced",Font.BOLD,30));
        g4.drawString  ("   1                 ",0,y);y+=headerRectHeight;
        g1.setFont(new Font("Monospaced",Font.PLAIN,9));
        g1.drawString("********************************",0,y);y+=yShift; 
        g1.drawString("         THANK YOU            ",0,y);y+=yShift;
        g1.drawString("********************************",0,y);y+=yShift;
        g1.drawString("    SOFTWARE BY: GROUP 3      ",0,y);y+=yShift;
        g1.drawString("                                    ",0,y);y+=yShift;       
        g1.drawString("                                     ",0,y);y+=yShift; 
         g1.drawString("                                    ",0,y);y+=yShift;       
        g1.drawString("                                     ",0,y);y+=yShift; 
        g1.drawString(".                                    ",0,y);y+=yShift; 

}
catch(Exception e){
e.printStackTrace();
}

          result = PAGE_EXISTS;    
      }    
      return result;    
  }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题