I would really appreciate if you can help me with this little problem I`ve had for a while now.
The thing is that when i print a pdf wich contains a qr code, the qr code is not shown in the printed document, it just shows a blank space.
Please, I hope you can help me...Here is the code I`m using:
package utilities;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PDFRenderer;
import static java.awt.print.Printable.NO_SUCH_PAGE;
import static java.awt.print.Printable.PAGE_EXISTS;
/**
* Converts the PDF content into printable format
*/
public class PrintPdf2 {
private PrinterJob pjob = null;
public static void main(String[] args) throws IOException, PrinterException {
String ruta="C:\\examplePDF.pdf";
System.out.println("Printing: " + ruta);
// Create a PDFFile from a File reference
FileInputStream fis = new FileInputStream(ruta);
PrintPdf2 printPDFFile = new PrintPdf2(fis, "Test Print PDF");
printPDFFile.print();
}
public PrintPdf2(InputStream inputStream, String jobName) throws IOException, PrinterException {
byte[] pdfContent = new byte[inputStream.available()];
inputStream.read(pdfContent, 0, inputStream.available());
initialize(pdfContent, jobName);
}
public PrintPdf2(byte[] content, String jobName) throws IOException, PrinterException {
initialize(content, jobName);
}
private void initialize(byte[] pdfContent, String jobName) throws IOException, PrinterException {
ByteBuffer bb = ByteBuffer.wrap(pdfContent);
// Create PDF Print Page
PDFFile pdfFile = new PDFFile(bb);
PDFPrintPage pages = new PDFPrintPage(pdfFile);
// Create Print Job
pjob = PrinterJob.getPrinterJob();
PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
pjob.setJobName(jobName);
Book book = new Book();
book.append(pages, pf, pdfFile.getNumPages());
pjob.setPageable(book);
// to remove margins
Paper paper = new Paper();
paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
pf.setPaper(paper);
}
public void print() throws PrinterException {
// Send print job to default printer
pjob.print();
}
}
/**
* Class that actually converts the PDF file into Printable format
*/
class PDFPrintPage implements Printable {
private PDFFile file;
PDFPrintPage(PDFFile file) {
this.file = file;
}
public int print(Graphics g, PageFormat format, int index) throws PrinterException {
int pagenum = index + 1;
if ((pagenum >= 1) && (pagenum <= file.getNumPages())) {
Graphics2D g2 = (Graphics2D) g;
PDFPage page = file.getPage(pagenum);
// fit the PDFPage into the printing area
Rectangle imageArea = new Rectangle((int) format.getImageableX(), (int) format.getImageableY(),
(int) format.getImageableWidth(), (int) format.getImageableHeight());
g2.translate(0, 0);
PDFRenderer pgs = new PDFRenderer(page, g2, imageArea, null, null);
try {
page.waitForFinish();
pgs.run();
} catch (InterruptedException ie) {
// nothing to do
}
return PAGE_EXISTS;
} else {
return NO_SUCH_PAGE;
}
}
}
Thanks in advance :)