возникли проблемы с печатью текста, изображения или отчета на принтере в Java 11 - PullRequest
0 голосов
/ 18 марта 2019

У меня проблема с печатью любого значка, текста или отчета в java 9 или более поздних версиях. Мой код работает отлично на Java 8, но мы с 9 Java до 11 Java это работает. Невозможно распечатать документ.

private void cmdPrintActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {

        printComponent(frmDoc, true);
        printComponent(frmDoc, false);
    } catch (PrinterException exp) {
        javax.swing.JOptionPane.showMessageDialog(this, exp.getMessage());
    }
}



public class ComponentPrinter implements Printable{
private Component comp;
private boolean fill;
public ComponentPrinter(Component comp, boolean fill) {
    this.comp = comp;
    this.fill = fill;
}
@Override
public int print(Graphics g, PageFormat format, int page_index) throws PrinterException {
    if (page_index > 0) {
        return Printable.NO_SUCH_PAGE;
    }

    Graphics2D g2 = (Graphics2D) g;
    g2.translate(format.getImageableX(), format.getImageableY());
    double width = (int) Math.floor(format.getImageableWidth());
    double height = (int) Math.floor(format.getImageableHeight());
    if (!fill) {
        width = Math.min(width, comp.getPreferredSize().width);
        height = Math.min(height, comp.getPreferredSize().height);

    }

    comp.setBounds(0, 0, (int) Math.floor(width), (int) Math.floor(height));
    if (comp.getParent() == null) {
        comp.addNotify();
    }
    comp.validate();
    comp.doLayout();
    comp.printAll(g2);
    if (comp.getParent() != null) {
        comp.removeNotify();
    }
    return Printable.PAGE_EXISTS;
}

}

public static void printComponent(JComponent comp, boolean fill) throws PrinterException {
    PrinterJob pjob = PrinterJob.getPrinterJob();
    PageFormat pf = pjob.defaultPage();
    pf.setOrientation(PageFormat.PORTRAIT);

    PageFormat postformat = pjob.pageDialog(pf);
    if (pf != postformat) {
        //Set print component
        pjob.setPrintable(new ComponentPrinter(comp, fill), postformat);
        //pjob.print();

        //pjob.
        if (pjob.printDialog()) {
            pjob.print();
        }
    }
}

Когда я запускаю программу, я получаю эту ошибку в консоли. output error

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