Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printing in java sets a title somewhere to "Java Printing"

Tags:

java

printing

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();
        }
    }   
}
like image 749
Jason S Avatar asked Jun 03 '11 15:06

Jason S


People also ask

What is the printing method in Java?

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.

Which of this command is the correct way of printing in Java?

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!".

How do you print on the same line in Java?

print( ) method The print() method prints the required output on the same line continuously again and again on the screen.


2 Answers

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.

like image 131
kensen john Avatar answered Oct 04 '22 01:10

kensen john


Same answer, but for DocPrintJob:

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

docPrintJob.print(docToPrint, pras);
like image 40
user11153 Avatar answered Oct 04 '22 00:10

user11153