简体   繁体   English

在 java 中打印将标题设置为“Java 打印”

[英]printing in java sets a title somewhere to “Java Printing”

The following code works, but when I print to the PDFCreator printer driver, its default title is "Java Printing".以下代码有效,但是当我打印到 PDFCreator 打印机驱动程序时,它的默认标题是“Java 打印”。 (I suspect this is true for Adobe Distiller as well, since if you search google for PDFs with Java Printing , you get a lot of results.) (我怀疑 Adobe Distiller 也是如此,因为如果您在Google 上搜索带有 Java Printing 的 PDF ,您会得到很多结果。)

Is there a way to change this from "Java Printing" to another string?有没有办法将它从“Java 打印”更改为另一个字符串?

package com.example.test.gui;

import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class TestPrint implements Printable 
{
    @Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex != 0)
            return NO_SUCH_PAGE;
        graphics.drawString("Hi there", 100, 100);
        return PAGE_EXISTS;
    }

    public void printPage() throws PrinterException
    {
        PrinterJob job = PrinterJob.getPrinterJob();
        boolean ok = job.printDialog();
        if (ok) {
            job.setPrintable(this);
            job.print();
        }
    }
    public static void main(String[] args) {
        try {
            new TestPrint().printPage();
        }
        catch (PrinterException e) {
            e.printStackTrace();
        }
    }   
}

Have you tried this setJobName( String jobName ).你试过这个 setJobName( String jobName )。

job.setJobName("New Printing Name");

The API says that it is the name of the document to be printed. API 说它是要打印的文档的名称。

I am running my code on Ubuntu, it doesn't print the title, so I am unable to verify whether it works or not.我在 Ubuntu 上运行我的代码,它不打印标题,所以我无法验证它是否有效。

Same answer, but for DocPrintJob :相同的答案,但对于DocPrintJob

PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
pras.add(new JobName("your job name", Locale.getDefault())); 

docPrintJob.print(docToPrint, pras);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM