pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andreas Lehmkuehler <andr...@lehmi.de>
Subject Re: endless loop in printing Printable
Date Wed, 10 Jul 2013 14:07:25 GMT
Hi,

Am 02.07.2013 13:25, schrieb Jeroen Verhagen:
> 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?
What is on those pages? Are they repeated or do they just contain garbage?

> 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.
Hmmm, silentPrint and print are the very same from the printing POV. The only
difference is the print options dialog which is presented to the user before
printing. Saying that, I can't image that you are really doing the same.

> 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
> HashPrintServiceAttributeSet();
>              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
> HashPrintRequestAttributeSet();
>                  if (mt != null) {
>                      attrs = new HashPrintRequestAttributeSet(mt);
>                  }
>
>                  attrs.add(new Copies(3));
>                  attrs.add(Sides.ONE_SIDED);
>
>                  // Get the printer job
>                  PrinterJob pJob = PrinterJob.getPrinterJob();
>                  pJob.setPrintService(pServices[0]);
>
>                  // Attribute set
>
>                  PDPage page = new PDPage();
>                  document.addPage(page);
>
>                  // Create a new font object selecting one of the PDF base
> fonts
>                  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.beginText();
>                  contentStream.setFont(font, 12);
>                  contentStream.moveTextPositionByAmount(100, 700);
>                  contentStream.drawString("Hello World");
>                  contentStream.endText();
>
>                  // Make sure that the content stream is closed:
>                  contentStream.close();
>
>                  // print
>                  pJob.setPrintable(document.getPrintable(0));
>                  pJob.print(attrs);
>              }
>          } catch (Throwable t) {
>              t.printStackTrace();
>          } finally {
>              document.close();
>          }
>
>      }
>
>      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;
>      }
> }
>
>


Mime
View raw message