pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeroen Verhagen <jeroenverha...@gmail.com>
Subject endless loop in printing Printable
Date Tue, 02 Jul 2013 11:25:51 GMT
Hi all,

I'm using PDFBox 1.8.2 to print a pdf to my printer using a certain tray.
However the following code (below) produces a pdf being sent to the printer
with an endless number of pages. Can anyone tell me what's causing this or
how to avoid this?

Btw if I use the PDDocument silentPrint() method this problem does not
occur and only 1 page is printed but then I cannot supply
PrintRequestAttributes to say to which tray to print.

Thanks for any help.

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.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.HashPrintServiceAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.*;
import java.awt.print.PrinterJob;
import java.io.IOException;

public class PDFBoxTest {

    public static void main(String[] args) throws IOException {
        PDDocument document = new PDDocument();
        try {
            // Get a named printer
            PrintServiceAttributeSet pSet = new
            pSet.add(new PrinterName("PDFCreator", null));
            PrintService[] pServices =
PrintServiceLookup.lookupPrintServices(null, pSet);
            if (pServices.length > 0) {
                // Get media tray
                MediaTray mt = getMediaTray(pServices[0], "Tray 3");
                PrintRequestAttributeSet attrs = new
                if (mt != null) {
                    attrs = new HashPrintRequestAttributeSet(mt);

                attrs.add(new Copies(3));

                // Get the printer job
                PrinterJob pJob = PrinterJob.getPrinterJob();

                // Attribute set

                PDPage page = new PDPage();

                // Create a new font object selecting one of the PDF base
                PDFont font = PDType1Font.HELVETICA_BOLD;

                // Start a new content stream which will "hold" the to be
created content
                PDPageContentStream contentStream = new
PDPageContentStream(document, page);

                // Define a text content stream using the selected font,
moving the cursor and drawing the text "Hello World"
                contentStream.setFont(font, 12);
                contentStream.moveTextPositionByAmount(100, 700);
                contentStream.drawString("Hello World");

                // Make sure that the content stream is closed:

                // print
        } catch (Throwable t) {
        } finally {


    private static MediaTray getMediaTray(PrintService ps, String trayName)
        // Get supported media
        Media[] media = (Media[])
ps.getSupportedAttributeValues(Media.class, null, null);
        for (int count = 0; count < media.length; ++count) {
            if (media[count] instanceof MediaTray) {
                if (trayName.equals(media[count].toString())) {
                    return (MediaTray) media[count];

        return null;




  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message