Return-Path: X-Original-To: apmail-pdfbox-users-archive@www.apache.org Delivered-To: apmail-pdfbox-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EA2121017B for ; Wed, 10 Jul 2013 14:08:18 +0000 (UTC) Received: (qmail 14756 invoked by uid 500); 10 Jul 2013 14:08:18 -0000 Delivered-To: apmail-pdfbox-users-archive@pdfbox.apache.org Received: (qmail 14357 invoked by uid 500); 10 Jul 2013 14:08:13 -0000 Mailing-List: contact users-help@pdfbox.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@pdfbox.apache.org Delivered-To: mailing list users@pdfbox.apache.org Received: (qmail 14338 invoked by uid 99); 10 Jul 2013 14:08:11 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 10 Jul 2013 14:08:11 +0000 X-ASF-Spam-Status: No, hits=0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE X-Spam-Check-By: apache.org Received-SPF: error (nike.apache.org: local policy) Received: from [81.169.146.161] (HELO mo-p00-ob.rzone.de) (81.169.146.161) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 10 Jul 2013 14:08:04 +0000 X-RZG-AUTH: :LWIAZ0WpaN8UY5o8XRz0jOyrHsdEC+nAE10OdySrgHL6ku8U1wBfg85cAw== X-RZG-CLASS-ID: mo00 Received: from [192.168.1.8] (dslb-088-076-252-005.pools.arcor-ip.net [88.76.252.5]) by smtp.strato.de (josoe mo11) (RZmta 31.29 DYNA|AUTH) with ESMTPA id 00333fp6ADMwBT for ; Wed, 10 Jul 2013 16:07:25 +0200 (CEST) Message-ID: <51DD6A9D.2060609@lehmi.de> Date: Wed, 10 Jul 2013 16:07:25 +0200 From: Andreas Lehmkuehler User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130625 Thunderbird/17.0.7 MIME-Version: 1.0 To: users@pdfbox.apache.org Subject: Re: endless loop in printing Printable References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org 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; > } > } > >