为什么别人通过网络访问后,主机上仍然出现PrinterJob[JAVA]对话框?

发布于 2025-01-04 15:21:36 字数 1697 浏览 1 评论 0原文

再会!

我目前正在开发一个程序,需要将某些数据直接打印到打印机。幸运的是,我已经使用了 Java 中的 PrinterJob 和 Printable 实用程序。这是代码:

SWFPrintableUtil oUtil = new SWFPrintableUtil(sResult);//SWFPrintableUtil implements Printable

PrinterJob oPrinterJob = PrinterJob.getPrinterJob();

oPrinterJob.setPrintable(oUtil);

boolean bDoPrint = oPrinterJob.printDialog();
if ( bDoPrint )
{
    try 
    {
        oPrinterJob.print();
    } 
    catch (PrinterException e) 
    {
        e.printStackTrace();
    }
}
else
{
    ;
}

这是实现 Printable 接口的代码:

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

    if (pageIndex != 0) 
    {
        return NO_SUCH_PAGE;
    }

    System.out.println(csContents);
    Graphics2D g2d = (Graphics2D) graphics;
    int nX = (int) pageFormat.getImageableX();
    int nY = (int) pageFormat.getImageableY();
    g2d.translate(nX, nY);

    Font oFont = new Font("Serif", Font.PLAIN, 8);
    FontMetrics oMetrics = graphics.getFontMetrics(oFont);
    int nLineHeight = oMetrics.getHeight();

    BufferedReader oReader = new BufferedReader(new StringReader(csContents));

    String sLine = "";
    try 
    {
        while( (sLine = oReader.readLine()) != null )
        {
            nY = nY + nLineHeight;
            g2d.drawString(sLine, nX, nY);
        }
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

    return PAGE_EXISTS;
}

显然,这段代码可以做我想要的。单击按钮后通过网页触发此代码。我已将此网页部署在 Tomcat 服务器中。单击该按钮后,将出现一个对话框,供您选择打印机并设置页面布局(横向/纵向、纸张尺寸等)。假设我已经在“计算机A”中部署了我的网站(通过Tomcat),“计算机B”访问并尝试使用打印功能,所发生的情况是,打印的弹出对话框仍然出现在“计算机A”上。谁能帮我解决这个问题?提前致谢!

问候, 奥内布

Good day!

I am currently working on a program that needs to print a certain data directly to the printer. Fortunately, I've ran over the PrinterJob and the Printable utility in Java. Here is the code:

SWFPrintableUtil oUtil = new SWFPrintableUtil(sResult);//SWFPrintableUtil implements Printable

PrinterJob oPrinterJob = PrinterJob.getPrinterJob();

oPrinterJob.setPrintable(oUtil);

boolean bDoPrint = oPrinterJob.printDialog();
if ( bDoPrint )
{
    try 
    {
        oPrinterJob.print();
    } 
    catch (PrinterException e) 
    {
        e.printStackTrace();
    }
}
else
{
    ;
}

This is the code which implements the Printable interface:

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

    if (pageIndex != 0) 
    {
        return NO_SUCH_PAGE;
    }

    System.out.println(csContents);
    Graphics2D g2d = (Graphics2D) graphics;
    int nX = (int) pageFormat.getImageableX();
    int nY = (int) pageFormat.getImageableY();
    g2d.translate(nX, nY);

    Font oFont = new Font("Serif", Font.PLAIN, 8);
    FontMetrics oMetrics = graphics.getFontMetrics(oFont);
    int nLineHeight = oMetrics.getHeight();

    BufferedReader oReader = new BufferedReader(new StringReader(csContents));

    String sLine = "";
    try 
    {
        while( (sLine = oReader.readLine()) != null )
        {
            nY = nY + nLineHeight;
            g2d.drawString(sLine, nX, nY);
        }
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

    return PAGE_EXISTS;
}

Apparently, this code can do what I want. This code is triggered via a web page upon clicking a button. I've deployed this web page in Tomcat Server. Upon clicking the button, a dialog box appears for you to pick your printer and to layout your page(landscape/portrait, paper size etc etc.). Let's say I've deployed my website(via Tomcat) in "Computer A" and "computer B" accesses and tries to use the print functionality, what happens is that the pop-up dialog box for printing still appears to "Computer A". Can anyone help me to solve this problem? Thanks in advance!

Regards,
Oneb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别念他 2025-01-11 15:21:36

因为代码是在主机上运行的。我想您需要将文档/可打印文件下载到远程计算机(“计算机 B”)并在那里打印。

Because the code runs on the host computer. I guess you will need to download the document/printable to the remote computer ("Computer B") and print it there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文