Problema ao gerar simples PDF com PDFBOX, gera PDF em branco!

Pessoal estou tentando fazer um teste bem simples de imprimir texto e gerar um arquivo PDF, mas não estou conseguindo… ele fica gerando o PDF em branco e não me retorna nenhuma excessão… alguém poderia me ajudar

import java.awt.Color;
import java.io.IOException;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class TestePDF {

	public static void main(String[] args) throws IOException, COSVisitorException {

		String filename = "C:\\PdfWithtext4.pdf";
		PDDocument doc = new PDDocument();
		PDPage page = new PDPage();
		doc.addPage(new PDPage());
	 	PDPageContentStream stream = new PDPageContentStream(doc, page);
		
		    stream.beginText();
		    stream.setNonStrokingColor(Color.blue );
		    stream.moveTextPositionByAmount(250, 750);
		    stream.drawString("mensagem aqui");
		    stream.endText();
		    stream.close();
		    doc.save(filename);
		

	}

}

@Rodrigoappellegrini

Eu uso PDF View, acho q é melhor:

Esta é minha classe:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package classes;

import com.sun.pdfview.PDFFile;
import entidades.Config;
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.PrinterJob;

public class Print {

    private final Config con;

    public Print(Config imp) {
        this.con = imp;
    }

    public boolean imprimir(java.io.File arquivo) {
        try {
            java.io.FileInputStream fis = new java.io.FileInputStream(arquivo);
            java.nio.channels.FileChannel fc = fis.getChannel();
            java.nio.ByteBuffer bb = fc.map(java.nio.channels.FileChannel.MapMode.READ_ONLY, 0, fc.size());
            PDFFile pdfFile = new PDFFile(bb);
            PrinterJob pjob = PrinterJob.getPrinterJob();
            pjob.setPrintService(new controll.ImpressoraControll(con).getImpressoraImpPDF());
            PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
            pf.setOrientation(con.getOrientacao());
            pf = pjob.validatePage(pf);
            pjob.setJobName(arquivo.getName());
            Book book = new Book();
            book.append(new PDFPrintPage1(pdfFile), pf, pdfFile.getNumPages());
            pjob.setPageable(book);
            Paper paper = new Paper();
            paper.setSize(595, 842); // A4 dimensions in font points
            paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
            pf.setPaper(paper);
            pjob.print();
            closeDirectBuffer(bb);
            fc.close();
            fis.close();
            return true;
        } catch (java.io.IOException | java.awt.print.PrinterException | IllegalArgumentException | NullPointerException a) {
            new controll.LogControll().appendError(this.getClass().getName(), Thread.currentThread().getStackTrace()[1].getMethodName(), a.getMessage());
            return false;
        }
    }

    private void closeDirectBuffer(java.nio.ByteBuffer cb) {
        if (cb == null || !cb.isDirect()) {
            return;
        }
        try {
            java.lang.reflect.Method cleaner = cb.getClass().getMethod("cleaner");
            cleaner.setAccessible(true);
            java.lang.reflect.Method clean = Class.forName("sun.misc.Cleaner").getMethod("clean");
            clean.setAccessible(true);
            clean.invoke(cleaner.invoke(cb));
        } catch (NoSuchMethodException | SecurityException | ClassNotFoundException | IllegalAccessException | IllegalArgumentException | java.lang.reflect.InvocationTargetException ex) {
            new controll.LogControll().appendError(this.getClass().getName(), Thread.currentThread().getStackTrace()[1].getMethodName(), ex.getMessage());
        }
        cb = null;
    }

    private class PDFPrintPage1 implements java.awt.print.Printable {

        private final PDFFile file;

        PDFPrintPage1(PDFFile file) {
            this.file = file;
        }

        @Override
        public int print(java.awt.Graphics g, PageFormat pf, int index) {
            int pagenum = index + 1;
            if ((pagenum >= 1) && (pagenum <= file.getNumPages())) {
                Graphics2D g2 = (Graphics2D) g;
                com.sun.pdfview.PDFPage page = file.getPage(pagenum);
                Rectangle imageArea = new Rectangle((int) pf.getImageableX(), (int) pf.getImageableY(),
                        (int) pf.getImageableWidth(), (int) pf.getImageableHeight());
                g2.translate(0, 0);
                com.sun.pdfview.PDFRenderer pgs = new com.sun.pdfview.PDFRenderer(page, g2, imageArea, null, null);
                try {
                    page.waitForFinish();
                    pgs.run();
                } catch (InterruptedException ex) {
                    new controll.LogControll().appendError(this.getClass().getName(), Thread.currentThread().getStackTrace()[1].getMethodName(), ex.getMessage());
                }
                return 0;
            } else {
                return 1;
            }
        }
    }
}

Ela esta pronta, é só passar o arquivo do tipo File ao metodo imprimir, porém deve baixar as bibliotecas pdf-renderer.jar e acho q tem q baixar o commons-io-2.5.jar

Quase me esqueci, eu tenho uma entidade chamada Config, la tem o nome da impressora que será impresso, obviamente, porém no local que esta escrito:

pjob.setPrintService(new controll.ImpressoraControll(con).getImpressoraImpPDF());

mude para

pjob.setPrintService(javax.print.PrintServiceLookup.lookupDefaultPrintService().getName());

Assim pega impressora padrão

@4mega

Muito obrigado pela resposta!!

achei o que estava fazendo errado

Troquei essa linha:
doc.addPage(new PDPage());
por essa:

doc.addPage(page);

1 curtida