I used this example to write a program for PDF printing. But I found out, that java standard print for images is set to 72 DPI. So I searched further and added attributes for changing the resolution. But it does not work, it is still 72 DPI. The funny part is, that black text is perfectly fine. But if the color of the text is not black it looks as bad as images (logos) or bar codes. All the solutions I have found are for image printing and not pdfs. So I would like to ask how I can change the following code that it might still work. Thx.
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.PrinterResolution;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.MediaPrintableArea;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;
public class App {
public static void main(String[] args) throws PrinterException, IOException {
if (args.length != 1)
{
System.err.println("usage: java " + App.class.getName() + " <input>");
System.exit(1);
}
String filename = args[0];
try (PDDocument document = PDDocument.load(new File(filename)))
{
print(document);
}
}
private static void print(PDDocument document) throws PrinterException
{
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(document));
PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
attr.add(MediaSizeName.ISO_A4);
attr.add(new PrinterResolution(300, 300, PrinterResolution.DPI));
attr.add(new MediaPrintableArea(2, 2, 210 - 4, 297 - 4, MediaPrintableArea.MM));
job.print(attr);
}
}