Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print pdf with java

Tags:

java

printing

pdf

I have tried to print pdf with this code:

package imprimir;

import java.io.*;
import java.awt.print.*;
import java.awt.print.PrinterJob.*;
import java.awt.print.PageFormat.*;

public class Imprimir {

    static public void main(String[] args) {
        PrinterJob job = PrinterJob.getPrinterJob();
        PageFormat pf = job.defaultPage();
        Paper paper = new Paper();
        paper.setSize(612.0, 832.0);
        double margin = 10;
        paper.setImageableArea(margin, margin, paper.getWidth() - margin, paper.getHeight() - margin);
        pf.setPaper(paper);
        pf.setOrientation(PageFormat.LANDSCAPE);
        job.setPrintable(new ObjetoDeImpresion(), pf);
        job.setJobName("funciona?");
        try {
            job.print();
        } catch (PrinterException e) {
            System.out.println(e);
        }
    }
}


package imprimir;

import java.awt.*;
import java.awt.print.*;

public class ObjetoDeImpresion implements Printable {

    public int print(Graphics g, PageFormat f, int pageIndex) {
        if (pageIndex == 0) {
            g.drawString("Hola ivan", 100, 200);
            return PAGE_EXISTS;
        } else {
            return NO_SUCH_PAGE;
        }
    }
}

and also from other different ways there on the internet, but with all the ways I've tried, when I print the document, print odd numbers and letters, like this:

% PDF ||1.6
endobobj <</linerrized 1/L 20597/O 40/E 14115/N 1/T ............
xref
37 34
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
...
..
..
trailer
<</Size...... 

Someone can help me achieve print my document?

like image 620
Ivan Avatar asked Feb 05 '26 18:02

Ivan


1 Answers

I think PDFBox from Apache better suit your need (http://pdfbox.apache.org/).

Here is how it can fit inside your code:

static public void main(String[] args) {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    Paper paper = new Paper();
    paper.setSize(612.0, 832.0);
    double margin = 10;
    paper.setImageableArea(margin, margin, paper.getWidth() - margin, paper.getHeight() - margin);
    pf.setPaper(paper);
    pf.setOrientation(PageFormat.LANDSCAPE);

    // PDFBox
    PDDocument document = PDDocument.load("yourfile.pdf");
    job.setPageable(new PDPageable(document, job));

    job.setJobName("funciona?");
    try {
        job.print();
    } catch (PrinterException e) {
        System.out.println(e);
    }
}

You can find more info about this if you look at the source of org.apache.pdfbox.PrintPDF.

like image 179
Raphaël Avatar answered Feb 07 '26 08:02

Raphaël



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!