Java Проблема печати при использовании других компьютеров - PullRequest
0 голосов
/ 09 января 2020

Вывод такой, когда я печатаю его с моего компьютера

Фактический вывод

Но когда я печатаю его с другого компьютера, он печатает только в этой области

Выделено желтым

package attendancesystem;
import java.awt.*;
import java.awt.geom.AffineTransform;
import javax.swing.*;
import java.awt.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.PrintQuality;
import javax.print.attribute.standard.PrinterResolution;

public class PrintUtilities implements Printable {
  private Component componentToBePrinted;

  public static void printComponent(Component c) {
    new PrintUtilities(c).print();
  }

  public PrintUtilities(Component componentToBePrinted) {
    this.componentToBePrinted = componentToBePrinted;
  }

  public void print() {
    PrinterJob job = PrinterJob.getPrinterJob();
    double cmPx300 = 300.0 / 2.54;
    Paper paper = new Paper();

    paper.setImageableArea(0, 0, 21.3 * cmPx300, 29.7 * cmPx300);
    PageFormat f = job.defaultPage();
    //f.setPaper(paper);
    // f.setOrientation(PageFormat.PORTRAIT);


    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(new PrinterResolution(300, 300, PrinterResolution.DPI));
    aset.add(PrintQuality.HIGH);
    aset.add(MediaSizeName.ISO_A4);
    //aset.add(new MediaPrintableArea(0, 0, 150, 100, MediaPrintableArea.MM));


    job.setPrintable(this, f);

    try {
      if (job.printDialog()) {
        job.print();

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

  }


  public static double getScaleFactor(int iMasterSize, int iTargetSize) {

    double dScale = 1;
    if (iMasterSize > iTargetSize) {

      dScale = (double) iTargetSize / (double) iMasterSize;

    } else {

      dScale = (double) iTargetSize / (double) iMasterSize;

    }

    return dScale;

  }


  public static double getScaleFactorToFit(Dimension original, Dimension toFit) {

    double dScale = 1 d;

    if (original != null && toFit != null) {

      double dScaleWidth = getScaleFactor(original.width, toFit.width);
      double dScaleHeight = getScaleFactor(original.height, toFit.height);

      dScale = Math.min(dScaleHeight, dScaleWidth);

    }

    return dScale;

  }

  public int print(Graphics g, PageFormat pf, int pagenum) {
    if (pagenum > 0) {
      return Printable.NO_SUCH_PAGE;
    }
    // Get the preferred size ofthe component...
    Dimension compSize = componentToBePrinted.getPreferredSize();
    // Make sure we size to the preferred size
    componentToBePrinted.setSize(compSize);
    componentToBePrinted.setVisible(true);
    // Get the the print size
    Dimension printSize = new Dimension();
    printSize.setSize(pf.getImageableWidth(), pf.getImageableHeight());

    // Calculate the scale factor
    double scaleFactor = getScaleFactorToFit(compSize, printSize);
    // Don't want to scale up, only want to scale down
    if (scaleFactor > 1 d) {
      scaleFactor = 1 d;
    }

    // Calcaulte the scaled size...
    double scaleWidth = compSize.width * scaleFactor;
    double scaleHeight = compSize.height * scaleFactor;

    // Create a clone of the graphics context.  This allows us to manipulate
    // the graphics context without begin worried about what effects
    // it might have once we're finished
    Graphics2D g2 = (Graphics2D) g.create();
    // Calculate the x/y position of the component, this will center
    // the result on the page if it can
    double x = ((pf.getImageableWidth() - scaleWidth) / 2 d) + pf.getImageableX();
    double y = ((pf.getImageableHeight() - scaleHeight) / 2 d) + pf.getImageableY();
    // Create a new AffineTransformation
    AffineTransform at = new AffineTransform();
    // Translate the offset to out "center" of page
    at.translate(x, y);
    // Set the scaling
    at.scale(scaleFactor, scaleFactor);
    // Apply the transformation
    g2.transform(at);
    // Print the component
    componentToBePrinted.printAll(g2);
    // Dispose of the graphics context, freeing up memory and discarding
    // our changes
    g2.dispose();

    componentToBePrinted.revalidate();

    return (PAGE_EXISTS);
  }



  public static void disableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
  }

  public static void enableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);
  }
}

Это код, который я использовал.

Я не знаю, как это происходит, но обычно он выдает ошибку, когда я использую другой компьютер.

Может быть, это зависит от разрешения экрана?

Мое разрешение - 1440x900, а для другого компьютера - 1366x768 или 1280x720

.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...