0

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);
    }
}
Namal
  • 281
  • 2
  • 10
  • 1
    Could you share the PDF? I've printed from Java without setting any resolution. Could it be that your images are of low resolution? Try also using 2.0.19 and 2.0.20. 2.0.20 has a bug that some images may be blurry, which will be fixed in 2.0.21: https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox-app/2.0.21-SNAPSHOT/ – Tilman Hausherr Aug 13 '20 at 10:49
  • It was the case indeed! i changed to 19 and it worked perfectly well! Thank you. – Namal Aug 13 '20 at 11:00

0 Answers0