The following code works, but when I print to the PDFCreator printer driver, its default title is "Java Printing". (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.)
Is there a way to change this from "Java Printing" to another string?
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();
}
}
}
print(): print() method in Java is used to display a text on the console. This text is passed as the parameter to this method in the form of String. This method prints the text on the console and the cursor remains at the end of the text at the console.
You can print any text you want with the command, as long as the command System. out. println("arbitrary text"); — i.e., System dot out dot println open parenthesis ( "the text" close parenthesis ) and semicolon ; remains unchanged. The command below will print the text "Hello there!".
print( ) method The print() method prints the required output on the same line continuously again and again on the screen.
Have you tried this setJobName( String jobName ) .
job.setJobName("New Printing Name");
The API says that it is the name of the document to be printed.
I am running my code on Ubuntu, it doesn't print the title, so I am unable to verify whether it works or not.
Same answer, but for DocPrintJob
:
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new JobName("your job name", Locale.getDefault()));
docPrintJob.print(docToPrint, pras);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With