0

I am attempting to print Word documents generated using docx4j that I convert to PDF and print using PDFBox. The document I'm attempting to print just contains a simple table with basic English text.

This is the method I use:

    private void printDocument(){
    PrinterJob job = PrinterJob.getPrinterJob();

    try {
        PDDocument document;

        document = PDDocument.load("files\\textDocs\\OITScanSheet.pdf");

        job.setPageable(new PDPageable(document, job));
        job.setJobName("OIT Scan Sheet");
        job.print();

    } catch (NullPointerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (PrinterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

This is what I'm trying to print: Before print

And this is what comes out when I print (Sorry for the terrible quality): After print

The table is intact with the correct number of rows, but the text is not there. When I print the document from Adobe Reader it prints as it should.

I'm using PDFBox 2.0.0-snapshot right now. I switched from version 1.8.4 because I was running into this problem, where it was printing the table correctly but all of the text was junk.

I'm fairly certain the problem has something to do with some sort of font problem behind the scenes, but I have no idea how to go about fixing it. Any help would be greatly appreciated.

Community
  • 1
  • 1
  • 1
    Have you tried relegating printing via the [`Desktop`](http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html) class instead? For example: `Desktop.getDesktop().print(new File("path to your Word document"));` – Ben Aug 02 '14 at 11:54
  • 1
    That's what I'm going to have to end up doing. I wanted a print dialog box, but I can make it work with the Desktop class. Thanks for the response. – Josh Klingler Aug 04 '14 at 13:48
  • @JoshKlingler can you link the document `OITScanSheet.pdf`. – UdayKiran Pulipati Aug 08 '14 at 05:08

1 Answers1

0

If you really need the printDialog, this is a ready to use code, assuming you have PDFBox 2.0 and font 2.0 in your classpath

  public static void PDFPrintingServices(String filePath) throws IOException, PrinterException {

            PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
            attr_set.add(MediaSizeName.ISO_A4);
            attr_set.add(Sides.ONE_SIDED);
            PDDocument pdf = PDDocument.load(new File(filePath));
            PDFPageable p = new PDFPageable(pdf);
            PDFPrintable printable = new PDFPrintable(pdf, Scaling.SCALE_TO_FIT);

            PrinterJob job = PrinterJob.getPrinterJob();
            job.setJobName("My Printer");
            job.setPageable(p);
            job.setPrintable(printable);
            if(job.printDialog()) {
                job.print();
                job.print(attr_set);
            }
    }
benito
  • 655
  • 6
  • 12